好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java中的Struts2框架拦截器之实例代码

本文实例为大家分享了Struts2框架拦截器实例的示例代码,供大家参考,具体内容如下

在看拦截器的小例子的前我们先来看看sturts2的原理

 

struts2自己是有拦截器的,通过拦截器可以拦截用户请求,并作出处理

拦截器作用有很多,譬如:

1.Action里面有个属性,这个属性我想在action执行之前改成别的值,可以用拦截器解决。

2.比如每个人执行action之前,我可以查看他们有没有这个权限执行这个action。
如果不设置拦截器,你要在每种action方法之前设置判定程序,非常繁琐。

拦截器interceptor体现了一种编程理念,叫做AOP(面向切面编程)

实例1:使用token拦截器控制重复提交

token是用来解决下面的问题:

一旦有人通过表单提交数据,在提交表单的时候页面提交速度太慢,用户一直不停的刷新,如果不做一种机制防止他刷新的话,那么数据库中就会多出好多垃圾数据。

表单提交一般都要写成post(第一种解决方式,浏览器会提醒你是否重复提交)

拦截器解决方法:

struts2定义了一个拦截器(interceptor)叫--token

token的意思是[令牌],你要提交数据,我先发给你一个令牌,你的令牌要是和我能对上,你就提交,对不上就不允许提交

token为什么可以防止重复提交?

答:当访问界面时,在服务器那边的session里面,生成一个随机数,然后再把随机数写到form里,提交数据时session就会被带到服务器去。提交完成后session里面的值被清空,再次重复提交的时候,发现此token值在session不存在,说明已经被提交过了,这个时候就会显示友好界面提示用户。

实现代码:

struts.xml:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

< package name = "test" namespace = "/javaee" extends = "struts-default" >

  < action name = "pinput" class = "cn.edu.hpu.action.PinputAction" >

  < result >/input.jsp</ result >

  </ action >

 

  < action name = "person" class = "cn.edu.hpu.action.PersonAction" >

  < result >/addOK.jsp</ result >

 

  < interceptor-ref name = "defaultStack" ></ interceptor-ref >

  < interceptor-ref name = "token" ></ interceptor-ref >

  < result name = "invalid.token" >/error.jsp</ result >

  </ action >

</ package >

PersonAction.java:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

package cn.edu.hpu.action;

import com.opensymphony.xwork2.ActionSupport;

public class PersonAction extends ActionSupport {

  private String name;

  private int age;

 

  @Override

  public String execute() throws Exception {

  System.out.println( "a person added!" );

  return super .execute();

  }

  public String getName() {

  return name;

  }

 

  public void setName(String name) {

  this .name = name;

  }

 

  public int getAge() {

  return age;

  }

 

  public void setAge( int age) {

  this .age = age;

  }

}

input.jsp:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%

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%>" rel="external nofollow" >

  < title >My JSP 'input.jsp' starting page</ title >

  </ head >

 

  < body >

  < form action="<%=basePath %>javaee/person" method="post">

  name:< input name = "name" >

  age:< input name = "age" >

  < input type = "submit" value = "add" >

  </ form >< br />

  </ body >

</ html >

addOK.jsp:

?

1

2

3

4

5

6

7

8

9

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

< html >

  < head >

  < title >My JSP 'addOK.jsp' starting page</ title >

  </ head >

  < body >

  add ok!! < br />

  </ body >

</ html >

error.jsp:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<%@ page language= "java" import= "java.util.*" pageEncoding= "UTF-8" %>

<%

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 'error.jsp' starting page</title>

  </head>

  <body>

  严禁做重复的事!!! <br>

  </body>

</html>

结果:

填写name与age之后,会跳入界面addOK.jsp,控制台会输出a person added!

返回再次提交时,就会跳转到error.jsp界面,无法重复提交

如果在表单中加<s:token></s:token>,则会看到源码:

?

1

2

3

< input type = "hidden" name = "struts.token.name" value = "struts.token" />

< input type = "hidden" name = "struts.token" value = "PZOQNKARYVQYDEVGNKTWFBF17735K6AI" />

<!--相当于生成了一个随机数-->

所原理是:在提交页面形成了一个token,这个token在服务器端对应的session里面已经有了,当我一点提交的时候,由于加了 <interceptor-ref name="token"></interceptor-ref>( token的拦截器),服务器就会帮我拦截,看看session里面有没有token的值,如果之前没有提交,session里面是有这个token值的,如果上次提交过了,session就会将token值清除掉。当发现页面的token值在服务器的session中找不到时,服务器发现出错了,重定向到error.jsp,显示错误信息

实例2:自定义拦截器

struts.xml:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

< pre name = "code" class = "html" >< pre name = "code" class = "html" >< pre name = "code" class = "html" >< pre name = "code" class = "html" ><? xml version = "1.0" encoding = "GBK" ?>

<!--指定struts2配置文件的DTD信息-->

<!DOCTYPE struts PUBLIC

"-//apache Software Foundation//DTD Struts Configuation 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<!-- struts 是struts2配置文件的根元素-->

 

< struts >

< constant name = "struts.devMode" value = "true" ></ constant >

< constant name = "struts.i18n.encoding" value = "UTF-8" ></ constant >

<!--允许静态方法的执行-->

< constant name = "struts.ognl.allowStaticMethodAccess" value = "true" ></ constant >

 

< package name = "test" namespace = "/" extends = "struts-default" >

  < interceptors >

  < interceptor name = "my" class = "cn.edu.hpu.interceptor.MyInterceptor" ></ interceptor >

  </ interceptors >

 

 

  < action name = "test" class = "cn.edu.hpu.action.TestAction" >

  < result >/test.jsp</ result >

  < interceptor-ref name = "my" ></ interceptor-ref >

  < interceptor-ref name = "defaultStack" ></ interceptor-ref >

  </ action >

</ package >

</ struts >

 TestAction.java:

?

1

2

3

4

5

6

7

8

9

10

package cn.edu.hpu.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport{

 

  @Override

  public String execute() throws Exception {

   // TODO Auto-generated method stub

   return super .execute();

  }

}

MyInterceptor.java:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

package cn.edu.hpu.interceptor;

 

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;

 

public class MyInterceptor implements Interceptor{

 

 

  public void destroy() {

 

  }

  public void init() {

 

  }

 

  //写好了一个拦截(计算了一个action运行的时间)

  public String intercept(ActionInvocation invocation) throws Exception {

  long start=System.currentTimeMillis();

  String r=invocation.invoke();

  long end=System.currentTimeMillis();

  System.out.println( "Action Time=" +(end-start));

  return r;

  }

 

 

}

访问:http://localhost:8080/struts2_LanJieQi/test后
控制台输出:
Action Time=200

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

原文链接:https://blog.csdn.net/acmman/article/details/47111009

查看更多关于Java中的Struts2框架拦截器之实例代码的详细内容...

  阅读:14次