Struts2访问ServletAPI(向JSP内置对象request,session,Application传值)

来源:互联网 发布:mac 手写 编辑:程序博客网 时间:2024/06/16 10:08

JSP:         request                                 session                                   application

Servlet:HttpServletRequest            HttpSession                            ServletContext

Struts2可通过ActionContext类,ServletActionContext类和IoC(接口)方式访问这三个对象。

ActionContext类:

package com.hp.easycar.action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginServletAPI1 extends ActionSupport {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;}@Overridepublic void validate() {if (username==null||username.trim().equals("")) {addFieldError("username", "输入的用户名不能为空!!");}if (password==null||password.trim().equals("")) {addFieldError("password", "输入的用户名不能为空!!");}}@Overridepublic String execute() throws Exception {if (username.equals("hp")&&password.equals("hp123")) {//获取ActionContext对象<strong>ActionContext context=ActionContext.getContext();</strong>//在请求request中保存欢迎信息<strong>context.put("welcome", "欢迎您登录HP EasyCar系统!");</strong>//在Session中保存用户名<strong>context.getSession().put("username", "HP");</strong>//统计用户量,在Apllacation中保存<strong>Integer count=(Integer) context.getApplication().get("counter");</strong>if (count==null) {count=1;} else {count++;}<strong>context.getApplication().put("counter", count);</strong>return SUCCESS;} else {return ERROR;}}}

ServletActionContext类:

package com.hp.easycar.action;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginServletAPI2 extends ActionSupport {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;}@Overridepublic void validate() {if (username==null||username.trim().equals("")) {addFieldError("username", "输入的用户名不能为空!!");}if (password==null||password.trim().equals("")) {addFieldError("password", "输入的用户名不能为空!!");}}@Overridepublic String execute() throws Exception {if (username.equals("hp")&&password.equals("hp123")) {//在请求request中保存欢迎信息<strong>ServletActionContext.getRequest().setAttribute("welcome", "欢迎您登录HP EasyCar系统!");</strong>//在Session中保存用户名<strong>ServletActionContext.getRequest().getSession().setAttribute("username", "HP");</strong>//统计用户量,在Apllacation中保存<strong>Integer count=(Integer)ServletActionContext.getServletContext().getAttribute("counter");</strong>if (count==null) {count=1;} else {count++;}<strong>ServletActionContext.getServletContext().setAttribute("counter", count);</strong>return SUCCESS;} else {return ERROR;}}}

IoC

package com.hp.easycar.action;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.SessionAware;import org.apache.struts2.util.ServletContextAware;import com.opensymphony.xwork2.ActionSupport;public class LoginServletAPI3 extends ActionSupport implements<strong>ServletRequestAware, SessionAware, ServletContextAware</strong> {<strong>private static final long serialVersionUID=1004770319661315412L;private HttpServletRequest request;private Map<String,Object> session;private ServletContext context;</strong>////////////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;}@Overridepublic void validate() {if (username==null||username.trim().equals("")) {addFieldError("username", "输入的用户名不能为空!!");}if (password==null||password.trim().equals("")) {addFieldError("password", "输入的用户名不能为空!!");}}@Overridepublic String execute() throws Exception {if (username.equals("hp")&&password.equals("hp123")) {//在请求request中保存欢迎信息<strong>request.setAttribute("welcome", "欢迎您登录HP EasyCar系统!");</strong>//在Session中保存用户名<strong>session.put("username", "HP");</strong>//统计用户量,在Apllacation中保存<strong>Integer count=(Integer)context.getAttribute("counter");</strong>if (count==null) {count=1;} else {count++;}<strong>context.setAttribute("counter", count);</strong>return SUCCESS;} else {return ERROR;}}/////////////<strong>@Overridepublic void setServletContext(ServletContext arg0) {// TODO Auto-generated method stub}@Overridepublic void setSession(Map<String, Object> arg0) {// TODO Auto-generated method stub}@Overridepublic void setServletRequest(HttpServletRequest arg0) {// TODO Auto-generated method stub}</strong>}

使用HttpServletRequest:实现ServletRequestAware接口,并重写public void setServletContext(ServletContext arg0)方法

使用HttpServletSession:实现 SessionAware,接口,并重写public void setSession(Map<String, Object> arg0)方法

使用ServletContext实现ServletContextAware接口,并重写public void setServletRequest(HttpServletRequest arg0) 方法

0 0