日期:2014-05-20  浏览次数:20704 次

如何把JSP中声明的变量传给Action
我在JSP中声明了变量username如下:(ACEGI获取用户名)
<%
 
  SecurityContext context=(SecurityContext)SecurityContextHolder.getContext();
  Authentication auth=context.getAuthentication();
  String username="";
  if(auth.getPrincipal() instanceof UserDetails){
  username=((UserDetails)auth.getPrincipal()).getUsername();

   
   
  } else{
  username=auth.getPrincipal().toString();
  }
%>


我现在需要把username传到action里,应该用什么样的方式?
或者是我可以在ACTION中直接调用ACEGI获取用户名的方法吗?我试了一下再ACTION中调用上面那段代码来获取用户名,结果是NULLPOINTEXCEPTION
请教高手指点

------解决方案--------------------
探讨
引用:
我在jsp中加入 ServletActionContext.getRequest().getSession().setAttribute("name",username)
该JSP页面报错啊

问题就出在我在JSP中把username装进request.setAttribute;
在Action中用ServletActionContext.getRequest().getSession().getAttribute("login")
然后System.out.println(login)
系统打印的值为NULL
究竟是怎么回事啊?