您的位置:首页 > 编程语言 > Java开发

Struts2 官方文档使用—Processing Forms

2016-06-14 12:03 399 查看
Reference:《ST2.NO.0005 struts2.0 官网文档学习笔记之五 - Processing Forms》《Struts 2 的拦截器(一)》直接贴代码:一个注册的过程PersonBean.java
1: package test;
2:
3: public class PersonBean implements java.io.Serializable {
4:     private static final long serialVersionUID = -5007279129818462704L;
5:     private String firstName;
6:     private String lastName;
7:     private String email;
8:     private int age;
9:
10:     public String getFirstName() {
11:         return firstName;
12:     }
13:
14:     public void setFirstName(String firstName) {
15:         this.firstName = firstName;
16:     }
17:
18:     public String getLastName() {
19:         return lastName;
20:     }
21:
22:     public void setLastName(String lastName) {
23:         this.lastName = lastName;
24:     }
25:
26:     public String getEmail() {
27:         return email;
28:     }
29:
30:     public void setEmail(String email) {
31:         this.email = email;
32:     }
33:
34:     public int getAge() {
35:         return age;
36:     }
37:
38:     public void setAge(int age) {
39:         this.age = age;
40:     }
41:
42:     public String toString() {
43:         return "First Name: " + getFirstName() + " Last Name:  "
44:                 + getLastName() + " Email:      " + getEmail() + " Age:      "
45:                 + getAge();
46:     }
47: }
RegisterAction.java
1: package test;
2: 
3: import com.opensymphony.xwork2.ActionSupport;
4: 
5: public class RegisterAction extends ActionSupport {
6: 
7:     private static final long serialVersionUID = -726538404572102944L;
8:     private PersonBean personBean;
9: 
10:     public String execute() {
11:         /* Call other Service class to store personBean's state in database */
12:         return SUCCESS;
13:   }
14: 
15:     public void setPersonBean(PersonBean personBean) {
16:         this.personBean = personBean;
17:   }
18: 
19:     public PersonBean getPersonBean() {
20:         return this.personBean;
21:   }
22: }
Register.jsp
1: <%@ page language="java" contentType="text/html; charset=utf-8"
2:   pageEncoding="utf-8"%>
3: <%@ taglib prefix="s" uri="/struts-tags"%>
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=utf-8">
8: <title>register</title>
9: </head>
10: <body>
11:     <s:form action="register">
12:         <s:textfield name="personBean.firstName" label="First name" />
13:       <s:textfield name="personBean.lastName" label="Last name" />
14:         <s:textfield name="personBean.email" label="Email" />
15:         <s:textfield name="personBean.age" label="Age" />
16:         <s:submit />
17:   </s:form>
18: </body>
19: </html>
我理解的过程:JSP文件中<s:textfield name="personBean.firstName" label="First name" />这类代码首先会在RegisterAction里面查找一个叫personBean的对象,会调用到action里面的getPersonBean()方法,显然这个时候this.personBean为null,我的猜想是在发现为null的时候会去调用setPersonBean,使用setPersonBean()的参数时框架会自动用PersonBean 类的默认的构造方法来创建这个对象(这时候personBean已经就不是null了但是其全部的值,firstName之类还都是null),然后又调用getPersonBean得到了不为null的personBean对象。下一步就是查找personBean.firstName了,调用personBean里面的setFisrtName()赋了值。这个地方忘了设断点查了不过可以想象也应该和personBean一样的先调用getFirstName()发现为null(我估计不为null也会调用后面的setter),然后调用setFirstName()给丫赋了一值。 这些全部是发生在execute()方法执行之前的哦!Thanks to Struts2的默认拦截器。在注册成功之后会跳转到Thankyou.jspstruts.xml片段
1: <action name="register" class="test.RegisterAction"
2:           method="execute">
3:             <result name="success">/test/Thankyou.jsp</result>
4:         </action>
Thankyou.jsp
1: <%@ page language="java" contentType="text/html; charset=UTF-8"
2:   pageEncoding="UTF-8"%>
3: <%@taglib prefix='s' uri='/struts-tags'%>
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=UTF-8">
8: <title>Registration Successful</title>
9: </head>
10: <body>
11:     <h3>Thank you for registering for a prize.</h3>
12:     <p>
13:       Your registration information:
14:         <s:property value="personBean" />
15:     </p>
16:     <p>
17:       <a href="<s:url action='index' />"<Return to home page</a>.
18:     </p>
19: </body>
20: </html>
这个里面<s:property value="personBean" />又通过get得到了action里面的perseonBean对象(已经都有值了哦),然后toString()输出了。
直接到这里参考:http://www.cnblogs.com/jersey/category/289906.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: