Struts2访问ServletAPI(向JSP内置对象request,session,Application传值)
2016-09-13 11:00
405 查看
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;
}
@Override
public void validate() {
if (username==null||username.trim().equals("")) {
addFieldError("username", "输入的用户名不能为空!!");
}
if (password==null||password.trim().equals("")) {
addFieldError("password", "输入的用户名不能为空!!");
}
}
@Override
public 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;
}
@Override
public void validate() {
if (username==null||username.trim().equals("")) {
addFieldError("username", "输入的用户名不能为空!!");
}
if (password==null||password.trim().equals("")) {
addFieldError("password", "输入的用户名不能为空!!");
}
}
@Override
public 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;
}
@Override
public void validate() {
if (username==null||username.trim().equals("")) {
addFieldError("username", "输入的用户名不能为空!!");
}
if (password==null||password.trim().equals("")) {
addFieldError("password", "输入的用户名不能为空!!");
}
}
@Override
public 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>@Override
public void setServletContext(ServletContext arg0) {
// TODO Auto-generated method stub
}
@Override
public void setSession(Map<String, Object> arg0) {
// TODO Auto-generated method stub
}
@Override
public 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) 方法
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;
}
@Override
public void validate() {
if (username==null||username.trim().equals("")) {
addFieldError("username", "输入的用户名不能为空!!");
}
if (password==null||password.trim().equals("")) {
addFieldError("password", "输入的用户名不能为空!!");
}
}
@Override
public 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;
}
@Override
public void validate() {
if (username==null||username.trim().equals("")) {
addFieldError("username", "输入的用户名不能为空!!");
}
if (password==null||password.trim().equals("")) {
addFieldError("password", "输入的用户名不能为空!!");
}
}
@Override
public 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;
}
@Override
public void validate() {
if (username==null||username.trim().equals("")) {
addFieldError("username", "输入的用户名不能为空!!");
}
if (password==null||password.trim().equals("")) {
addFieldError("password", "输入的用户名不能为空!!");
}
}
@Override
public 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>@Override
public void setServletContext(ServletContext arg0) {
// TODO Auto-generated method stub
}
@Override
public void setSession(Map<String, Object> arg0) {
// TODO Auto-generated method stub
}
@Override
public 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) 方法
相关文章推荐
- Struts2访问ServletAPI(Request, Session, ServletContext,Application,response)
- [Struts2] 如何访问或添加request/session/application属性, 以及获取对象.
- Struts2访问web元素(包括:request,session,application和HttpServletRequest,HttpServletSession, HttpServletContext)的4种方法
- struts2中,在request/session/application中访问或添加属性
- Struts2访问request,session,application的四种方式
- 关于struts2中对 request /session/ application 的访问
- Struts2初级教程03 访问request session application
- 在 Struts2 中访问 Request、Session 和 Application
- Struts2访问web元素(Request,Session,Application)的四种方式
- struts2示例代码-访问或者添加application、session、request等几个属性
- Struts2访问request,session,application的四种方式
- struts2访问或添加request/session/application属性
- Struts2 访问或添加request/session/application属性
- struts2中freemarker访问request session Application parameter对象
- Struts2 访问request、session和application对象
- 关于struts2中对 request session application 的访问
- Struts2 访问request、session和application对象
- 在Struts2中访问Web页面元素request、response、session、application的四种方法
- 【Struts2学习笔记(8)】访问或添加request/session/application属性获取HttpServletRequest / HttpSession / ServletContex
- struts2中访问和添加Application、session以及request属性