Struts2的幾種傳參方式

第一種:(EL表達式傳參)
***UserAction.java***
package org.zttc.itat.action;
public class UserAction {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String addInput(){
        return "success";
    }

    public String add(){
        System.out.println("add");
        return "r_list";
    }

    public String list(){
        //此處進行數據設置JSP頁面中可以通過EL表達式獲取
        this.setUsername("張三");
        this.setPassword("123");
        return "success";
    }
}

***list.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>
<h1>User list</h1>
${username}--${password}
</body>
</html>
第二種:(Struts自帶標籤 注:在jsp中加入標籤taglib)
***list.jsp***
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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>
<h1>User list</h1>
<s:property value="username"/>---<s:property value="password"/>
</body>
</html>
第三種:(通過ActionContext.getContext().put(String,String)方式)
***UserAction.java***
package org.zttc.itat.action;
import com.opensymphony.xwork2.ActionContext;
public class UserAction {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String addInput(){
        return "success";
    }

    public String add(){
        System.out.println("add");
        return "r_list";
    }

    public String list(){
        ActionContext.getContext().put("aaa", 123);
        ActionContext.getContext().put("bbb", 456);
        ActionContext.getContext().put("ccc", "678");
        return "success";
    }

}


***list.jsp***
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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>
<h1>User list</h1>
${aaa}---${bbb}---${ccc}
<!-- 使用s:property來訪問actionContext中的數據都需要加#,在struts2.3之後,如果ActionContext中的數據
是String的類型就不用加#,但是在將來的開發中,只要是ActionContext中的數據一定加個#訪問,所以下面也可以是value="#ccc" -->
<s:property value="#aaa"/>---<s:property value="#bbb"/>---<s:property value="ccc"/>
</body>
</html>
第四種:(通過servlet的API傳值方式)
***UserAction.java***
package org.zttc.itat.action;
import org.apache.struts2.ServletActionContext;
public class UserAction {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String addInput(){
        return "success";
    }

    public String add(){
        System.out.println("add");
        return "r_list";
    }

    public String list(){
        ServletActionContext.getRequest().setAttribute("aaa", "123");
        return "success";
    }

}


***list.jsp***
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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>
<h1>User list</h1>
<s:property value="#request.aaa"/>
</body>
</html>
第五種(前端form表單傳給後臺)
在url中帶參數
http://localhost:8090/struts2/User_addInput.action?username=abcd&password=123

後臺action必須帶有getter setter的方法就自動獲取
***UserAction.java***
package org.zttc.itat.action;
import org.apache.struts2.ServletActionContext;
public class UserAction {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String addInput(){
        System.out.println(username+","+password);
        return "success";
    }

    public String add(){
        System.out.println("add");
        return "r_list";
    }

    public String list(){
        ServletActionContext.getRequest().setAttribute("aaa", "123");
        return "success";
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章