2017年2月19日 星期日

JAVA-Struts2 入門初體驗 ( 四 )__參數傳遞__Jsp to Action

在上述幾篇講到有關如何建置 Strut2 的專案
此篇會提到要如何 收 / 送 資料

我們先來講要如何把頁面資料送至後端處理,方法:
1 . 用網頁標籤內的 name 來對應到 Action 內的 Value ( get / set )
2 . 用網頁標籤內的 name 來對應到 Action 內的 Bean  ( get / set )
3 . 用網頁的表單方式統捆起來後 Action 實作 Module( form get/set )

簡單分析以上幾種方法
1 . 如果用第一種,之後 Action 一定看起來凌亂不堪,但初學好入手。
2 . 第二與第三差別在於頁面宣告的長度

以下我們就針對以上方法寫下範例:
1 . 用一般參數 set/get
仔細看 Action 部分,如果只有兩個參數還好,如果是十幾個?
整體偏程式碼都被 set /get 塞爆了
hello.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>
    <!-- 皆可使用,二選一
    <form action="hello" method="post" name="form">
        姓名: <input type="text" name="userName" /><br/>
        電話: <input type="text" name="userPhone" /><br/> 
        <input type="submit" value="提交"/><br/> 
    </form>
     -->
    <!-- struts 標籤 </form>
    <form action="hello" method="post" name="form">
        姓名: <s:textfield name="userName"/><br/>
        電話: <s:textfield name="userPhone"/><br/>
        <s:submit value="提交"/> <br/> 
    </form>
    
</body>
</html>

Action.java
說明:Jsp 的標籤名稱對應在 Action 內,並且設置 Set / Get
package com.brian.example.action;

public class HelloAction {
    private String userName;
    private String userPhone;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    .( userPhone set/get )
    .
    .
    .

    public String execute() {
        System.out.println(getUserName() + "," + getUserPhone());
        return "success";
    }

}


2 . 用 Bean 來
<%@ 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>
    
    <form action="hello" method="post">
        姓名: <s:textfield name="user.userName"/><br/>
        電話: <s:textfield name="user.userPhone"/><br/>
        <s:submit value="提交"/> <br/> 
    </form>
    
</body>
</html>

User.java
說明:
package com.brian.example.action;

public class User {
    private String userName;
    private String userPhone;
    
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    public String getUserPhone() {
        return userPhone;
    }
    public void setUserPhone(String userPhone) {
        this.userPhone = userPhone;
    }
    
}

Action.java
說明:
package com.brian.example.action;

public class HelloAction {

    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String execute() {
        try {
            if(user.getUserName() != null || user.getUserPhone() != null) {
                System.out.println(user.getUserName() + " : " + user.getUserPhone() );
            }
        } catch (Exception e ) {
            System.out.println(e.toString());
        }
        
        return "success";
    }

}


3 . Module
<%@ 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>
    
    <form action="hello" method="post" name="user">
        姓名: <s:textfield name="userName"/><br/>
        電話: <s:textfield name="userPhone"/><br/>
        <s:submit value="提交"/> <br/> 
    </form>
    
</body>
</html>

User
同上方法 User.java

Action
package com.brian.example.action;

import com.opensymphony.xwork2.ModelDriven;

public class HelloAction implements ModelDriven<User> {

    private User user = new User();

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public User getModel() {

        return user;
    }

    public String execute() {
        try {
            if (user.getUserName() != null || user.getUserPhone() != null) {
                System.out.println(user.getUserName() + " : " + user.getUserPhone());
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return "success";
    }

}



參考資料:Struts2的三种传值方式比较(附demo)
參考資料:struts2 actions - Multiple Methods & Multiple classes

沒有留言:

張貼留言