struts---最简单实例步骤

1.第一步web.xml 配置struts2的过滤器

<filter>
      <!-- 过滤器名字 -->
      <filter-name>struts2</filter-name>
      <!-- 过滤器支持的struts2类 -->
      <!-- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> -->
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      <!--  -->
</filter>
<filter-mapping>
      <!-- 过滤器拦截名字 -->
      <filter-name>struts2</filter-name>
      <!-- 过滤器拦截文件路径名字 -->
      <url-pattern>/*</url-pattern>
</filter-mapping>

 

2.根据配置的过滤器,中间键加载struts.xml文件

 <?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>
    <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    
    <!-- 设置动态调用方法 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />

    <!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts 2处理。    
    如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
    <constant name="struts.action.extension" value="action"/>
   
    <!-- 开发阶段设置 -->
    <!-- 设置浏览器是否缓存静态内容,默认值为true,开发阶段最好false -->
    <constant name="struts.serve.static.browserCache " value="false"/>

    <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,
    默认值为false,开发阶段最好true -->
    <constant name="struts.configuration.xml.reload" value="true"/>

    <!-- 开发模式下设为true,这样可以打印出更详细的错误信息 -->
    <constant name="struts.devMode" value="true"/>
    
    
    
    <include file="struts-user.xml" />
</struts>

 

3.第三步为每一个用户实例化一个action,并将表单数据映射到action属性中,跳转到相应的action进行处理

public class UserAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{

    private User user;
    private HttpServletRequest request;
    private HttpServletResponse response;
    
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }

    public String execute() throws Exception {
        System.out.println(user.getName());
        System.out.println(user.getPwd());
        return SUCCESS;
    }

    public String login() throws Exception {
        
        return SUCCESS;
    }

    

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
        
    }

    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
        
    }
    
    
    
    
    public HttpServletRequest getRequest() {
        return request;
    }
    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }
    public HttpServletResponse getResponse() {
        return response;
    }
    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }


}

 

4.第四步返回result,Struts2设置attribute属性并转发页面

<?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="user" namespace="/user" extends="struts-default">
           <action name="userAction" method="execute"
               class="com.seecen.action.UserAction">
               <result name="success">/index.jsp</result>
               <result name="input">/input.jsp</result>
           </action>
   </package>

</struts>

 

posted @ 2015-01-19 16:38  江湖一笑  阅读(140)  评论(0编辑  收藏  举报