Struts2 (一)

来源:互联网 发布:淘宝怎么清仓 编辑:程序博客网 时间:2024/05/21 20:27

1.配置struts2 过滤器

<filter>    <filter-name>Struts2</filter-name>    <!--           Struts 2.1.3之后该类就已经被废弃了 替代的类是:         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter     -->      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</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>    <!--如果需要使用struts2默认xml中特性 就必须继承struts-default          namespace定义action上层路径 比如namespace定义为 /test action定义为 login                       访问路径为  /test/login.action            -->   <package name="hellowPakage" namespace="/" extends="struts-default">       <!-- struts2的action定义 name为action的路径 class为对应的处理类                           如果不定义method属性 会自动进入execute方法 如果定义了方法调转到定义的方法        -->       <action name="login" class="com.etop.struts2.action.StrutsAction" >           <result name="success">/suc.jsp</result>       </action>       <action name="resetFom" class="com.etop.struts2.action.StrutsAction" method="resetForm" >            <!--                result用于控制action的跳转  name为action返回的名称               type常用的值 : chain,dispatcher,redirect,redirectAction,stream               type 是转发的类型  dispatcher:请求转发,                                chain 请求转发 跳转action                                redirect请求重定向                                redirectAction 请求重定向到action                                stream 文件下载            -->           <result name="success" type="redirect">/login.jsp</result>       </action>   </package></struts>

3.编写Action

package com.etop.struts2.action;import com.opensymphony.xwork2.ActionSupport;/** * struts2可以是任意一个pojo类 不需要继承任何对象\ * 如果需要使用struts2的高级特性 就必须继承 ActionSupport * @author teacher * */public class StrutsAction extends ActionSupport{   private String ruserName;   private String rpassword;   /**    * Struts2方法结构 返回值必须是String类型 不需要任何 参数    * 返回参数 决定需要跳转的jsp    * 必须在struts.xml定义result    * execute是ActionSupport 默认的执行方法     * @return    */   public String execute(){       return "success";   }   public String resetForm(){       return "success";   }   public String getRuserName() {       return ruserName;   }   public void setRuserName(String ruserName) {       this.ruserName = ruserName;   }   public String getRpassword() {       return rpassword;   }   public void setRpassword(String rpassword) {       this.rpassword = rpassword;   }}
0 0
原创粉丝点击