首页 > 代码库 > Struts 2--Struts 2访问ServletAPI对象
Struts 2--Struts 2访问ServletAPI对象
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对象
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。