struts2后台的Action跟我们前台的页面来通讯的时候,由于它不能直接拿到request,session,servletContext,比如当我们有人登陆了,我们要在session中设一个值,表示它登陆了,如何把相关的数据设到session里面?同样如何设置request、servletContext等?response不用访问它,因为我们的结果是通过result返回的.
访问Web元素可以有四种方法,下面依次列出
index.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<% String context = request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
<form name="f" action="" method="post">
用户名:<input type="text" name="name"/>
密码:<input type="text" name="password"/>
<br />
<input type="button" value="submit1" onclick="javascript:document.f.action='login/login1';document.f.submit();" />
<input type="button" value="submit2" onclick="javascript:document.f.action='login/login2';document.f.submit();" />
<input type="button" value="submit3" onclick="javascript:document.f.action='login/login3';document.f.submit();" />
<input type="button" value="submit4" onclick="javascript:document.f.action='login/login4';document.f.submit();" />
</form>
</body>
</html>
user_login_success.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
User Login Success!
<br />
<s:property value="#request.r1"/> | <%=request.getAttribute("r1") %> <br />
<s:property value="#session.s1"/> | <%=session.getAttribute("s1") %> <br />
<s:property value="#application.a1"/> | <%=application.getAttribute("a1") %> <br />
<s:property value="#attr.a1"/><br />
<s:property value="#attr.s1"/><br />
<s:property value="#attr.r1"/><br />
<s:debug></s:debug>
<br />
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="login" extends="struts-default" namespace="/login">
<action name="login*" class="com.wwm.struts2.user.action.LoginAction{1}">
<result>/user_login_success.jsp</result>
</action>
</package>
</struts>
1、通过struts2 中的ActionContext类来访问(不常用)
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction1 extends ActionSupport {
private Map request;
private Map session;
private Map application;
public LoginAction1() {
request = (Map)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
}
public String execute() {
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return SUCCESS;
}
}
Context直接翻译过来是:”上下文”。Context就是当前执行的环境,它会把周围环境帮你封转到Context这个对象里面,当你想访问这些东西的时候,通过Context去取。
原来学习过得ServletContext代表的就是servlet的运行环境,原来我们写程序就用ServletContext拿到各种各样的配置,ActionContext也就是Action周边运行的情况,Action运行的时候,首先接收到请求接收到request,response等等后面再接着调你的处理过程,Action处理的过程中,比如说那些配置怎么配的,执行环境怎么样等等都要通过ActionContext来拿。
ActionContext类中以下几个方法为其常用:
Static ActionContext getContext(): 获取ActionContext实例;
Object get(Object key): 类似于request.getAttribute(String name);
Map getParameters():获取所有的请求参数,类似于request.getParameterMap();
Map getApplication(): 相当于ServletContext实例,返回的是Map对象
Map getSession() :返回类似于HttpSession的实例
void setApplication(Map application): 直接传入一个Map实例,将Map实例里的key-value对转换成application的key-value
void setSession(Map session): 直接传入一个Map实例,将Map实例里的key-value对转换成session的key-value
我们的后台放了request,session,application这三个东西都是map,但是它反馈到前台之后居然用request,session,application可以拿到,所以struts2一定帮我们在Map和HttpRequest之间建立某种联系,对于我们自己设定的map类型的request这里面的属性在处理的过程中会全都复制到Http对象里边去。应该就是用void setApplication(Map application),void setSession(Map session)做的处理
在jsp中
第一种访问request里面的值:<%=request.getAttribute("r1") //原始的方式
第二种访问request里面的值:<s:property value="#request.r1"/>
2.IoC方式 IoC(控制反转)是Spring里面的特征之一(最常用)
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.ActionSupport;
public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware {
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
//DI dependency injection
//IoC inverse of control
public String execute() {
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return SUCCESS;
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
}
一个请求过来Struts会new出一个Action出来;2.new完之后它会检查Action是否实现了RequestAware接口.3如果Action实现了RequestAware接口,就可以调用RequestAware接口的setRequest()方法.
而且其实request我们也很少去拿它,因为我们Action的成员变量默认会起到request的作用,它自己会放到valueStack里面, valueStack本身就是放到request里面,所以根本不用去拿request.
3、
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction3 extends ActionSupport {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public LoginAction3() {
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
}
public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return SUCCESS;
}
}
4、
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction4 extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return SUCCESS;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
this.application = session.getServletContext();
}
}
总结:
第一种方法依赖于Struts2容器,取得相关对象数据;
第二种方法,依赖注入DI(dependency injection),控制反转IoC(Inverse of control),对象数据被容器注入;
第三种方法,依赖于容器取得相关对象数据;
第四种方法,依赖注入一个对象的数据后,再通过这个对象获取到其它对象数据。
推荐使用第二种方法。 [color=red][/color]