web12 使用map型的request、session、application
项目网站:www.aikan66.com
游戏网站:www.aikan66.com
图片网站:www.aikan66.com
书籍网站:www.aikan66.com
学习网站:www.aikan66.com
Java网站:www.aikan66.com
iOS网站:www.aikan66.com
----
通过ActionContext对象获取map类型的request、session、application,分别为这3个对象设置一个info属性并赋值,然后在jsp页面获取3种作用域下的info属性信息。
----
1、创建web项目,jwrm05-mapRequest,拷贝包到lib,配置web.xm。(详见web08)
----
2、创建类TestAction
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; } }
----
3、配饰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>
----
4、创建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>
----
5、部署,访问: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>