action:
public class LoginAction {
public String execute(){
String username=request.getParameter("username");
String userpwd=request.getParameter("userpwd");
if(username.equals("scott")&&userpwd.equals("tiger")){
System.out.println(getUsername());
ActionContext.getContext().getSession().put("user", getUsername());
return "success";
}
return "error";
}
} 我记得是用这个方法来取得视图层传过来的数据,如果想要对某个对象赋值,可以用session.setAtribute(" ", );
getParameter()和这个方法setAtribute(“”, )的拼写不知道对不对,有点想不起来了;
public class LoginAction {
public String execute(){
String username=request.getParameter("username");
String userpwd=request.getParameter("userpwd");
if(username.equals("scott")&&userpwd.equals("tiger")){
System.out.println(getUsername());
ActionContext.getContext().getSession().put("user", getUsername());
return "success";
}
return "error";
}
} 我记得是用这个方法来取得视图层传过来的数据,如果想要对某个对象赋值,可以用session.setAtribute(" ", );
getParameter()和这个方法setAtribute(“”, )的拼写不知道对不对,有点想不起来了;
request.getParameter("userpwd");
可能会抛异常,可以try{}catch{}一下
我一般是这么写,一般用DispathMappingAction
if(username.equals("scott")&&userpwd.equals("tiger")){
System.out.println(getUsername());
ActionContext.getContext().getSession().put("user", getUsername());
return "success";
}
return "error";
}改下public String execute(){
if(username.equals("scott")&&userpwd.equals("tiger")){
System.out.println(getUsername());
request.getSession().setAttribute("user", getUsername());
return "success";
}
return "error";
}
最后在显示页面通过${sessionScope.user}获取传递的值
2.显示最好用以下形式。
<s:property value="#session.user" /> 注:#是OGNL表达式的操作符,struts2默认支持的表达式为OGNL表达式,建议不要使用EL表达式。
应该是继承Action或者ActionSupport接口惹得祸吧
其他代码是没有问题的
<result name="error">/error.jsp</result>
<result>/success.jsp</result>
</action>2.画面点提交,输入正确的用户名和密码后,将用户名设置到session中。用以下代码从session中取出来看看。 String user = (String) ServletActionContext.getRequest().getSession().getAttribute("user");
System.out.println("session user :" + user);3.如果能取出来,jsp中用以下代码进行显示看看。 <s:property value="#session.user"/>4.如果用上面的形式能输出来,那你的jsp页面不支持EL(不支持画面显示:${sessionScope.user}),否则应该是环境问题。