Action訪問Servlet API 在頁面間傳遞參數

來源:互聯網
上載者:User

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;</p><p>import com.opensymphony.xwork2.ActionContext;<br />import com.opensymphony.xwork2.ActionSupport;</p><p>public class Login extends ActionSupport{<br />private String myname;<br />private String password;<br />public String getMyname() {<br />return myname;<br />}<br />public void setMyname(String myname) {<br />this.myname = myname;<br />}<br />public String getPassword() {<br />return password;<br />}<br />public void setPassword(String password) {<br />this.password = password;<br />}<br />public String execute() {<br />ActionContext cxt = ActionContext.getContext();<br />Integer counter = (Integer)cxt.getApplication().get("counter");<br />if(counter==null){<br />counter=1;<br />}else {<br />counter+=1;<br />}<br />//System.out.println(counter);<br />//通過ActionContext設定application範圍的屬性<br />cxt.getApplication().put("counter", counter);<br />//通過ActionContext設定request範圍的屬性<br />cxt.getSession().put("user", getMyname());<br />if(getMyname().equals("scott")&&getPassword().equals("tiger")){<br />//通過ActionContext設定request範圍的屬性<br />cxt.put("tip", "伺服器提示:你已經成功登陸");<br />return SUCCESS;<br />}else{<br />cxt.put("tip", "伺服器提示:登陸失敗");<br />return ERROR;<br />}</p><p>}</p><p>}<br />

 

第三步:配置sturts.xml

<?xml version="1.0" encoding="UTF-8" ?><br /><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><br /><struts><br /><package name="LoginPage" extends="struts-default"><br /><action name="Login" class="com.Login"><br /><result name="success">/welcome.jsp</result><br /><result name="error">/error.jsp</result><br /></action><br /></package></p><p></struts><br />

 

第四步:登陸成功介面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>
注意:上面是兩種取值方法

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.