您的位置:首页 > 其它

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类

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>
注意:上面是两种取值方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐