拦截器,避免重复提交
来源:互联网 发布:美国风投公司排名矩阵 编辑:程序博客网 时间:2024/06/05 04:15
一、如何分离字符串
使用StringTokenizer:
String flower = "水仙 玫瑰 百合";
StringTokenizer tokenizer = new StringTokenizer(flower);
while(tokenizer.hasMoreTokens()){
System.out.println(tokenizer.nextToken());
}//默认是以空格分割
//分割不考虑正则
String flower1 = "水仙*玫瑰*百合";
StringTokenizer tokenizer1 = new StringTokenizer(flower1,"*");
while(tokenizer1.hasMoreTokens()){
System.out.println(tokenizer1.nextToken());
}//使用*分割字符串
二、向存储范围中添加数据或访问数据
Action中的数据要显示在视图上,通常将数据保存到4个范围中,然后通过EL表达式显示;
Struts 2提供了一个:1——ActionContext的API,通过这个API的静态方法getContext( )可以向四个范围中添加数据。
2——ServletActionContext的API,通过这个API可以获取request,session,application,response对象; ServletActionContext直接得到原生的内置对象;
Action中访问或添加request,session,application属性:
/**
* @功能:ActionContext应用
*/
public String one(){
//得到ActionContext对象
ActionContext ac = ActionContext.getContext();
//ActionContext也可以得到原生的request
HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE);
System.out.println("sessionId==="+request.getSession().getId());
//向request赛值
ac.put("k.1", "石板桥上跑马------不留痕迹");
//向session中赛值
ac.getSession().put("k.2", "猴子吃大象------- 亏他张得开嘴");
//向application中塞值
ac.getApplication().put("k.3", "呆子不识走马灯 ------- 来的来,去的去");
return "success";
}
在Action中获取HttpServletRequest、HttpSession、ServletContext、HTTPServletResponse对象
/**
* @功能:servletActionContext
*/
public String two(){
//ServletActionContext获取的示内置对象
HttpServletRequest request = ServletActionContext.getRequest();
ServletContext application = ServletActionContext.getServletContext();
HttpSession session = ServletActionContext.getRequest().getSession();
HttpServletResponse response = ServletActionContext.getResponse();
//request
request.setAttribute("k.1", "瞎子坐飞机----不知高低");
//session
//ServletActionContext.getRequest().getSession().setAttribute("k.2", "晚点的火车-------赶得上");
session.setAttribute("k.2", "晚点的火车-------赶得上");
//application
//ServletActionContext.getServletContext().setAttribute("k.3","六月的山----清一色");
application.setAttribute("k.3","六月的山----清一色");
return "success";
}
使用ServletActionContext向存储范围中添加获取数据,类要继承ServletRequestAware,ServletResponseAware,SessionAware,ApplicationAware接口,重写里面的方法
public class ClearAction implements ServletRequestAware,ServletResponseAware,SessionAware,ApplicationAware{
//定义四个范围对象
private HttpServletRequest request;
private HttpServletResponse response;
private Map<String, Object> sessionMap;
private Map<String, Object> applicationMap;
public String three(){
request.setAttribute("k.1", "灯影子打店-----人旺财不旺");
sessionMap.put("k.2", "猪苦胆泡黄连------苦上加苦");
applicationMap.put("k.3", "孙悟空听见紧箍咒------头痛");
return "success";
}
@Override
public void setApplication(Map<String, Object> arg0) {
this.applicationMap=arg0;
}
@Override
public void setSession(Map<String, Object> arg0) {
this.sessionMap=arg0;
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
this.response=arg0;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
this.request = arg0;
}
}
三、拦截器
定义拦截器步骤:
1:定义页面:xxx.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
session.setAttribute("user", "您已登录");
%>>
<h1>您已登录</h1>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
session.invalidate();//销毁session
%>
<h2>您已退出</h2>
2.定义拦截类
拦截器必须实现com.opensymphony.xwork2.interceptor接口,并实现接口中的 public String intercept(ActionInvocation invocation)方法,
方法参数invocation调用invoke( )方法,表示继续调用目标Action,方法的返回值是目标Action返回的视图;
/**
* @功能:拦截器类
*/
public class LoginInteception implements Interceptor {
@Override
public void destroy() {
}
@Override
public void init() {
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
if(ActionContext.getContext().getSession().get("user")!=null){
//已经登录
//执行下一个拦截器或返回指定action
return invocation.invoke();
}
//没有授权,拦截器拦住
ActionContext.getContext().getSession().put("message", "您没有登录,请先登录");
return "login";
}
}
3.定义Action
public class UserAction extends ActionSupport {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute(){
this.message="您已登录,有权操作";
return "success";
}
}
4.设置配置文件struts.xml
<!-- 配置拦截器 -->
<package name="userpkg" extends="struts-default" namespace="/interceptor">
<!-- 定义拦截器类 -->
<interceptors>
<interceptor name="quanxian" class="org.ycpower.inteceptor.LoginInteception"></interceptor>
<!-- 定义拦截器栈 -->
<interceptor-stack name="mystack">
<!--defaultStack是系统拦截器栈,定义在struts-default.xml,必须将系统的拦截器类定义在上面-->
<interceptor-ref name="defaultStack" />
<interceptor-ref name="timer" />
<!--必须把自己的拦截器定义在下面-->
<interceptor-ref name="quanxian" />
</interceptor-stack>
</interceptors>
<!-- 整个包中所有的action都是用mystack拦截器栈 -->
<default-interceptor-ref name="mystack"></default-interceptor-ref>
<global-results>
<result name="login">/interceptor/main.jsp</result>
</global-results>
<action name="main" class="org.ycpower.actions.UserAction">
<!-- 本action内应用拦截器 -->
<interceptor-ref name="mystack" />
<result>/interceptor/main.jsp</result>
</action>
</package>
四、避免重复提交表单
1——Action类必须继承ActionSupport
public class RegisterAction extends ActionSupport implements ModelDriven<UserModel> {
UserModel user = new UserModel();
@Override
public UserModel getModel() {
return user;
}
public UserModel getUser() {
return user;
}
public void setUser(UserModel user) {
this.user = user;
}
public String execute(){
System.out.println("注册。。。。。。。。。。。。");
return "success";
}
}
2.页面是用struts标签
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="suc.action" method="post">
姓名:<input type="text" name="username"/>
性别:<input type="radio" name="sex" value="男" />男
<input type="radio" name="sex" value="女"/>女
爱好:<input type="checkbox" name="hobby"/>打篮球
<input type="checkbox" name="hobby"/>跳舞
<input type="checkbox" name="hobby"/>唱歌
籍贯:<select name="province">
<option value="甘肃">甘肃省</option>
<option value="甘肃">山西省</option>
<option value="甘肃">湖南省</option>
<option value="甘肃">浙江省</option>
</select>
<s:token />
<s:submit value="注册" />
</s:form>
</body>
</html>
3.配置struts.xml
<package name="regpkg" extends="struts-default">
<!-- 配置拦截器 -->
<interceptors>
<!-- 配置拦截器栈 -->
<interceptor-stack name="repeatstack" >
<interceptor-ref name="defaultStack" />
<interceptor-ref name="token" />
</interceptor-stack>
</interceptors>
<!-- 解决表单提交重复 -->
<action name="suc" class="org.ycpower.actions.RegisterAction">
<interceptor-ref name="repeatstack" />
<result name="invalid.token" type="redirect">/volide/register.jsp</result>
<result>/volide/ok.jsp</result>
</action>
</package>
0 0
- 拦截器,避免重复提交
- Struts2中㡰自定义数据转换器,自定义拦截器,避免表单重复提交的总结
- struts2避免重复提交
- 避免表单重复提交
- 避免表单重复提交
- struts2避免重复提交
- 避免表单重复提交
- 如何避免重复提交
- struts避免重复提交
- struts2避免重复提交
- 避免表单重复提交
- Android 避免重复提交
- 避免表单重复提交
- 使用拦截器防止表单重复提交
- Struts拦截器解决表单重复提交
- Struts2中的拦截器防止重复提交
- Struts2拦截器token防止重复提交
- stucts2 表单重复提交拦截器 token
- C语言中的__FILE__、__LINE__和#line
- 让初入职场者少走弯路的14个忠告
- php-fpm 启动参数及重要配置详解
- web前端性能优化总结
- 传智播客:为什么未来是全栈工程师的世界?
- 拦截器,避免重复提交
- Linux查看日志常用命令
- lynx
- vbs教程《模拟按键》
- 编译在路由器上可以运行的FRP
- Java随机数
- Java类和类成员、方法的访问权限
- 3.java面向对象语法学习(部分2-抽象类,接口,内部类)
- 在hibernate框架中配置显示sql语句