您的位置:首页 > 编程语言 > Java开发

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

下面这个例子可以运行。





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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: