struts2访问servletAPI的几种方式
2017-12-08 09:36
204 查看
struts2作为一个控制层的框架,难免要与servlet接触,下面是常用的三种访问serveltAPI的方式:
通过ActionContext对象间接访问servletAPI
通过在业务控制类action实现相应的接口
通过ServletActionContext直接在业务控制类action里面访问
注:之所以称之为间接访问,是因为ActionContext并不是直接调用对应的域的setAttribute( key, value )方法放入属性和值,而是通过put方法间接的调用setAttribute方法,其底层实际上还是调用的setAttribute设置属性和值。
实现ServletResponseAware可以获得response域
实现ServletContextAware可以获得Application域
通过ActionContext对象间接访问servletAPI
通过在业务控制类action实现相应的接口
通过ServletActionContext直接在业务控制类action里面访问
1.通过ActionContext对象间接访问servletAPI
package com.lyu.struts.sysmanage.action; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; public class LoginAction { private String userName; private 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() { System.out.println(userName); System.out.println(password); // 将用户名放入session,为将来的访问控制做准备 ActionContext.getContext().getSession().put("userName1", userName + "actionContext"); // 放入request域里面 ActionContext.getContext().put("userName2", userName + "actionContext"); // 放入application域里面 ActionContext.getContext().getApplication().put("userName3", userName + "actionContext"); // 将用户名放入cookie中 HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE); Cookie cookie = new Cookie("userName4", userName + "actionContext"); cookie.setMaxAge(24*60*60); response.addCookie(cookie); if (userName.equals("admin")) { return "success"; } else { return "fail"; } } }
注:之所以称之为间接访问,是因为ActionContext并不是直接调用对应的域的setAttribute( key, value )方法放入属性和值,而是通过put方法间接的调用setAttribute方法,其底层实际上还是调用的setAttribute设置属性和值。
2.通过在业务控制类action实现相应的接口
实现ServletRequestAware可以获得request域实现ServletResponseAware可以获得response域
实现ServletContextAware可以获得Application域
package com.lyu.struts.sysmanage.action; import javax.servlet.ServletContext; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.util.ServletContextAware; public class LoginSecondAction implements ServletRequestAware, ServletResponseAware, ServletContextAware { private String userName; private String password; private HttpServletRequest serveltRequest; private HttpServletResponse serveltResponse; private ServletContext servletContext; 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() { System.out.println(userName); System.out.println(password); // 将用户名放入session,为将来的访问控制做准备 this.serveltRequest.getSession().setAttribute("userName1", userName + "aware"); // 放入request域里面 this.serveltRequest.setAttribute("userName2", userName + "aware"); // 放入application域里面 this.servletContext.setAttribute("userName3", userName + "aware"); // 将用户名放入cookie中 HttpServletResponse response = this.serveltResponse; Cookie cookie = new Cookie("userName4", userName + "aware"); cookie.setMaxAge(24*60*60); response.addCookie(cookie); if (userName.equals("admin")) { return "success"; } else { return "fail"; } } @Override public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; } @Override public void setServletResponse(HttpServletResponse serveltResponse) { this.serveltResponse = serveltResponse; } @Override public void setServletRequest(HttpServletRequest serveltRequest) { this.serveltRequest = serveltRequest; } }
3.通过ServletActionContext直接在业务控制类action里面访问
直接调用setAttribute方法设置属性和值。package com.lyu.struts.sysmanage.action; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; public class LoginThirdAction { private String userName; private 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() { System.out.println(userName); System.out.println(password); // 将用户名放入session,为将来的访问控制做准备 ServletActionContext.getRequest().getSession().setAttribute("userName1", userName + "ServletActionContext"); // 放入request域里面 ServletActionContext.getRequest().setAttribute("userName2", userName + "ServletActionContext"); // 放入application域里面 ServletActionContext.getServletContext().setAttribute("userName3", userName + "ServletActionContext"); // 将用户名放入cookie中 HttpServletResponse response = ServletActionContext.getResponse(); Cookie cookie = new Cookie("userName4", userName + "ServletActionContext"); cookie.setMaxAge(24*60*60); response.addCookie(cookie); if (userName.equals("admin")) { return "success"; } else { return "fail"; } } }
相关文章推荐
- Struts2访问ServletAPI的三种方式,以及使用推荐对比
- Struts2中访问ServletAPI的四种方式
- Java EE SSH框架之Struts2(3)—— 结果跳转方式和访问ServletAPI方式
- struts2学习笔记--总结获取servletAPI的几种方式
- Structs中Action访问ServletAPI的几种方式
- 总结Struts2获取servletAPI的几种方式
- Struts2的Action中方法的几种访问方式
- Struts2获取servletAPI的几种方式
- Struts2访问ServletAPI的三种方式
- Struts2的Action中访问servletAPI方式
- JAVAEE学习——struts2_02:结果跳转方式、访问servletAPI方式、获得参数以及封装、练习:添加客户
- Struts2学习(五)【访问servletAPI方式】
- Struts2的国际化和访问资源文件的几种方式
- Struts2的Action访问ServletAPI的方式
- Struts2中访问Servlet API的几种方式
- 访问Struts2中的自定义方法的几种方式
- struts2的Action访问Servlet的几种方式和全局result
- Struts2的国际化和访问资源文件的几种方式
- 访问web-inf下jsp资源的几种方式
- 访问Excel的几种方式