天天看點

Struts2 result中的type常用的四種類型

dispatcher:result type預設的類型,相當于servlet的foward方式跳轉頁面。用戶端看到的是struts2中配置的位址,而不是真正頁面的位址,一般用于跳轉到jsp頁面,頁面能拿到值

redirect:頁面重定向,用戶端跳轉,資料全部丢失,位址欄發生變化,頁面不能拿到值

chain:将請求轉發給一個Action,Action能通過getAttribute(“uname”)拿到值

redirect-action:一般用于跳轉到Action,Action不能通過getAttribute(“uname”)拿到值

前兩種的xml action的配置如下(傳值給jsp):

後兩種的xml action的配置如下(傳值給Action):

success.jsp代碼如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
   isELIgnored="false" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags"  prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:debug></s:debug>
<!--值在ValueStack的值中,是以直接拿,注意實體類的屬性和下面的value還有表單的input的name保持一緻,-->
<h3>從debug的值棧中拿值:</h3><br>
使用者名:<s:property value="uname"/>=======密碼:<s:property value="upwd"/>
<!--通過四種跳轉方式拿值  -->
<h3>通過四種跳轉方式拿值:</h3><br>
<!-- 使用EL表達式記得在頁面的開始寫上isELIgnored="false" -->
使用者名:${request.uname}=======密碼:${request.upwd}
</body>
</html>
           

用戶端login.jsp代碼如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="LoginAction" method="post">
  使用者名:<input type="text" name="uname"><br>
    密碼:<input type="password" name="upwd"><br>
    <input type="submit" value="登入">
</form>
</body>
</html>
           

LoginAction:接受用戶端(login.jsp)的資料代碼如下:

package com.zs.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

    private String uname;
    private String upwd;
    private HttpServletRequest request;
    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUpwd() {
        return upwd;
    }

    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }

    public String test() {
        //測試響應到頁面的幾種請求方式
        request=ServletActionContext.getRequest();
        request.setAttribute("uname", uname);
        request.setAttribute("upwd", upwd);
        System.out.println("uname:"+uname+"upwd:"+upwd);
        return SUCCESS;
    }
}
           

TestAction測試後兩種方式的Action代碼如下:

package com.zs.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport{
    private HttpServletRequest request;
    public String test() {
        request=ServletActionContext.getRequest();
        //測試redirect-action這種方式是否拿到資料
        System.out.println("uname:"+request.getAttribute("uname")+"=====upwd:"+request.getAttribute("upwd"));
        return NONE;
    }

}
           

說在最後的話:編寫實屬不易,若喜歡或者對你有幫助記得點贊+關注或者收藏哦~

繼續閱讀