Action访问Servlet API 在页面间传递参数
2010-09-25 15:17
417 查看
1.Struts 2 的Action可以通过ActionContext来访问Servlet API。下面是几种常用的方法
a. Object get(Object key):该方法类似于调用HttpServletRequest的getAttribute(String name)
方法。
b. Map getApplication():返回一个Map对象,该对象模拟了该应用的ServletContext实例。
c. static ActionContext getContext():静态方法,获取系统的ActionContext实例。
d. Map getParameters():获取所有的请求参数。类似于调用HttpServletRequest对象的
getparameterMap()方法。
e. Map getSession():返回一个Map对象,该对象模拟了HttPSession实例。
f. void setApplication(Map application):直接传入一个map实例,将该Map实例里的key-value对转
换成application的属性名、属性值
g. void setSession(Map session):直接传入一个map实例,将该Map实例里的key-value对转 换成
session的属性名、属性值
第一步:写登陆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>
<form name="myform" action="Login.action">
请输入姓名:<input type="text" name="myname"/><br/>
请输入密码:<input type="password" name="password"/><br/>
<input type="submit"/>
</form>
</body>
</html>
第二步:写Action类
第三步:配置sturts.xml
第四步:登陆成功界面jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
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 'welcome.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>
本站访问次数为:${applictionScope.counter }<br/>
${sessionScope.user },你已经登录
${requestScope.tip }
<br/>
<s:property value="#attr.counter"/><br/>
<s:property value="#attr.user"/><br/>
<!-- 因为是requset,所以可以直接取值不用#attr -->
<s:property value="tip"/>
</body>
</html>
注意:上面是两种取值方法
a. Object get(Object key):该方法类似于调用HttpServletRequest的getAttribute(String name)
方法。
b. Map getApplication():返回一个Map对象,该对象模拟了该应用的ServletContext实例。
c. static ActionContext getContext():静态方法,获取系统的ActionContext实例。
d. Map getParameters():获取所有的请求参数。类似于调用HttpServletRequest对象的
getparameterMap()方法。
e. Map getSession():返回一个Map对象,该对象模拟了HttPSession实例。
f. void setApplication(Map application):直接传入一个map实例,将该Map实例里的key-value对转
换成application的属性名、属性值
g. void setSession(Map session):直接传入一个map实例,将该Map实例里的key-value对转 换成
session的属性名、属性值
第一步:写登陆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>
<form name="myform" action="Login.action">
请输入姓名:<input type="text" name="myname"/><br/>
请输入密码:<input type="password" name="password"/><br/>
<input type="submit"/>
</form>
</body>
</html>
第二步:写Action类
package com; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class Login extends ActionSupport{ private String myname; private String password; public String getMyname() { return myname; } public void setMyname(String myname) { this.myname = myname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute() { ActionContext cxt = ActionContext.getContext(); Integer counter = (Integer)cxt.getApplication().get("counter"); if(counter==null){ counter=1; }else { counter+=1; } //System.out.println(counter); //通过ActionContext设置application范围的属性 cxt.getApplication().put("counter", counter); //通过ActionContext设置request范围的属性 cxt.getSession().put("user", getMyname()); if(getMyname().equals("scott")&&getPassword().equals("tiger")){ //通过ActionContext设置request范围的属性 cxt.put("tip", "服务器提示:你已经成功登陆"); return SUCCESS; }else{ cxt.put("tip", "服务器提示:登陆失败"); return ERROR; } } }
第三步:配置sturts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="LoginPage" extends="struts-default"> <action name="Login" class="com.Login"> <result name="success">/welcome.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
第四步:登陆成功界面jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
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 'welcome.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>
本站访问次数为:${applictionScope.counter }<br/>
${sessionScope.user },你已经登录
${requestScope.tip }
<br/>
<s:property value="#attr.counter"/><br/>
<s:property value="#attr.user"/><br/>
<!-- 因为是requset,所以可以直接取值不用#attr -->
<s:property value="tip"/>
</body>
</html>
注意:上面是两种取值方法
相关文章推荐
- 当页面是本地页面时,通过ajax访问tomcat里的action,传递的参数在action里并不能识别
- 如何在HTML页面,访问request 对象里边传递的参数
- JSP中页面向Action传递参数的几种方式
- 在页面上用action传递参数到后台出现乱码的解决方法
- Spring MVC中action接收从页面传递过来的参数方式
- 在页面上用action传递参数到后台乱码解决办法
- JS统计页面访问次数并传递可选参数
- 在页面上用action传递参数到后台出现乱码的解决方法
- jsp页面传递参数到action中的几种方法
- struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
- Strut2在Action-Result的配置文件内转到jsp页面时用URL传递参数
- JSP中页面向Action传递参数的几种方式
- 在页面上用action传递参数到后台出现乱码的解决方法
- JSP中页面向Action传递参数的几种方式
- struts2的Action从页面获取传递的参数的三种方法
- 【 jsp】页面和Action 之间传递参数的两种方式
- JavaScript跳转页面/Action并传递中文参数[解决js超链接传递过程中产生的中文乱码问题]
- 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
- ssh框架总结action接收参数的三种方式页面访问的方式
- JAVAEE中从JSP页面向Action传递中文参数