Struts2 官方文档学习—Processing Forms
2011-03-25 13:14
225 查看
Reference:《ST2.NO.0005 struts2.0 官网文档学习笔记之五 - Processing Forms》《Struts 2 的拦截器(一)》
直接贴代码:一个注册的过程
PersonBean.java
Register.jsp
我理解的过程:
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.jsp
struts.xml片段
Thankyou.jsp
这个里面<s:property value="personBean" />又通过get得到了action里面的perseonBean对象(已经都有值了哦),然后toString()输出了
以上理解如有不正之处 还望指出
直接贴代码:一个注册的过程
PersonBean.java
package test;
public class PersonBean implements java.io.Serializable {
private static final long serialVersionUID = -5007279129818462704L;
private String firstName;
private String lastName;
private String email;
private int age;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "First Name: " + getFirstName() + " Last Name:"
+ getLastName() + " Email:" + getEmail() + " Age:"
+ getAge();
}
}
RegisterAction.java
package test;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private static final long serialVersionUID = -726538404572102944L;
private PersonBean personBean;
public String execute() {
/* Call other Service class to store personBean's state in database */
return SUCCESS;
}
public void setPersonBean(PersonBean personBean) {
this.personBean = personBean;
}
public PersonBean getPersonBean() {
return this.personBean;
}
}
Register.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>register</title>
</head>
<body>
<s:form action="register">
<s:textfield name="personBean.firstName" label="First name" />
<s:textfield name="personBean.lastName" label="Last name" />
<s:textfield name="personBean.email" label="Email" />
<s:textfield name="personBean.age" label="Age" />
<s:submit />
</s:form>
</body>
</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.jsp
struts.xml片段
<action name="register" class="test.RegisterAction"
method="execute">
<result name="success">/test/Thankyou.jsp</result>
</action>
Thankyou.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix='s' uri='/struts-tags'%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration Successful</title>
</head>
<body>
<h3>Thank you for registering for a prize.</h3>
<p>
Your registration information:
<s:property value="personBean" />
</p>
<p>
<a href="<s:url action='index' />"<Return to home page</a>.
</p>
</body>
</html>
这个里面<s:property value="personBean" />又通过get得到了action里面的perseonBean对象(已经都有值了哦),然后toString()输出了
以上理解如有不正之处 还望指出
相关文章推荐
- TensorFlow官方文档学习|TensorFlow运作方式入门
- ActionBar(官方文档学习续(二)。。)Adding and Handling Actions
- 阅读Oracle官方文档一步步学习Oracle知识的正确顺序
- OpenCV官方文档学习记录(7)
- tkinter官方文档学习笔记
- Libev 官方文档学习笔记 - 01:概述和 ev_loop
- Jersey 2.22.2 官方文档第18章学习笔记
- ES权威指南[官方文档学习笔记]-5---talking to elasticsearch
- Spring Boot 官方文档学习(一)入门及使用
- ES权威指南[官方文档学习笔记]-14 phrase search
- ES权威指南[官方文档学习笔记]-31 Retrieving a document
- ES权威指南[官方文档学习笔记]-15 highlighting our searches
- Spring Boot 官方文档学习
- Spring 4 官方文档学习(⑥)Web MVC 框架之配置Spring MVC
- ES权威指南[官方文档学习笔记]-51 Multi-index,multi-type
- Spark学习之路 (二十三)SparkStreaming的官方文档
- OpenCV官方文档学习记录(17)
- Android Geofence的学习(二)继续翻译官方文档
- 学习Oracle7的OCI(Oracle Call Interface)时,翻译了一部分Oracle7的OCI官方文档
- Spring 4 官方文档学习(十一)Web MVC 框架之multipart(文件上传)支持