Action访问Servlet API 在页面间传递参数

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>
注意:上面是两种取值方法

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值