Struts2 Action 访问web对象一共有四种,分别是:
一、创建request,session,application的Map对象
二、实现RequestAware,SessionAware,ApplicationAware接口(IOC注入)
三、创建传统的HttpServletRequest,HttpSession,ServletContext对象
四、实现ServletRequestAware,ServletContextAware接口(IOC注入)
其中 二、四方法是ICO(注入)方式实现,都是通过一、二的基础上加以改进的实现方式,下面列举相对应的集体实例:
一、创建request,session,application的Map对象(主要代码背景蓝色标记)
LoginAction.java
package com.wym.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wym.bean.User;
import com.wym.bussiess.UserDao;
public class LoginAction extends ActionSupport{
private User user;
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
public LoginAction(){
System.out.println("执行LoginAction构造方法!");
request = (Map)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
}
public String execute(){
UserDao userDao = new UserDao();
if(userDao.loginUser(user)){
request.put("a", "this is a");
session.put("b", "this is b");
application.put("c", "this is c");
return "success";
}
else{
return "faint";
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
获取web对象的值(jsp文件中)
<s:property value="#request.a"/><br/> //获取request域中的值
<s:property value="#session.b"/><br/> //获取session域中的值
<s:property value="#application.c"/><br/>
<s:property value="#attr.b"/><br/> //先发现那个域由该属性名就使用该域
从实例中可以看到,该方式的实现方法,首先创建私有(private)的Map对象,
对象名分别为request、session、application 之后在Action的构造方法中初始化这三个Map对象,在为这三个对象赋值时,要使用Map的put方法。
二、实现RequestAware,SessionAware,ApplicationAware接口
该方式与第一种方式差不多,仅仅是实现RequestAware,SessionAware,ApplicationAware三个接口 并实现对应的方法 也需要创建Map对象 但不需要再Action的构造方法中初始化
package com.wym.action;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wym.bean.User;
import com.wym.bussiess.UserDao;
public class LoginAction extends ActionSupport implements RequestAware, SessionAware, ApplicationAware{
private User user;
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
public LoginAction(){
System.out.println("执行LoginAction构造方法!");
}
public String execute(){
UserDao userDao = new UserDao();
if(userDao.loginUser(user)){
request.put("a", "this is a");
session.put("b", "this is b");
application.put("c", "this is c");
return "success";
}
else{
return "faint";
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void setRequest(Map<String, Object> request) {
// TODO Auto-generated method stub
this.request = request;
}
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session = session;
}
public void setApplication(Map<String, Object> application) {
// TODO Auto-generated method stub
this.application = application;
}
}
三、创建传统的HttpServletRequest,HttpSession,ServletContext对象
package com.wym.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wym.bean.User;
import com.wym.bussiess.UserDao;
public class LoginAction extends ActionSupport{
private User user;
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public LoginAction(){
System.out.println("执行LoginAction构造方法!");
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
}
public String execute(){
UserDao userDao = new UserDao();
if(userDao.loginUser(user)){
request.setAttribute("a", "this is a");
session.setAttribute("b", "this is b");
application.setAttribute("c", "this is c");
return "success";
}
else{
return "faint";
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
四、实现ServletRequestAware,ServletContextAware接口
package com.wym.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
import com.wym.bean.User;
import com.wym.bussiess.UserDao;
public class LoginAction extends ActionSupport implements ServletContextAware, ServletRequestAware{
private User user;
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public LoginAction(){
System.out.println("执行LoginAction构造方法!");
}
public String execute(){
UserDao userDao = new UserDao();
if(userDao.loginUser(user)){
request.setAttribute("a", "this is a");
session.setAttribute("b", "this is b");
application.setAttribute("c", "this is c");
return "success";
}
else{
return "faint";
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void setServletContext(ServletContext application) {
// TODO Auto-generated method stub
this.application = application;
}
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request = request;
this.session = request.getSession();
}
}