zoukankan      html  css  js  c++  java
  • (三)Struts2 拦截器

    所有的学习我们必须先搭建好Struts2的环境(1、导入对应的jar包,2、web.xml,3、struts.xml)

    第一节:拦截器简介 

        (百度百科Struts2)

        Struts2 拦截器是在访问某个Action 或Action 的某个方法,字段之前或之后实施拦截,并且Struts2 拦截器是可
        插拔的,拦截器是AOP的一种实现.
        优点:通用功能的封装,提供了可重用性;

    第二节:Struts2 预定义拦截器&拦截器栈

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
     4     "http://struts.apache.org/dtds/struts-2.0.dtd">
     5 
     6 <struts>
     7 
     8   <package name="manage" namespace="/" extends="struts-default">
     9       
    10       <interceptors>
    11           <interceptor name="myInterceptor" class="com.wishwzp.interceptor.MyInterceptor"></interceptor>
    12       </interceptors>
    13       
    14       <action name="hello" class="com.wishwzp.action.HelloAction">
    15           <result name="success">success.jsp</result>
    16           
    17           <interceptor-ref name="myInterceptor"></interceptor-ref>
    18           <interceptor-ref name="defaultStack"></interceptor-ref>
    19       </action>
    20   </package>
    21 
    22 </struts>
    struts.xml
     1 package com.wishwzp.interceptor;
     2 
     3 import com.opensymphony.xwork2.ActionInvocation;
     4 import com.opensymphony.xwork2.interceptor.Interceptor;
     5 
     6 public class MyInterceptor implements Interceptor{
     7 
     8     @Override
     9     public void destroy() {
    10         // TODO Auto-generated method stub
    11         System.out.println("MyInterceptor销毁");
    12     }
    13 
    14     @Override
    15     public void init() {
    16         // TODO Auto-generated method stub
    17         System.out.println("MyInterceptor初始化");
    18     }
    19 
    20     @Override
    21     public String intercept(ActionInvocation invocation) throws Exception {
    22         System.out.println("在Action执行之前");
    23         String result=invocation.invoke();
    24         System.out.println("result:"+result);
    25         System.out.println("在Action执行之后");
    26         return result;
    27     }
    28 
    29 }
    MyInterceptor.java
     1 package com.wishwzp.action;
     2 
     3 import com.opensymphony.xwork2.ActionSupport;
     4 
     5 public class HelloAction extends ActionSupport{
     6 
     7     /**
     8      * 
     9      */
    10     private static final long serialVersionUID = 1L;
    11     
    12     private String name;
    13     
    14     
    15 
    16     public String getName() {
    17         return name;
    18     }
    19 
    20     public void setName(String name) {
    21         this.name = name;
    22     }
    23 
    24     @Override
    25     public String execute() throws Exception {
    26         System.out.println("执行了HelloAction的默认方法");
    27         return SUCCESS;
    28     }
    29     
    30     
    31 }
    HelloAction.java
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 Name:${name }
    11 </body>
    12 </html>
    success.jsp

    url:http://localhost:8080/Struts2Chap03/hello?name=Struts2

    我们需要注意的是在MyInterceptor.java中的:

    String result=invocation.invoke();

    第三节:自定义拦截器简单实例

    我们在第二节中“MyInterceptor.java”中已经是自己定义的拦截器了

    第四节:自定义拦截器-登录验证拦截器

     struts.xml
    1
    <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 <package name="manage" namespace="/" extends="struts-default"> 9 10 <interceptors> 11 <interceptor name="loginInterceptor" class="com.wishwzp.interceptor.LoginInterceptor"></interceptor> 12 </interceptors> 13 14 <action name="user" class="com.wishwzp.action.UserAction"> 15 <result name="success">success.jsp</result> 16 <result name="error">error.jsp</result> 17 <interceptor-ref name="defaultStack"></interceptor-ref> 18 </action> 19 20 <action name="gril" class="com.wishwzp.action.GrilAction"> 21 <result name="success">success.jsp</result> 22 <result name="error">error.jsp</result> 23 <interceptor-ref name="loginInterceptor"></interceptor-ref> 24 <interceptor-ref name="defaultStack"></interceptor-ref> 25 </action> 26 </package> 27 28 </struts>
     User.java
    1
    package com.wishwzp.model; 2 3 public class User { 4 5 private String userName; 6 private String password; 7 8 public String getUserName() { 9 return userName; 10 } 11 public void setUserName(String userName) { 12 this.userName = userName; 13 } 14 public String getPassword() { 15 return password; 16 } 17 public void setPassword(String password) { 18 this.password = password; 19 } 20 }
     UserService.java
    1
    package com.wishwzp.service; 2 3 import com.wishwzp.model.User; 4 5 public class UserService { 6 7 public boolean login(User user){ 8 if("wishwzp".equals(user.getUserName())&&"123".equals(user.getPassword())){ 9 return true; 10 }else{ 11 return false; 12 } 13 } 14 }
     UserAction.java
    1
    package com.wishwzp.action; 2 3 import java.util.Map; 4 5 import com.wishwzp.model.User; 6 import com.wishwzp.service.UserService; 7 import com.opensymphony.xwork2.ActionContext; 8 import com.opensymphony.xwork2.ActionSupport; 9 10 public class UserAction extends ActionSupport{ 11 12 /** 13 * 14 */ 15 private static final long serialVersionUID = 1L; 16 17 private UserService userService=new UserService(); 18 private User user; 19 private String error; 20 21 public User getUser() { 22 return user; 23 } 24 25 public void setUser(User user) { 26 this.user = user; 27 } 28 29 public String getError() { 30 return error; 31 } 32 33 public void setError(String error) { 34 this.error = error; 35 } 36 37 @Override 38 public String execute() throws Exception { 39 if(userService.login(user)){ 40 ActionContext actionContext=ActionContext.getContext();//获取上下文,去获取actionContext 41 Map<String, Object> session=actionContext.getSession();//通过actionContext去获取session 42 session.put("currentUser", user);//将用户存放在session中,就是放到了currentUser 43 return SUCCESS;//返回到SUCCESS 44 }else{ 45 this.error="用户名或密码错误"; 46 return "error"; 47 } 48 } 49 50 51 }
     GrilAction.java
    1
    package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class GrilAction extends ActionSupport{ 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 1L; 11 @Override 12 public String execute() throws Exception { 13 System.out.println("看美女"); 14 return SUCCESS; 15 } 16 17 18 }
     LoginInterceptor.java
    1
    package com.wishwzp.interceptor; 2 3 import java.util.Map; 4 5 import javax.servlet.http.HttpServletRequest; 6 7 import org.apache.struts2.ServletActionContext; 8 9 import com.opensymphony.xwork2.ActionContext; 10 import com.opensymphony.xwork2.ActionInvocation; 11 import com.opensymphony.xwork2.interceptor.Interceptor; 12 13 public class LoginInterceptor implements Interceptor{ 14 15 @Override 16 public void destroy() { 17 // TODO Auto-generated method stub 18 System.out.println("LoginInterceptor销毁"); 19 } 20 21 @Override 22 public void init() { 23 // TODO Auto-generated method stub 24 System.out.println("LoginInterceptor初始化"); 25 } 26 27 @Override 28 public String intercept(ActionInvocation invocation) throws Exception { 29 System.out.println("在Action执行之前"); 30 ActionContext actionContext=invocation.getInvocationContext();//获取actionContext,这样就可以获取session了 31 Map<String, Object> session=actionContext.getSession();//获取session 32 Object currentUser=session.get("currentUser");//获取前面的的用户有没有 33 String result=null; 34 if(currentUser!=null){ 35 result=invocation.invoke();//不是空的话,说明已经登录过了 36 }else{//否则没有登录 37 //获取request 38 HttpServletRequest request=(HttpServletRequest)invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST); 39 request.setAttribute("error", "请先登录!"); 40 result="error"; 41 } 42 System.out.println("result:"+result); 43 System.out.println("在Action执行之后"); 44 return result; 45 } 46 47 }
     login.jsp
    1
    <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="user" method="post"> 11 用户名:<input type="text" name="user.userName"/> 12 密码:<input type="password" name="user.password"/> 13 <input type="submit" value="登录"/> 14 </form> 15 </body> 16 </html>
     success.jsp
    1
    <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 当前用户:${currentUser.userName } 11 </body> 12 </html>
     error.jsp
    1
    <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 错误信息:${error } <a href="login.jsp">登录</a> 11 </body> 12 </html>

    url:http://localhost:8080/Struts2Chap03/login.jsp

    url:http://localhost:8080/Struts2Chap03/gril


    我们再上面的代码中优化一下strut.xml。

    其他什么地方都不变

     struts.xml
    1
    <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 9 <package name="manage" namespace="/" extends="struts-default"> 10 11 <interceptors> 12 <!-- 配置loginInterceptor拦截器 --> 13 <interceptor name="loginInterceptor" class="com.wishwzp.interceptor.LoginInterceptor"></interceptor> 14 15 <!-- 拦截器栈 --> 16 <!-- 配置一个名为myStack的拦截器栈,里面有拦截器loginInterceptor和默认的拦截器 --> 17 <interceptor-stack name="myStack"> 18 <interceptor-ref name="loginInterceptor"></interceptor-ref> 19 <interceptor-ref name="defaultStack"></interceptor-ref> 20 </interceptor-stack> 21 </interceptors> 22 23 <!-- 配置使用自己配置的默认拦截器myStack --> 24 <default-interceptor-ref name="myStack"></default-interceptor-ref> 25 26 <!-- 使用全局错误信息,就不需要去一个个写error了 ,所有的action都会找到这个全局error的--> 27 <global-results> 28 <result name="error">error.jsp</result> 29 </global-results> 30 31 <action name="user" class="com.wishwzp.action.UserAction"> 32 <result name="success">success.jsp</result> 33 34 <!-- 这个默认拦截器栈这里必须有,不然的话这里的登录也被拦截掉了 --> 35 <interceptor-ref name="defaultStack"></interceptor-ref> 36 </action> 37 38 <action name="gril" class="com.wishwzp.action.GrilAction"> 39 <result name="success">success.jsp</result> 40 </action> 41 </package> 42 43 </struts>
  • 相关阅读:
    grunt in webstorm
    10+ Best Responsive HTML5 AngularJS Templates
    响应式布局
    responsive grid
    responsive layout
    js event bubble and capturing
    Understanding Service Types
    To add private variable to this Javascript literal object
    Centering HTML elements larger than their parents
    java5 新特性
  • 原文地址:https://www.cnblogs.com/wishwzp/p/5470955.html
Copyright © 2011-2022 走看看