前面了解了与ActionForm有关的流程。现在就来用应用程序测试一下那些方法的正确调用顺序。使用的应用程序是在前面工程的基础做一下修改。下面就是修改了的AddStudentForm代码:
public class AddStudentForm extends ActionForm {
private String name;
private String major;
private float score;
private Date birth;
public AddStudentForm() {
System.out.println("in AddStudentForm construtcor!");
}
public String getName() {
System.out.println("getName=" + name);
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("setName=" + this.name);
}
public String getMajor() {
System.out.println("getMajor=" + major);
return major;
}
public void setMajor(String major) {
this.major = major;
System.out.println("setMajor=" + this.major);
}
public float getScore() {
System.out.println("getScore=" + score);
return score;
}
public void setScore(float score) {
this.score = score;
System.out.println("setScore=" + this.score);
}
public Date getBirth() {
System.out.println("getBirth=" + birth);
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
System.out.println("setBirth=" + this.birth);
}
@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
System.out.println("in reset method!");
}
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
System.out.println("in validate method!");
return null;
}
}
按照如上代码修改好Form以后,再次提交请求。就一次打印出构造方法、reset方法、相应setter方法和validate方法中的打印语句。虽然第一请求不需要调用reset方法,但是struts的实现就是每次都会调用reset方法。其实有setter方法的输出语句就能说明struts调用了该方法。在这里,代码输出的是ActionForm的成员变量的值,所以一定要先赋值,然后再输出。还有一个要注意的,现在还未处理Date类型的赋值,所以最好请求中不要包括这个参数,否则服务器端会抛异常,不过也可以忽略不管。因为这个代码的目的是了解ActionForm的调用顺序。当ActionForm被填充好后,struts会将其存入相应的session或者request对象中。怎么才能证明struts的确是做了这件事呢?目前有两种方式可以采用,一个是从过程中查看,就是使用监听器来实现。另外一种是从结果中查看,即获取该属性的对象,检查相应范围内是否有该ActionForm。先从过程中来查看,下面是监听器的代码:
public class AttributeListener implements HttpSessionAttributeListener,
ServletRequestAttributeListener {
public void attributeAdded(ServletRequestAttributeEvent event) {
if (event.getValue() instanceof ActionForm) {
System.out.println("request add name=" + event.getName()
+ "/t value=" + event.getValue());
}
}
public void attributeRemoved(ServletRequestAttributeEvent event) {
if (event.getValue() instanceof ActionForm) {
System.out.println("request remove name=" + event.getName()
+ "/t value=" + event.getValue());
}
}
public void attributeReplaced(ServletRequestAttributeEvent event) {
if (event.getValue() instanceof ActionForm) {
System.out.println("request replace name=" + event.getName()
+ "/t value=" + event.getValue());
}
}
public void attributeAdded(HttpSessionBindingEvent event) {
if (event.getValue() instanceof ActionForm) {
System.out.println("session add name=" + event.getName()
+ "/t value=" + event.getValue());
}
}
public void attributeRemoved(HttpSessionBindingEvent event) {
if (event.getValue() instanceof ActionForm) {
System.out.println("session remove name=" + event.getName()
+ "/t value=" + event.getValue());
}
}
public void attributeReplaced(HttpSessionBindingEvent event) {
if (event.getValue() instanceof ActionForm) {
System.out.println("session replace name=" + event.getName()
+ "/t value=" + event.getValue());
}
}
}
写好监听器,就应该在web.xml中注册监听器,注册以后这个监听器就会发生作用了。在代码中只监听与ActionForm有关的的事件。部署好应用程序以后,提交和以前一样的请求。如果没有在struts的配置文件中设置该action元素的scope="request",那么就会在控制台看到如下输出语句"request add name=addStuForm value=com.yxb.struts.AddStudentForm@13e4a5a"。如果没有配置该属性或者属性值设置成session,则打印的语句是"session add...",这说明struts默认将ActionForm放入session范围内。第一提交请求后,同一个页面继续刷新页面,如果配置的session属性值,会发现打印出了"session replace..."语句,说明session范围内属性发生了替换,如果配置成request结果仍然不会变,因为对服务器来说这是两个不同的request。
public class AddStudentForm extends ActionForm {
private String name;
private String major;
private float score;
private Date birth;
public AddStudentForm() {
System.out.println("in AddStudentForm construtcor!");
}
public String getName() {
System.out.println("getName=" + name);
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("setName=" + this.name);
}
public String getMajor() {
System.out.println("getMajor=" + major);
return major;
}
public void setMajor(String major) {
this.major = major;
System.out.println("setMajor=" + this.major);
}
public float getScore() {
System.out.println("getScore=" + score);
return score;
}
public void setScore(float score) {
this.score = score;
System.out.println("setScore=" + this.score);
}
public Date getBirth() {
System.out.println("getBirth=" + birth);
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
System.out.println("setBirth=" + this.birth);
}
@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
System.out.println("in reset method!");
}
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
System.out.println("in validate method!");
return null;
}
}
按照如上代码修改好Form以后,再次提交请求。就一次打印出构造方法、reset方法、相应setter方法和validate方法中的打印语句。虽然第一请求不需要调用reset方法,但是struts的实现就是每次都会调用reset方法。其实有setter方法的输出语句就能说明struts调用了该方法。在这里,代码输出的是ActionForm的成员变量的值,所以一定要先赋值,然后再输出。还有一个要注意的,现在还未处理Date类型的赋值,所以最好请求中不要包括这个参数,否则服务器端会抛异常,不过也可以忽略不管。因为这个代码的目的是了解ActionForm的调用顺序。当ActionForm被填充好后,struts会将其存入相应的session或者request对象中。怎么才能证明struts的确是做了这件事呢?目前有两种方式可以采用,一个是从过程中查看,就是使用监听器来实现。另外一种是从结果中查看,即获取该属性的对象,检查相应范围内是否有该ActionForm。先从过程中来查看,下面是监听器的代码:
public class AttributeListener implements HttpSessionAttributeListener,
ServletRequestAttributeListener {
public void attributeAdded(ServletRequestAttributeEvent event) {
if (event.getValue() instanceof ActionForm) {
System.out.println("request add name=" + event.getName()
+ "/t value=" + event.getValue());
}
}
public void attributeRemoved(ServletRequestAttributeEvent event) {
if (event.getValue() instanceof ActionForm) {
System.out.println("request remove name=" + event.getName()
+ "/t value=" + event.getValue());
}
}
public void attributeReplaced(ServletRequestAttributeEvent event) {
if (event.getValue() instanceof ActionForm) {
System.out.println("request replace name=" + event.getName()
+ "/t value=" + event.getValue());
}
}
public void attributeAdded(HttpSessionBindingEvent event) {
if (event.getValue() instanceof ActionForm) {
System.out.println("session add name=" + event.getName()
+ "/t value=" + event.getValue());
}
}
public void attributeRemoved(HttpSessionBindingEvent event) {
if (event.getValue() instanceof ActionForm) {
System.out.println("session remove name=" + event.getName()
+ "/t value=" + event.getValue());
}
}
public void attributeReplaced(HttpSessionBindingEvent event) {
if (event.getValue() instanceof ActionForm) {
System.out.println("session replace name=" + event.getName()
+ "/t value=" + event.getValue());
}
}
}
写好监听器,就应该在web.xml中注册监听器,注册以后这个监听器就会发生作用了。在代码中只监听与ActionForm有关的的事件。部署好应用程序以后,提交和以前一样的请求。如果没有在struts的配置文件中设置该action元素的scope="request",那么就会在控制台看到如下输出语句"request add name=addStuForm value=com.yxb.struts.AddStudentForm@13e4a5a"。如果没有配置该属性或者属性值设置成session,则打印的语句是"session add...",这说明struts默认将ActionForm放入session范围内。第一提交请求后,同一个页面继续刷新页面,如果配置的session属性值,会发现打印出了"session replace..."语句,说明session范围内属性发生了替换,如果配置成request结果仍然不会变,因为对服务器来说这是两个不同的request。