struts2学习笔记十九(第19讲.Struts2深入探索 续)

Struts2应用开发详解 第十九讲 Struts2 深入探索 续

一、分模块开发

在src目录下创建struts1.xml和struts2.xml文件,然后在struts.xml文件中引用这两个文件:

在struts.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,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的请求:


           /success.jsp
           /register2.jsp

 验证结果先输出Hello world!!!

 

三、 表单重复提交

 还是以注册功能为例。

1、在register2.jsp页面form表单中添加标签:

 

 2、struts.xml文件里添加:

/register2.jsp
   
   

如下:


			/success.jsp
			/register2.jsp
			/register2.jsp
			
			
			

 

 验证结果:


struts2学习笔记十九(第19讲.Struts2深入探索 续)_第1张图片
 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(学习笔记,java)