struts2学习笔记十九(第19讲.Struts2深入探索 续)
<include file="struts1.xml"></include><include file="struts2.xml"></include>
??然后删掉这两个文件。
二、模型驱动
还是以注册功能为例。
1、在com.test.bean下创建一个javabean:User.java
package com.test.bean;import java.util.Date;public class User {private String username;private String password;private String repassword;private int age;private Date birthday;private Date graduation;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getRepassword() {return repassword;}public void setRepassword(String repassword) {this.repassword = repassword;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public Date getGraduation() {return graduation;}public void setGraduation(Date graduation) {this.graduation = graduation;}}
?
2、在com.test.action文件夹下创建action类文件:RegisterAction2.java
package com.test.action;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.opensymphony.xwork2.Preparable;import com.test.bean.*;public class RegisterAction2 extends ActionSupport implements ModelDriven<User>,Preparable{private User user = new User();public User getModel() {return user;}public void prepare() throws Exception {System.out.println("Hello World!!!");//此方法最先执行}@Overridepublic String execute() throws Exception {return SUCCESS;}}
主要介绍ModelDriven的使用方法
?
3、验证时注意修改struts.xml中action的请求:
<action name="register" name="code"><s:token name="hello"></s:token>
?
?2、struts.xml文件里添加:
<result name="invalid.token">/register2.jsp</result>
???<interceptor-ref name="token"></interceptor-ref>
???<interceptor-ref name="defaultStack"></interceptor-ref>
如下:
<action name="register" method="test"><result name="success">/success.jsp</result><result name="input">/register2.jsp</result><result name="invalid.token">/register2.jsp</result><interceptor-ref name="token"></interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref>
?
?验证结果:
?
?
?
?
?
?
?
?
?
?