struts2中的session到底是个什么东西
2013-06-07 18:55
393 查看
what the hell is Map session in Struts2. Anything to do with jsp session?
提示空值java.lang.NullPointerException
When I get the session map using sessionaware interface, I get an empty map
下面这个例子可以运行。
注意#session.在action中的变量是userSession
原文:http://www.itinpractice.com/tutorials/403/implementing-sessionaware-interface.html#.UbGOZvkweX8
另外一个用到SessionAware的例子:/article/8209495.html
而在这里,session就是空值:
源代码:http://pan.baidu.com/share/link?shareid=3546835572&uk=3878681452
提示空值java.lang.NullPointerException
When I get the session map using sessionaware interface, I get an empty map
下面这个例子可以运行。
package com.itinpractice.struts2.action; import java.util.Map; import org.apache.struts2.interceptor.SessionAware; import com.itinpractice.struts2.model.MessageBean; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.interceptor.ParameterNameAware; public class HelloWorldAction extends ActionSupport implements SessionAware, ParameterNameAware{ private static final long serialVersionUID = 1L; private Map<String, Object> userSession ; private MessageBean messageBean; private String userName; private static final String HELLO_COUNT = "helloCount"; @Override public String execute() throws Exception { messageBean = new MessageBean(); messageBean.setMessage("Hello, How are you?" + " " + userName); System.out.println(userName); increaseHelloCount(); return SUCCESS; } /** * Increase the value of HELLO_COUNT stored in the user's HTTP session. */ private void increaseHelloCount() { //Using the HTTP Session Object In The Action Class Integer helloCount = (Integer) userSession.get(HELLO_COUNT); if (helloCount == null ) { helloCount = 1; } else { helloCount++; } userSession.put(HELLO_COUNT, helloCount); } public MessageBean getMessageBean() { return messageBean; } public void setMessageBean(MessageBean messageBean) { this.messageBean = messageBean; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } /** * This method will be called by the Struts 2 framework * for each parameter in the request scope. * By returning false if the parameter name contains "session" * we are telling the Struts 2 framework to ignore that parameter. * This will prevent a malicious user from trying to hack the HTTP session object. */ public boolean acceptableParameterName(String parameterName) { boolean allowedParameterName = true ; System.out.println("parameterName = " + parameterName); if ( parameterName.contains("session") || parameterName.contains("request") ) { allowedParameterName = false ; } return allowedParameterName; } /** * Do not have a public Map<String, Object) getSession method in the Action class. * You only need a public void setSession method to implement the SessionAware interface. */ public void setSession(Map<String, Object> session) { userSession = session ; System.out.println("injected NiMaBi map 对象"); } }
<body> <h3>You've said hello <s:property value="#session.helloCount"/> times.</h3> <h3><s:property value="messageBean.message"/> </h3> </body>
注意#session.在action中的变量是userSession
原文:http://www.itinpractice.com/tutorials/403/implementing-sessionaware-interface.html#.UbGOZvkweX8
另外一个用到SessionAware的例子:/article/8209495.html
而在这里,session就是空值:
源代码:http://pan.baidu.com/share/link?shareid=3546835572&uk=3878681452
相关文章推荐
- 硬盘的前512字节到底放了些什么东西?
- 模块识别地址到底是个什么东西
- 拷贝构造函数到底是个是什么东西?
- delegate和protocol(协议与代理到底是个什么东西?)
- 合生元到底是什么东西?
- JavaBean到底是个什么东西
- Python到底是个什么东西
- 专用播放器到底是些什么东西?
- JavaBean到底是个什么东西
- request.getSession(false)到底返回什么
- session和application到底有什么区别呢?请诸位打虾指点下。谢谢。。
- HttpModule到底是个什么东西
- struts2的ActionContext到底是一个什么对象?
- session的到底是做什么的?
- JavaBean到底是个什么东西
- 产品的本质,到底是个什么东西?
- 【转载】知识普及:天煞的HTML5到底是个什么东西
- 华为虚拟化到底是个什么东西?