Form中使用Dispatch
程序工作流程:在index.jsp页面中输入用户信息。为了验证不同的方法,有两个form。Action响应后转到show.jsp页面显示。
关键点:一,在struts-config.xml中配置parameter属性。它就是ActionForm中对应那个决定函数名称的变量;二在ActionForm中声明一个变量,它代表函数的名字;三:在form中给这个变量赋值,决定这个form有Action中的哪个函数响应。
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>