Form中使用DispatchAction 響應

dadidehaizi發表於2020-04-07

                                    Form中使用Dispatch<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

程式工作流程:在index.jsp頁面中輸入使用者資訊。為了驗證不同的方法,有兩個formAction響應後轉到show.jsp頁面顯示。

 

關鍵點:一,在struts-config.xml中配置parameter屬性。它就是ActionForm中對應那個決定函式名稱的變數;二在ActionForm中宣告一個變數,它代表函式的名字;三:在form中給這個變數賦值,決定這個formAction中的哪個函式響應。

 

1 index.jsp檔案

<%@ page language="java" contentType="text/html; charset=gb2312"%>

<html>

<head>

<title>入基本資訊</title>

</head>

<body >

<center>

       <form action="UserInfoAction.do" method="post">//不要再這裡給function賦值

       <table width="480" border="1">

              <tr>

                     <td align="center"><h1>入基本資訊</h1></td>

              </tr>

              <tr>

                     <td align="center">名字:<input type="text" name="name"></td>

              </tr>

              <tr>

                     <td align="center">:<input type="text" name="age"></td>

              </tr>

              <tr>

                     <td align="center">:<input type="text" name="sex"></td>

              </tr>

              <tr>

                     <td align="center"><input type="hidden" name="function" value="select"></td>

              </tr>

              <tr>

                     <td align="center"><input type="submit" value="提交"></td>

              </tr>        

       </table>

       </form>

       <br><hr><br>

       <form action="UserInfoAction.do" method="post">

       <table width="480" border="1">

              <tr>

                     <td><img src="imgs/1.jpg"/></td>

              </tr>

              <tr>

                     <td align="center"><h1>入基本資訊</h1></td>

              </tr>

              <tr>

                     <td align="center">名字:<input type="text" name="name"></td>

              </tr>

              <tr>

                     <td align="center">:<input type="text" name="age"></td>

              </tr>

              <tr>

                     <td align="center">:<input type="text" name="sex"></td>

              </tr>

              <tr>

<td align="center">

<input type="hidden" name="function" value="show">

</td><--在這裡給function賦值,決定呼叫哪個函式-->

              </tr>

              <tr>

                     <td align="center"><input type="submit" value="提交"></td>

              </tr>        

       </table>

       </form>

</center>

</body>

</html>

 

2 show.jsp檔案

<%@ page language="java" contentType="text/html; charset=gb2312"%>

<%@ page import="com.form.UserInfoForm"%>

<html>

<head>

<title>示用資訊</title>

</head>

<body>

<%UserInfoForm user = (UserInfoForm)request.getAttribute("UserInfoForm");%>

<center>

<table width="480" border="1">

              <tr>

                     <td align="center"><h1>出基本資訊</h1></td>

              </tr>

              <tr>

                     <td align="center">名字:<%=user.getName()%></td>

              </tr>

              <tr>

                     <td align="center">:<%=user.getAge()%></td>

              </tr>

              <tr>

                     <td align="center">:<%=user.getSex()%></td>

              </tr>

</table>

</center>

</body>

</html>

 

3 form檔案

package com.form;

 

import org.apache.struts.action.ActionForm;

 

public class UserInfoForm extends ActionForm

{

       String name = "name";

       String age  = "0";

       String sex  = "sex";

       String function = "select";

       public String getFunction() {

              return function;

       }

       public void setFunction(String function) {

              this.function = function;

       }

       public String getName() {

              return name;

       }

       public void setName(String name) {

              this.name = name;

       }

       public String getAge() {

              return age;

       }

       public void setAge(String age) {

              this.age = age;

       }

       public String getSex() {

              return sex;

       }

       public void setSex(String sex) {

              this.sex = sex;

       }     

}

 

4 Action檔案

package com.action;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.actions.DispatchAction;

 

public class UserInfoAction extends DispatchAction

{

              public ActionForward select(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)

              {

                            return mapping.findForward("show");

              }

              public ActionForward show(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)

              {

                            return mapping.findForward("show");

              }

}

 

5 struts-config.xml檔案

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

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

<struts-config>

       <form-beans>

              <form-bean name="UserInfoForm" type="com.form.UserInfoForm" />

       </form-beans>

       <global-exceptions />

       <global-forwards />

       <action-mappings>

              <action path="/UserInfoAction" type="com.action.UserInfoAction" name="UserInfoForm" scope="request" parameter="function">

                     <forward name="show" path="/pages/show.jsp"/>

              </action>

       </action-mappings>

       <controller />

</struts-config>

 

相關文章