如何在jsp(struts)中打印会话属性
这是我有的:
Java类(添加用户):
public String addUser() throws NoSuchAlgorithmException { HttpSession currentSession = request.getSession(); User u = new User(); u.setUname(getUserName()); u.setPassword(StringHash(getUserPass())); u.setUtype(getUserType()); plResponse = iUserDAO.addUser(u); setActionMessage(plResponse.getMessage()); currentSession.setAttribute("actionMessage", this.actionMessage); return SUCCESS; }
Java类(添加关联):
public String saveAssoc() throws Exception { HttpSession currentSession = request.getSession(); try { plResponse = iUserDAO.saveUserAssoc(currentSession.getAttribute("selectedUser").toString(), countryId, langId); refreshUserAssociations(); setActionMessage(plResponse.getMessage()); currentSession.setAttribute("actionMessage", this.actionMessage); } catch (Exception e) { throw new Exception(e.getMessage()); } return SUCCESS; }
JSP(两种情况相同):
我有两种将返回消息传递给页面的情况。 添加用户后,添加用户关联后。 在这两种情况下,参数都正确地传递给会话(我对代码进行了调试),但它仅在第一种情况下显示(添加用户)。 第二种情况假装在会话中没有actionMessage。
可能是什么原因?
实际上,第一种或第二种情况都不是从会话中显示消息。 它正在寻找值堆栈中的变量。 在第一种情况下,您具有返回值的action属性getter。 在第二种情况下,它可能不是相同的值。 在动作类中使用会话的正确方法是实现SessionAware
,它通过servletConfig
拦截器向会话映射注入动作bean属性。 然后使用该映射而不是http会话。 请参阅如何访问会话 。
public String addUser() throws NoSuchAlgorithmException {
HttpSession currentSession = request.getSession();映射currentSession = ActionContext.getContext()。getSession(); 用户u =新用户(); u.setUname(getUserName()); u.setPassword(StringHash(getUserPass())); u.setUtype(getUserType()); plResponse = iUserDAO.addUser(u); setActionMessage(plResponse.getMessage());currentSession.setAttribute(“actionMessage”,this.actionMessage);currentSession.put(“actionMessage”,getActionMessage()); 返回SUCCESS; }
在JSP中,您可以从上下文中访问会话对象。
要访问存储在会话中的属性,请使用以下代码:
检查在属性标记页中访问范围对象以查看更多详细信息。