标签:action scope lan cat space print 定义 cep des
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <% String path=request.getContextPath(); %> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <pre> <h3>把数据放到Servlet原生作用域(request、session、servletContext)然后显示</h3> <a href="<%=path%>/scope/scope_1">点我显示数据</a> </pre> </body> </html>
界面:
struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/scope" extends="struts-default"> <action name="scope_1" class="scope.ActionScope_1"> <result name="show">/show.jsp</result> </action> </package> </struts>
ActionScope.java
package scope; import action.BaseAction; public class ActionScope_1 extends BaseAction{ @Override public String execute() { request.setAttribute("request_key", "request作用域的值"); session.setAttribute("session_key", "session作用域的值"); servletContext.setAttribute("servletContext_key", "servletContext作用域的值"); return "show"; } }
show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <pre> <h3>显示放在作用域中的数据</h3> request作用域的值为:${requestScope.request_key } session作用域的值为:${sessionScope.session_key } servletContext作用域的值为:${applicationScope.servletContext_key } </pre> </body> </html>
结果:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <% String path=request.getContextPath(); %> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <pre> <h3>把数据放到Struts封装的作用域中然后显示</h3> <a href="<%=path%>/scope/scope_2">点我显示数据</a> </pre> </body> </html>
界面:
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/scope" extends="struts-default"> <action name="scope_2" class="scope.ActionScope_2"> <result name="show">/show.jsp</result> </action> </package> </struts>
ActionScope_2.java
package scope; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import action.BaseAction; public class ActionScope_2 extends BaseAction{ /** * 用Struts封装的作用域ActionContext(Action上下文)存放数据 * ActionContext和ServletActionContext的区别,ServletActionContext用于 * 获取servlet原生对象 * ActionContext把servlet作用域封装成Map */ @Override public String execute() { ActionContext actionContext=ActionContext.getContext(); //request作用域的获取比较特殊 //获取action封装后的作用域 Map<String,Object> requestMap=(Map<String,Object>)actionContext.get("request"); Map<String,Object> sessionMap=actionContext.getSession(); Map<String,Object> applicationMap=actionContext.getApplication(); //在作用域中存放数据 requestMap.put("requestMap_key", "requset中的值1"); sessionMap.put("sessionMap_key", "sessionMap中的值1"); applicationMap.put("applicationMap_key", "applicationMap中的值1"); return "show"; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <pre> <h3>显示放在作用域中的数据</h3> request作用域的值为:${requestScope.requestMap_key } session作用域的值为:${sessionScope.sessionMap_key } servletContext作用域的值为:${applicationScope.applicationMap_key } </pre> </body> </html>
结果:
以上述例子所示,index.jsp和struts.xml和ActionScope_2.java文件均与上例一样,show.jsp如下:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <pre> 12 <h3>El表达式+JSTL标签显示放在作用域中的数据</h3> 13 <c:if test="${requestScope.requestMap_key==null}"> 14 <c:out value="${requestScope.requestMap_key}"></c:out> 15 </c:if> 16 17 session作用域的值为:${sessionScope.sessionMap_key } 18 servletContext作用域的值为:${applicationScope.applicationMap_key } 19 </pre> 20 </body> 21 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 <%@ taglib prefix="s" uri="/struts-tags" %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <pre> 13 <h3>El表达式+JSTL标签显示放在作用域中的数据</h3> 14 <c:if test="${requestScope.requestMap_key==null}"> 15 <c:out value="${requestScope.requestMap_key}"></c:out> 16 </c:if> 17 18 <h3>OGNL表达式+Struts标签显示放在作用域中的数据</h3> 19 20 <s:if test="#request.requestMap_key!=null"> 21 <s:property value="#request.requestMap_key"/> 22 </s:if> 23 </pre> 24 </body> 25 </html>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet> <description></description> <display-name>TestServlet</display-name> <servlet-name>TestServlet</servlet-name> <servlet-class>servlet.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/servlet/test</url-pattern> </servlet-mapping>
我们希望会执行servlet.TestServlet这个servlet类,但是实际上被struts拦截器当成action了,就去struts.xml中找命名空间为/servlet的action名为TestServlet,但是找不到于是就是报错。
要解决这个问题就不得不提起struts.xml中的一个常量<constant name="struts.action.extension" value="=action,,"/> 默认也是这个,意思是对于所有的请求后缀名为action或者为空的请求,都会被拦截,并到struts.xml中寻找相对应的Action.
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <% String path=request.getContextPath(); %> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <pre> <a href="<%=path%>/servlet/test.do">点击跳转到servlet页面</a> </pre> </body> </html>
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <constant name="struts.action.extension" value="=action,,"/> <package name="default" namespace="/scope" extends="struts-default"> <action name="scope_2" class="scope.ActionScope_2"> <result name="show">/show.jsp</result> </action> </package> </struts>
<servlet> <description></description> <display-name>TestServlet</display-name> <servlet-name>TestServlet</servlet-name> <servlet-class>servlet.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/servlet/test.do</url-pattern> </servlet-mapping>
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out=response.getWriter(); out.print("我是servlet"); }
结果:
标签:action scope lan cat space print 定义 cep des
原文地址:http://www.cnblogs.com/shyroke/p/6553811.html