Struts2.5学习9-HTTP会话接口,Preparable,排除请求参数
Struts2.5学习9-HTTP会话接口
1.HTTP会话接口
Struts 2应用程序可能需要访问HTTP会话对象。Struts 2提供了一个接口SessionAware,您的Action类应实现该接口 来获取对HTTP会话对象的引用。
1.1修改HelloWorldAction类
1.实现SessionAware接口,并创建一个Map<String,Object> userSession代表Session,并重写setSession方法
2.写一个increaseHelloCount()方法,用于每次访问+1
3.在execute()中执行此方法
package com.zhou.action; import java.util.Map; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport implements SessionAware{ private String name; private Map<String, Object> userSession; private static final String HELLO_COUNT = "helloCount";public String execute() throws Exception{ increaseHelloCount(); return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void setSession(Map<String, Object> userSession) { this.userSession = userSession; } private void increaseHelloCount() { Integer helloCount = (Integer) userSession.get(HELLO_COUNT); if (helloCount == null ) { helloCount = 1; } else { helloCount++; } userSession.put(HELLO_COUNT, helloCount); } }
1.2修改helloworld.jsp页面
value="#session.helloCount"
从session会话中读取helloCount属性,这里的helloCount在Action类已经定义过
private static final String HELLO_COUNT = "helloCount";
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!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> <body> <h1><s:text name="greeting"/></h1> Hello World, <s:property value="name"/><br/> <p>I've said hello to you <s:property value="#session.helloCount" /> times!</p> </body> </body> </html>
1.3运行测试结果
1.4 安全漏洞
为了避免被他人获取session
使用SessionAware确实引入了一个潜在的安全漏洞
Map<String, Object> getSession在Action类中没有公共方法。您只需要一个公共void setSession方法即可实现该SessionAware接口。
还可以使Action类实现ParameterNameAware接口 并重写其acceptableParameterName方法:
public boolean acceptableParameterName(String parameterName) { boolean allowedParameterName = true ; if ( parameterName.contains("session") || parameterName.contains("request") ) { allowedParameterName = false ; } return allowedParameterName; }
除了让每个实现SessionAware的操作也实现ParameterNameAware接口,您可以告诉参数拦截器为包中的所有操作排除特定的请求属性。在struts.xml中,配置struts-default拦截器集,如下所示:
<package name="basicstruts2" extends="struts-default"> <interceptors> <interceptor-stack name="appDefault"> <interceptor-ref name="defaultStack"> <param name="exception.logEnabled">true</param> <param name="exception.logLevel">ERROR</param> <param name="params.excludeParams">dojo..*,^struts..*,^session..*,^request..*,^application..*,^servlet(Request|Response)..*,parameters...*</param> </interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="appDefault" /> ... </package>
上面的代码将确保实现“ SessionAware”接口的“ basicstruts2”包中的每个操作都将从params.excludeParams节点中提供的字符串开头的处理参数中排除。
2.Preparable Interface
如果某些参数验证失败了却还要跳转到画面,那么使用Preparable接口
实现此接口
让Action完成一些初始化的工作,这些初始化工作是放在Preparable接口的prepare()方法中的,该方法在execute()方法之前得到调用。
所以当参数没有,也要跳转的时候不用丢失参数
3.排除请求参数
<interceptors> <interceptor-stack name="appDefault"> <interceptor-ref name="defaultStack"> <param name="exception.logEnabled">true</param> <param name="exception.logLevel">ERROR</param> <param name="params.excludeParams">dojo..*,^struts..*,^session..*,^request..*,^application..*,^servlet(Request|Response)..*,parameters...*,submit</param> </interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="appDefault" />
node的值是用逗号分隔的正则表达式或简单字符串列表,这些列表标识不应由Parameters拦截器处理的请求参数。为了排除submit参数(这是上面表单代码中的提交按钮的名称),我刚刚添加submit到列表中。
请参阅此处描述的“ 基本拦截器堆栈”以查看要排除的参数名称/正则表达式的初始集合。确保复制已排除的参数列表,然后将自己的参数添加到末尾,以逗号分隔。
- 点赞
- 收藏
- 分享
- 文章举报
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- http接口请求参数签名工具类的实现和测试代码
- java Http中put请求传递参数调用接口
- C# HttpPost HttpGet 请求接口(Get为参数链 Post参数在请求体Body)
- 从HttpServletRequest读取请求参数转接到其它服务器接口的公共方法
- http接口开发请求参数签名实用工具类
- 项目背景: 1、接口URL:http://192.168.xx.xx:8080/mserver/rest/ms 2、接口参数:data=xxxxx&key=xxxxx,数据是加密的 3、请求方式
- HttpClient远程调用接口Post请求是所传参数为普通参数和Map
- HttpClient请求接口上传文件以及参数
- struts2学习笔记----------struts2.1.6接收中文请求参数乱码问题
- Struts学习_Jquery发送ajax请求以及datatype参数为text/JSON方式
- js中http请求参数与http协议
- Post方式发送http请求参数
- Jmeter学习(八)接口测试之二(设置参数)
- jdk HttpURLConnection类请求http接口工具类
- spring boot 常见http get ,post请求参数处理
- 关于struts2.1.6接收中文请求参数乱码问题
- [随记] 使用httpClient发送post请求连接restful接口
- 接口自动化测试之http请求实践
- Angularjs中$http以post请求通过消息体传递参数的实现方法