AdSense

網頁

2021/1/31

Struts 1 使用ActionForm Bean傳送表單資料

Struts 1將表單(<form>)資料轉為ActionFrom bean物進傳送到Action

參考Eclipse 建立Struts 1專案進行設定,本範例依此進行修改。

本範例將index.jsp的表單form資料經Struts 1 ActionServlet轉為HelloForm bean傳送到接收的HelloAction類,然後forward至hello.jsp

+-------------+                      +-------------+              +-----------+
|  index.jsp  |                      |             |              |           |
| +---------+ |------------------+-->| HelloAction |---forward--->| hello.jsp |
| |/hello.do| |   +-----------+  |   |             |              |           |
| | <form>  |-----| HelloForm |--+   +-------------+              +-----------+
| +---------+ |   +-----------+
+-------------+

strut-config.xml設定<form-bean>com.abc.demo.action.form.HelloForm,並設定<action>name<form-bean>的名稱。當request經ActionServlet轉送到對應的<action>com.abc.demo.action.HelloAction時會生成HelloForm的實例並將表單欄位的值塞入同名稱的屬性中。

strut-config.xml

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

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

<struts-config>

    <form-beans>
        <form-bean name="helloForm"
            type="com.abc.demo.action.form.HelloForm" />
    </form-beans>

    <global-exceptions>
    </global-exceptions>

    <global-forwards>
    </global-forwards>

    <action-mappings>
        <action path="/hello"
            type="com.abc.demo.action.HelloAction" 
            name="helloForm">
            <forward name="hello"
                path="/WEB-INF/pages/hello.jsp" />
        </action>
    </action-mappings>

</struts-config>

com.abc.demo.action.form.HelloForm為一個簡單的POJO,要繼承Struts 1的ActionForm類。index.jsp 表單的輸入欄位的值會設定到HelloForm同名稱的屬性變數中,然後傳入HelloAction.execute()

HelloForm

package com.abc.demo.action.form;

import org.apache.struts.action.ActionForm;

public class HelloForm extends ActionForm {
    private static final long serialVersionUID = 1L;
    
    private String name;
    private String greeting;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getGreeting() {
        return greeting;
    }
    
    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }

}

index.jsp<form>action為表單提交的request路徑,method為http request method。其中有兩個<input>輸入欄位,name屬性名稱對映到HelloForm的同名稱屬性。

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
    <form name="helloForm" action="/struts1-demo/hello.do" method="post">
        name:<input type="text" name="name"/><br>
        greeting:<input type="text" name="greeting"/><br>
        <input type="submit"/>
    </form>
</body>
</html>

HelloAction.execute()的參數ActionForm實際是HelloForm,轉型後可從中取得屬性,然後放入request的attribute轉交給hello.jsp

HelloAction

package com.abc.demo.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.abc.demo.action.form.HelloForm;

public class HelloAction extends Action {

    @Override
    public ActionForward execute(
            ActionMapping mapping, 
            ActionForm form, 
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        System.out.println("HelloAction.execute()...");
        
        String name = ((HelloForm) form).getName();
        String greeting = ((HelloForm) form).getGreeting();
        
        System.out.println("name=" + name);
        System.out.println("greeting=" + greeting);
        
        request.setAttribute("name", name);
        request.setAttribute("greeting", greeting);

        return mapping.findForward("hello");
    }

}

hello.jsp收到由HelloAction轉送來的request並透過JSP EL表達式取出。

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
    <h1>Hello ${name}, ${greeting}.</h1>
</body>
</html>

啟動專案後打開瀏覽器輸入http://localhost:8080/struts1-demo進入首頁index.jsp,並在表單填入內容如下。



點選提交按鈕結果如下。




範例專案目錄結構如下。




沒有留言:

AdSense