1. servlet 不相关的 非IoC 取得Request等对象的方式
2. servlet 不相关的 IoC 取得Request等对象的方式
3. servlet 相关的 非IoC 取得Request等对象的方式
4. servlet 相关的 IoC 取得Request等对象的方式
步骤一: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>
<package name="default" extends="struts-default">
<action name="login1" class="struts2.login.Login1Action">
<result name="success" >result.jsp</result>
</action>
<action name="login2" class="struts2.login.Login2Action">
<result name="success" >result.jsp</result>
</action>
<action name="login3" class="struts2.login.Login3Action">
<result name="success" >result.jsp</result>
</action>
<action name="login4" class="struts2.login.Login4Action">
<result name="success" >result.jsp</result>
</action>
</package>
</struts>
步骤二:四个类文件
Login1Action.java
package struts2.login;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* servlet 不相关的非IoC
* @author于祥通
* 2010-5-31上午10:19:52
* Login1Action.java
*/
publicclass Login1Action extends ActionSupport {
private ActionContext context;
private Map request;
private Map session;
private Map application;
public String execute() throws Exception {
this.context = ActionContext.getContext();
this.request = (Map) this.context.get("request");
this.session = this.context.getSession();
this.application = this.context.getApplication();
this.request.put("att", "request value servlet 不相关的非IoC ");
this.session.put("att", "session value servlet 不相关的非IoC ");
this.application.put("att",
"application value servlet 不相关的非IoC ");
returnSUCCESS;
}
}
Login2Action.java
package struts2.login;
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;
/**
* servlet 不相关的 IoC
* @author于祥通
* 2010-5-31上午10:20:12
* Login2Action.java
*/
publicclass Login2Action extends ActionSupport implements RequestAware,
SessionAware, ApplicationAware {
private Map<String,Object> request;
private Map<String,Object> session;
private Map<String,Object> application;
public String execute() throws Exception {
request.put("att","request value servlet 不相关的 IoC ");
session.put("att","session value servlet 不相关的 IoC ");
application.put("att","application value servlet 不相关的 IoC ");
returnSUCCESS;
}
publicvoid setRequest(Map<String, Object> arg0) {
this.request=arg0;
}
publicvoid setSession(Map<String, Object> arg0) {
this.session=arg0;
}
publicvoid setApplication(Map<String, Object> arg0) {
this.application=arg0;
}
}
Login3Action.java
package struts2.login;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* servlet 相关的非IoC
* @author于祥通
* 2010-5-31上午10:20:26
* Login3Action.java
*/
publicclass Login3Action extends ActionSupport {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
@Override
public String execute() throws Exception {
this.request = ServletActionContext.getRequest();
this.session = this.request.getSession();
this.application= ServletActionContext.getServletContext();
this.request.setAttribute("att","request value servlet 相关的非IoC ");
this.session.setAttribute("att","session value servlet 相关的非IoC ");
this.application.setAttribute("att","application value servlet 相关的非IoC ");
returnSUCCESS;
}
}
Login4Action.java
package struts2.login;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
/**
* servlet 相关的 IoC
* @author于祥通
* 2010-5-31上午10:20:37
* Login4Action.java
*/
publicclass Login4Action extends ActionSupport implements ServletRequestAware , ServletContextAware{
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public String execute() throws Exception {
this.request.setAttribute("att", "request value servlet 相关的 IoC ");
this.session = this.request.getSession();
this.session.setAttribute("att", "session value servlet 相关的 IoC ");
this.application.setAttribute("att", "application value servlet 相关的 IoC ");
returnSUCCESS;
}
publicvoid setServletContext(ServletContext arg0) {
application=arg0;
}
publicvoid setServletRequest(HttpServletRequest arg0) {
request=arg0;
}
}
步骤三:jsp页面
getrequest.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<a href="login1.action">servlet不相关的非IoC 取得Request等对象的方式</a><br>
<a href="login2.action">servlet不相关的 IoC 取得Request等对象的方式</a><br>
<a href="login3.action">servlet相关的非IoC 取得Request等对象的方式</a><br>
<a href="login4.action">servlet相关的 IoC 取得Request等对象的方式</a><br>
</html>
result.jsp
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<%@tagliburi="/struts-tags"prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>My JSP 'result.jsp' starting page</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:debug></s:debug>
*****************************取值法一********************************<br/>
<s:propertyvalue="#request.att"/><br/>
<s:propertyvalue="#session.att"/><br/>
<s:propertyvalue="#application.att"/><br/>
*****************************取值法二********************************<br/>
<%=request.getAttribute("att") %><br/>
<%=session.getAttribute("att") %><br/>
<%=application.getAttribute("att") %><br/>
*****************************取值法三********************************<br/>
request 属性是 ${request.att}<br>
session 属性是 ${session.att}<br>
application 属性是 ${application.att}<br>
</body>
</html>
教程学习截图:
第一种方法取到request,session,application.jpg
第二种方法取到request,session,application.jpg
第三种方法取到request,session,application.jpg
jsp中代码.jpg
struts2输出结果1.jpg