将数据信息放到Seesion中有三种方式'。通过实例,分别说明一下。现有登陆的类,在其方法中实现了,将用户信息放到Session中。
方法一:通过ActionContext访问Servlet API,此种方式没有入侵性
package com.bjpowernode.struts2;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
public class LoginAction implements Action {
public String username;
public String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute()throws Exception{
if("admin".equals(username) && "admin".equals(password)){
//将登陆信息设置到session中
ActionContext.getContext().getSession().put("user",username);
//采用如下方式访问request对象
//ActionContext.getContext().put(key,value);
//采用如下方式访问application对象
//ActionContext.getContext().getApplication().put(key,value);
//通过request.getParameter()取得数据
//String username = ActionContext.getContext().getParameters().get("username");
return SUCCESS;
}else{
return ERROR;
}
}
}
package com.bjpowernode.struts2;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
public class MustLoginAction implements Action {
public String execute() throws Exception {
//从Session中读取用户信息
//getSession()得到的是Map
if(ActionContext.getContext().getSession().get("user")==null){
//重定向到登录页面
return LOGIN;
}
return SUCCESS;
}
}
方法二:通过实现装配接口,完成对Servlet API的访问
package com.bjpowernode.struts2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action,ServletRequestAware,ServletResponseAware {
public String username;
public String password;
private HttpServletRequest request;
private HttpServletResponse response;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute()throws Exception{
if("admin".equals(username) && "admin".equals(password)){
request.getSession().setAttribute("user", username);
return SUCCESS;
}else{
return ERROR;
}
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
}
package com.bjpowernode.struts2;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.Action;
public class MustLoginAction implements Action,ServletRequestAware {
private HttpServletRequest request;
public String execute() throws Exception {
//从Session中取得用户信息
if(request.getSession().getAttribute("user")==null){
//重定向到登录页面
return LOGIN;
}
return SUCCESS;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
方法三:
package com.bjpowernode.struts2;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action {
public String username;
public String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute()throws Exception{
if("admin".equals(username) && "admin".equals(password)){
ServletActionContext.getRequest().getSession().setAttribute("user", username);
return SUCCESS;
}else{
return ERROR;
}
}
}
package com.bjpowernode.struts2;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
public class MustLoginAction implements Action {
public String execute() throws Exception {
if(ServletActionContext.getRequest().getSession().getAttribute("user")==null){
//重定向到登录页面
return LOGIN;
}
return SUCCESS;
}
}