日期:2014-05-18  浏览次数:20779 次

session在struts中的问题
这是struts的一段配置文件
<action name="loginUser" class="userAction" method="loginUser">
<result name="success">/userIndex.jsp</result>
<result name="fault">/fault.jsp</result>
</action>
下面是用spring转到的userAction:
public class UserAction extends ActionSupport{
  public String loginUser(){
HttpSession session;
session.setAttribute("id","ss");
return "success";
}
}
现在我想跳到userIndex.jsp页面的时候传一些参数过去。上面我想用的是创建session的方法,但session.setAttribute()时会出错。请问怎么解决?或者有什么其他好的方法。谢谢!

------解决方案--------------------
因为你根本就没有获得Session的实例,execute()方法里有HttpServletRequest参数,你可以用request.getSession()方法获得一个实例,来存储数据。不然像你上面写的代码,会抛一个NullPointerException
------解决方案--------------------
你的sesion要用request来创建。request.getsession().setAttribute();