标签:
ServletAP解析:API(Application Programming Interface) 应用程序编程接口。
ServletAPI就是和Servlet相关的一组接口和类。
为了能够实现对ServletAPI对象的访问,Struts 2 提供了多种方式,但归结起来可分为两大类:
1.与ServletAPI解耦的访问方式
2.与ServletAPI耦合的访问方式
下面介绍解耦方式:
1.使用ActionContex类获取ServletAPI对象对应的Map对象。
public class LoginAction implements Action { private UserInfo user =new UserInfo(); @Override public String execute() throws Exception { if(user.getUserName().equals("admin") && user.getPassword().equals("admin")){ Map<String, Object> session = ActionContext.getContext().getSession(); session.put("name", user.getUserName()); return "success"; }else{ return "error"; } } public UserInfo getUser() { return user; } public void setUser(UserInfo user) { this.user = user; } }
2.向Action注入ServletAPI对象对应的Map对象
public class LoginAction implements Action,SessionAware { private UserInfo user =new UserInfo(); private Map<String, Object> map; @Override public String execute() throws Exception { if(user.getUserName().equals("admin") && user.getPassword().equals("admin")){ map.put("name", user.getUserName()); return "success"; }else{ return "error"; } } //SessionAware实现方法 @Override public void setSession(Map<String, Object> map) { this.map=map; } public UserInfo getUser() { return user; } public void setUser(UserInfo user) { this.user = user; } public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } }
第三种:
public class LoginAction implements Action,ServletRequestAware { private UserInfo user =new UserInfo(); private HttpServletRequest request; @Override public String execute() throws Exception { if(user.getUserName().equals("admin") && user.getPassword().equals("admin")){ request.setAttribute("name", user.getUserName()); return "success"; }else{ return "error"; } } //ServletRequestAware实现方法 @Override public void setServletRequest(HttpServletRequest request) { this.request=request; } public HttpServletRequest getRequest() { return request; } public void setRequest(HttpServletRequest request) { this.request = request; } public UserInfo getUser() { return user; } public void setUser(UserInfo user) { this.user = user; } }
耦合方式:
第一种:
public class LoginAction implements Action { private UserInfo user =new UserInfo(); @Override public String execute() throws Exception { if(user.getUserName().equals("admin") && user.getPassword().equals("admin")){ HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("name", user.getUserName()); return "success"; }else{ return "error"; } } public UserInfo getUser() { return user; } public void setUser(UserInfo user) { this.user = user; } }
第二种:
public class LoginAction implements Action { private UserInfo user =new UserInfo(); @Override public String execute() throws Exception { if(user.getUserName().equals("admin") && user.getPassword().equals("admin")){ Map<String, Object> session = ServletActionContext.getContext().getSession(); session.put("name", user.getUserName()); return "success"; }else{ return "error"; } } public UserInfo getUser() { return user; } public void setUser(UserInfo user) { this.user = user; } }
Struts 2--Struts 2访问ServletAPI对象
标签:
原文地址:http://www.cnblogs.com/szj-ang/p/5920815.html