Struts2 (一)

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;
   }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值