温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Struts2框架拦截器怎么在Java中使用

发布时间:2021-01-16 10:20:47 来源:亿速云 阅读:123 作者:Leah 栏目:移动开发

这篇文章将为大家详细讲解有关Struts2框架拦截器怎么在Java中使用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

具体内容如下

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

 Struts2框架拦截器怎么在Java中使用

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:

<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:

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:

<%@ 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:

<!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:

<%@ 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>,则会看到源码:

<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:

<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:

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:

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

关于Struts2框架拦截器怎么在Java中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI