配置struts开发环境,写第一个struts程序

第一个struts2程序

1 配置开发环境

1.1   在package explorer右键点击>new>web project>键入project name为struts2>选择javaEE5.0>点击finish完成创建了一个名为struts2的web 项目。


1.2   新建完成的struts2 web 项目目录结构:

Struts2

——src

——jre

——javaee 2 lib

——WebRoot

————META-INF

————WEB-INF

——————lib

——————web.xml

此时还不能进行struts的开发,要引进struts的jar包配置web.xml后才可以


1.3配置web项目使用struts2

》在WEB-INF 的lib目录中导入以下jar包:

1.commons-filefuload
2.commons-io
3.commons-lang
4.commons-logging
5.freemarker
6.javassist
7.ognl
8.struts2-core
9.xwork-core

》配置WEN-INF目录下的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<!—配置struts的核心过滤器-->
 	<filter>
 	<filter-name>struts2</filter-name>
	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
       <!—可选参数-->
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>com.mycompany.myapp.actions</param-value>
        </init-param>
 	</filter>
 <!—将配置的拦截器映射到所有文件上-->
 	<filter-mapping>
 		<filter-name>struts2</filter-name>
 		<url-pattern>/*</url-pattern>
 	</filter-mapping>
 	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

1.4   测试你的struts2是否配置成功

在你的Tomcat中发布web项目struts2

在你的tomcat中的配置文件server.xml中最后几行,标签</Host>前写一下代码

<Context path="指定虚拟目录" docBase="你开发的项目的webroot目录" />注意指定的虚拟目录以”/“开头。

重启tomcat,在IE中键入http://localhost/指定虚拟目录出现以下页面则表是配置成功,可以使用struts2进行开发了。


 

2        第一个web程序

2.1   新建web项目struts2

2.2导入包

1.commons-filefuload
2.commons-io
3.commons-lang
4.commons-logging
5.freemarker
6.javassist
7.ognl
8.struts2-core
9.xwork-core

 

2.3 配置文件web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 	<filter>
 		<filter-name>struts2</filter-name>
 		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>com.mycompany.myapp.actions</param-value>
        </init-param>
 	</filter>
 
 	<filter-mapping>
 		<filter-name>struts2</filter-name>
 		<url-pattern>/*</url-pattern>
 	</filter-mapping>
 	

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

2.4 编写action类RegisterAction.java,对注册页面registerResult.jsp请求的数据进行处理。

package com.Mycom.action;

 

import java.util.Date;

 

import com.sun.net.httpserver.Authenticator.Success;

 

publicclass RegisterAction {

    private String username;

    private String password;

    privateintage;

    private String sex;

    privatedoubleheight;

    private Date birthday;

    privatebooleanabove18;

    public String getUsername() {

       returnusername;

    }

    publicvoid setUsername(String username) {

       this.username = username;

    }

    public String getPassword() {

       returnpassword;

    }

    publicvoid setPassword(String password) {

       this.password = password;

    }

    publicint getAge() {

       returnage;

    }

    publicvoid setAge(int age) {

       this.age = age;

    }

    public String getSex() {

       returnsex;

    }

    publicvoid setSex(String sex) {

       this.sex = sex;

    }

    publicdouble getHeight() {

       returnheight;

    }

    publicvoid setHeight(double height) {

       this.height = height;

    }

    public Date getBirthday() {

       returnbirthday;

    }

    publicvoid setBirthday(Date birthday) {

       this.birthday = birthday;

    }

    publicboolean isAbove18() {

       returnabove18;

    }

    publicvoid setAbove18(boolean above18) {

       this.above18 = above18;

    }

   

    public String execute(){

      

       return"success";

    }

}

2.5 编写请求页面,registerResult.jsp文件,这里模拟的是一个注册页面。

 

  <body>

    <form action="typeConvertor.action">

    username:<input type="text" name="username"/><br/>

    password:<input type="password" name="password"/><br/>

    age:<input type="text" name="age" /><br/>

    sex:male<input type="radio" name="sex" value="male"/>

        female<input type="radio" name="set" vlaue="female" /><br/>

    height:<input type="text" name="height" ><br/>

    birthday:<input type="text" name="birthday"/><br/>

    isAbove18:YES<input type="radio" name="above18" value="true"/>

              NO<input type="radio" name="above18" vlaue="false"/><br/>

    submit:<input type="submit" value="提交"><br/>

    </form>

  </body>

</html>

 



2.6 action类处理后返回的结果页面typeConveter.jsp文件。

  <body>

    username:${requestScope.username }<br/

    password:${requestScope.password }<br/>

    age:${requestScope.age }<br/>

    sex:${requestScope.sex }<br/>

    height:${requestScope.height }<br/>

    birthday:${requestScope.birthday }<br/>

    isAbove18:${requestScope.above18 }<br/>

  </body>

</html>

2.6  在struts.xml文件中配置action类RegisterAction.java的action,映射视图资源。

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

    "http://struts.apache.org/dtds/struts-2.3.dtd">

 

<struts>

    <package name="dufault" extends="struts-default">

       <action name="typeConvertor"

class="com.Mycom.action.RegisterAction">

           <result name="success">registerResult.jsp</result>

           <result name="input">TypeConversion.jsp</result>

       </action>

    </package>

</struts>
2.7在IE中键入 http://localhost/struts2/typeConversion.js出现注册页面

填入数据enter,页面跳转到registerResult的页面,程序运行成功。


至此,成功的创建了第一个struts2 web 程序。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值