Struts 2.X of Eclipse for J2EE – 靜態互動

Struts 系列文章第四篇。這篇主要進入前端資料送到後端的初階方式,還沒有到處理完再回送到網頁的部份;就簡單在網頁打個字、按鈕、送到 Server 端顯示文字、同時轉到成功頁面。

一樣、先上程式碼;如果有要補充的再等下篇。

  • 基本的檔案需求及位置。

  • CustomerController.java
package com.test.controller;

public class CustomerController {

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

    private String customerName;

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String customerSave(){
        System.out.println(customerName);
        return "OK";
    }

}

  • struts.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="customer" extends="struts-default" namespace="/customers">
        <action name="customerform" class="com.test.controller.CustomerController" method="execute">
            <result name="success">
                /customers/customersform.html
            </result>
        </action>
        <action name="customersave" class="com.test.controller.CustomerController" method="customerSave">
            <result name="OK" type="dispatcher">
                /customers/customerssave.html
            </result>
        </action>
    </package>

</struts>
  • customersform.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>customersform</title>
</head>
<body>
    <form method="post" action="customersave.action">
        <h1>Name Space</h1>
        <input type="text" name="customerName" />
        <input type="submit" value="submit" />
    </form>
</body>
</html>
  • customerssave.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SAVE OK</title>
</head>
<body>
    <h1>SAVE OK!</h1>
</body>
</html>
  • 成果。

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *