struts2中的session到底是个什么东西

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的例子:http://blog.csdn.net/weiyanghuadi/article/details/8931820

而在这里,session就是空值:

源代码:http://pan.baidu.com/share/link?shareid=3546835572&uk=3878681452

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值