struts中动态表单(DynaActionForm)的使用(超简单例子)
主要功能是在页面上输入username和password提交,然后在另一个页面显示。
1.所需要的包就是struts的lib包;
2.所涉及道德文件有:web.xml;struts_config.xml;两个页面(index.jsp、dyna_actionform.jsp);一个action(DynaAction.java);
3.这种动态的DynaActionForm是将Form继续传递。违背了分层的规则(少用);
web.xml 这个文件没什么的别的。
1
<?
xml version="1.0" encoding="UTF-8"
?>
2 < web-app version ="2.5"
3 xmlns ="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee
6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
7 < welcome-file-list >
8 < welcome-file > index.jsp </ welcome-file >
9 </ welcome-file-list >
10
11 < servlet >
12 < servlet-name > action </ servlet-name >
13 < servlet-class > org.apache.struts.action.ActionServlet </ servlet-class >
14 <!-- Default -->
15 < init-param >
16 < param-name > config </ param-name >
17 < param-value > /WEB-INF/struts-config.xml </ param-value >
18 </ init-param >
19 < init-param >
20 < param-name > debug </ param-name >
21 < param-value > 2 </ param-value >
22 </ init-param >
23 < init-param >
24 < param-name > detail </ param-name >
25 < param-value > 2 </ param-value >
26 </ init-param >
27 < load-on-startup > 2 </ load-on-startup >
28 </ servlet >
29
30 < servlet-mapping >
31 < servlet-name > action </ servlet-name >
32 < url-pattern > *.do </ url-pattern >
33 </ servlet-mapping >
34 </ web-app >
35
2 < web-app version ="2.5"
3 xmlns ="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee
6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
7 < welcome-file-list >
8 < welcome-file > index.jsp </ welcome-file >
9 </ welcome-file-list >
10
11 < servlet >
12 < servlet-name > action </ servlet-name >
13 < servlet-class > org.apache.struts.action.ActionServlet </ servlet-class >
14 <!-- Default -->
15 < init-param >
16 < param-name > config </ param-name >
17 < param-value > /WEB-INF/struts-config.xml </ param-value >
18 </ init-param >
19 < init-param >
20 < param-name > debug </ param-name >
21 < param-value > 2 </ param-value >
22 </ init-param >
23 < init-param >
24 < param-name > detail </ param-name >
25 < param-value > 2 </ param-value >
26 </ init-param >
27 < load-on-startup > 2 </ load-on-startup >
28 </ servlet >
29
30 < servlet-mapping >
31 < servlet-name > action </ servlet-name >
32 < url-pattern > *.do </ url-pattern >
33 </ servlet-mapping >
34 </ web-app >
35
struts_config.xml 动态的Form就是在这里配置的
1
<?
xml version="1.0" encoding="gb2312"
?>
2 <! DOCTYPE struts-config PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
4 "http://struts.apache.org/dtds/struts-config_1_3.dtd" >
5
6 < struts-config >
7 < form-beans >
8 < form-bean name ="dynaFrom" type ="org.apache.struts.action.DynaActionForm" >
9 < form-property name ="username" type ="java.lang.String" />
10 < form-property name ="age" type ="java.lang.Integer" />
11 </ form-bean >
12 </ form-beans >
13
14 < action-mappings >
15 < action path ="/danaform"
16 type ="com.eplugger.struts.action.DynaAction"
17 name ="dynaFrom"
18 scope ="request"
19 >
20 < forward name ="success" path ="/dyna_actionform.jsp" />
21 </ action >
22 </ action-mappings >
23 </ struts-config >
24
2 <! DOCTYPE struts-config PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
4 "http://struts.apache.org/dtds/struts-config_1_3.dtd" >
5
6 < struts-config >
7 < form-beans >
8 < form-bean name ="dynaFrom" type ="org.apache.struts.action.DynaActionForm" >
9 < form-property name ="username" type ="java.lang.String" />
10 < form-property name ="age" type ="java.lang.Integer" />
11 </ form-bean >
12 </ form-beans >
13
14 < action-mappings >
15 < action path ="/danaform"
16 type ="com.eplugger.struts.action.DynaAction"
17 name ="dynaFrom"
18 scope ="request"
19 >
20 < forward name ="success" path ="/dyna_actionform.jsp" />
21 </ action >
22 </ action-mappings >
23 </ struts-config >
24
DynaAction.java注意这个文件
1
package
com.eplugger.struts.action;
2
3 import javax.servlet.http.HttpServletRequest;
4 import javax.servlet.http.HttpServletResponse;
5
6 import org.apache.struts.action.Action;
7 import org.apache.struts.action.ActionForm;
8 import org.apache.struts.action.ActionForward;
9 import org.apache.struts.action.ActionMapping;
10 import org.apache.struts.action.DynaActionForm;
11
12 public class DynaAction extends Action {
13
14 @Override
15 public ActionForward execute(ActionMapping mapping, ActionForm form,
16 HttpServletRequest request, HttpServletResponse response)
17 throws Exception {
18 // 获取动态的ActionForm
19 DynaActionForm daf = (DynaActionForm) form;
20 // DynaActionForm中提供了get()方法,所以我们可以直接获得页面传来的值。但是他把值放到了Map中,所以我们要强制类型转换
21 String username = (String)daf.get( " username " );
22 Integer age = (Integer)daf.get( " age " );
23 // 成功转到成功页面
24 return mapping.findForward( " success " );
25 }
26
27 }
28
index.jsp
2
3 import javax.servlet.http.HttpServletRequest;
4 import javax.servlet.http.HttpServletResponse;
5
6 import org.apache.struts.action.Action;
7 import org.apache.struts.action.ActionForm;
8 import org.apache.struts.action.ActionForward;
9 import org.apache.struts.action.ActionMapping;
10 import org.apache.struts.action.DynaActionForm;
11
12 public class DynaAction extends Action {
13
14 @Override
15 public ActionForward execute(ActionMapping mapping, ActionForm form,
16 HttpServletRequest request, HttpServletResponse response)
17 throws Exception {
18 // 获取动态的ActionForm
19 DynaActionForm daf = (DynaActionForm) form;
20 // DynaActionForm中提供了get()方法,所以我们可以直接获得页面传来的值。但是他把值放到了Map中,所以我们要强制类型转换
21 String username = (String)daf.get( " username " );
22 Integer age = (Integer)daf.get( " age " );
23 // 成功转到成功页面
24 return mapping.findForward( " success " );
25 }
26
27 }
28
1
<%
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
ISO-8859-1
"
%>
2 <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
3 < html >
4 < head >
5 </ head >
6 < body >
7 < form action ="danaform.do" method ="post" >
8 username: < input type ="text" name ="username" >< br >
9 age: < input type ="text" name ="age" >< br >
10 < input type ="submit" value ="submit" >
11 </ form >
12 </ body >
13 </ html >
14
2 <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
3 < html >
4 < head >
5 </ head >
6 < body >
7 < form action ="danaform.do" method ="post" >
8 username: < input type ="text" name ="username" >< br >
9 age: < input type ="text" name ="age" >< br >
10 < input type ="submit" value ="submit" >
11 </ form >
12 </ body >
13 </ html >
14
dyna_actionform.jsp
1
<%
@ page language="java" contentType="text/html; charset=ISO-8859-1"
2 pageEncoding="ISO-8859-1" %>
3 <% @ page isELIgnored="false" %>
4 <! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
5 < html >
6 < head >
7 < meta http-equiv ="Content-Type" content ="text/html; charset=ISO-8859-1" >
8 < title > Insert title here </ title >
9 </ head >
10 < body >
11 username:${dynaFrom.map.username } < br >
12 age:${dynaFrom.map.age }
13 </ body >
14 </ html >
2 pageEncoding="ISO-8859-1" %>
3 <% @ page isELIgnored="false" %>
4 <! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
5 < html >
6 < head >
7 < meta http-equiv ="Content-Type" content ="text/html; charset=ISO-8859-1" >
8 < title > Insert title here </ title >
9 </ head >
10 < body >
11 username:${dynaFrom.map.username } < br >
12 age:${dynaFrom.map.age }
13 </ body >
14 </ html >
最后说一点动态的ActionForm的验证,通常使用动态验证框架validator。否则将失去动态ActionForm的意义。