struts2的基础讲解。
用前面讲的登录做例子,将登录的用户信息放到session中。
1、通过ActionContext访问Servlet API,此种方式没有侵入性.
2、如:
package com.struts2.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
public class loginAction implements Action{
//第二种
private String userName;
private String userPaw;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPaw() {
return userPaw;
}
public void setUserPaw(String userPaw) {
this.userPaw = userPaw;
}
public String execute() throws Exception{
if ("admin".equals(userName)&&"admin".equals(userPaw)) {
ActionContext.getContext().getSession().put("username", userName);//将登陆信息放到session中
//采用如下方式访问request
ActionContext.getContext().put(key,value);
//采用如下方式访问application对象
ActionContext.getContext().getAppliction().put(key,value);
//通过request.getParameter()取得数据
String username=ActionContext.getContext().getParameter().get("userName");
return "success";
}else{
return "error";
}
}
}
2、struts.xml的配置和前面的一样。