項目網站:www.aikan66.com
遊戲網站:www.aikan66.com
圖片網站:www.aikan66.com
書籍網站:www.aikan66.com
學習網站:www.aikan66.com
Java網站:www.aikan66.com
iOS網站:www.aikan66.comhtml
----java
經過ActionContext對象獲取map類型的request、session、application,分別爲這3個對象設置一個info屬性並賦值,而後在jsp頁面獲取3種做用域下的info屬性信息。web
----apache
一、建立web項目,jwrm05-mapRequest,拷貝包到lib,配置web.xm。(詳見web08)session
----app
二、建立類TestActionjsp
package dog; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport { private static final long serialVersionUID=1L; //map類型的request private Map<String,Object>request; //map類型的session private Map<String,Object>session; //map類型的application private Map<String,Object>application; //構造方法 @SuppressWarnings("unchecked") public TestAction(){ //獲取ActionContext對象 ActionContext context=ActionContext.getContext(); //獲取Map類型的request request=(Map<String,Object>)context.get("request"); //獲取Map類型的session session=context.getSession(); //獲取Map類型的application application=context.getApplication(); } /* * 請求處理方法 * return String */ public String execute() throws Exception{ //字符串信息 String info="星網:www.aikan66.com"; //向request添加信息 request.put("info", info); //向Session添加信息 session.put("info", info); //向application添加信息 application.put("info", info); //成功返回 return SUCCESS; } }
----學習
三、配飾struts.xml網站
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 聲明包 --> <package name="myPackage" extends="struts-default"> <!-- 定義action --> <action name="testAction" class="dog.TestAction"> <!-- 定義成功的映射頁面 --> <result>success.jsp</result> </action> </package> </struts>
----
四、建立success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> request範圍內的info值: <font color="red"><%=request.getAttribute("info") %></font><br> session範圍內的info值: <font color="red"><%=session.getAttribute("info")%></font><br> application範圍內info值: <font color="red"><%=application.getAttribute("info") %></font> </body> </html>
index.jsp中添加
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <a href="testAction.action">Map類型的request、session、application</a> </body> </html>
----
五、部署,訪問:http://localhost:8080/jwrm05-mapRequest/index.jsp
點擊,結果
----
完畢
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <!-- 聲明包 --> <package name="myPackage" extends="struts-default"> <!-- 定義action --> <action name="testAction" class="dog.TestAction"> <!-- 定義成功的映射頁面 --> <result>success.jsp</result> </action> </package> </struts>