Struts2实现第一个简单拦截器

来源:互联网 发布:淘宝订购的应用 编辑:程序博客网 时间:2024/06/05 06:04

 1:jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'reg.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
    <h2>拦截器初步使用(进行离开拦截后验证)</h2><hr/>
      <s:form action="helloaction" method="post">
            <s:textfield name="username" label="用户名"></s:textfield>
          <s:password name="pass1" label="密码"></s:password>
         <s:password name="pass2" label="确认密码"></s:password>
            <s:submit value="注册"></s:submit>
      </s:form>
            
  </body>
</html>

 

  2:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>StrutsTest1</display-name>
  <!-- 配置struts2的过滤器 -->  
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
           <param-name>struts.i18n.encoding</param-name>
           <param-value>UTF-8</param-value>
        </init-param>
    </filter>


    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  <welcome-file-list>
    <welcome-file>welcome.jsp</welcome-file>
  </welcome-file-list>
</web-app>


  3: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>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <package name="struts2login" extends="struts-default">  
        <action name="login" class="com.test.action.LoginAction">  
            <result name="success" >/result.jsp</result>  
            <result name="error">/error.jsp</result>  
        </action>    
        <action name="input" class="com.test.action.InputAction">  
            <result name="success" >/success.jsp</result>  
            <result name="error">/error.jsp</result>  
        </action> 
        <action name="message" class="com.test.action.InputAction">  
            <result name="success" >/message.jsp</result>  
            <result name="error">/error.jsp</result>  
        </action>
       </package>   
         <package name="demo1" extends="struts-default">
           <action name="addRegister" class="com.test.action.RegisterAction">
              <result name="success">/showRegInfo.jsp</result>
           </action>
            <action name="addRegister1" class="com.test.action.Register1Action">
              <result name="success">/showRegInfo1.jsp</result>
           </action>
           <action name="convert" class="com.test.action.ConvertAction">  
           <result name="success">/convertResult.jsp</result>  
    </action>  
         </package>
         <package name="hellointerceptor" extends="struts-default">
                <interceptors>
                   <interceptor name="helloInterceptor" class="com.test.action.HelloInterceptor">
                   </interceptor>
                </interceptors>
                <action name="helloaction" class="com.test.action.HelloAction">
                     <result name="success">/success1.jsp</result>
                     <result name="input1">/input1.jsp</result>
                      <interceptor-ref name="defaultStack"></interceptor-ref>
                      <interceptor-ref name="helloInterceptor"></interceptor-ref>
                </action>
         </package>
    
</struts>


  5;拦截器进行拦截

package com.test.action;


import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;


public class HelloInterceptor extends AbstractInterceptor {


private static final long serialVersionUID = 1L;


@Override
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("***拦截器开始执行***");
String resultString=arg0.invoke();
System.out.println("***拦截器执行完毕***");
return resultString;
}


}


 6:Action

package com.test.action;


import com.opensymphony.xwork2.ActionSupport;


public class HelloAction extends ActionSupport{


private static final long serialVersionUID = 1L;
     private String username;
     private String pass1;
     private String pass2;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPass1() {
return pass1;
}
public void setPass1(String pass1) {
this.pass1 = pass1;
}
public String getPass2() {
return pass2;
}
public void setPass2(String pass2) {
this.pass2 = pass2;
}
     public String execute() throws Exception
     {
    if((username!=null&&username!="")&&(pass1.equals(pass2)))
    {
    System.out.println("***验证正确,正在执行Action()方法***");
    return "success";
    }
    else
    {
    System.out.println("***验证错误,正在执行Action()方法***");
    return "input";
    }
     }
}


  7:jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success1.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
  </head>
  <body>
    <h2>用户名:<s:property value="username"/></h2>
    <h2>密&nbsp;码:<s:property value="pass1"/></h2>
  </body>
</html>

0 0
原创粉丝点击