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

Struts2 官方文档学习—Processing Forms

2011-03-25 13:14 225 查看
Reference:《ST2.NO.0005 struts2.0 官网文档学习笔记之五 - Processing Forms》《Struts 2 的拦截器(一)

直接贴代码:一个注册的过程

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()输出了

 

 

以上理解如有不正之处 还望指出
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: