struts2学习笔记十九(第19讲.Struts2深入探索 续)
Struts2应用开发详解 第十九讲 Struts2 深入探索 续
一、分模块开发
在src目录下创建struts1.xml和struts2.xml文件,然后在struts.xml文件中引用这两个文件:
在struts.xml文件中添加include属性:
<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!!!");//此方法最先执行
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
主要介绍ModelDriven的使用方法
3、验证时注意修改struts.xml中action的请求:
<action name="register" class="com.test.action.RegisterAction2"> <result name="success">/success.jsp</result> <result name="input">/register2.jsp</result> </action>
验证结果先输出Hello world!!!
三、 表单重复提交
还是以注册功能为例。
1、在register2.jsp页面form表单中添加标签:
<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" class="com.test.action.RegisterAction" 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>
验证结果: