`
wanglihu
  • 浏览: 917466 次
  • 性别: Icon_minigender_1
  • 来自: 黑龙江
社区版块
存档分类

struts2驱动模型的使用(Action类实现ModelDriven接口)

阅读更多
struts2驱动模型的使用实例:
1.modelDriven.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>模型驱动类测试</title>
  </head>
  
  <body>
    <s:form action="modeldriven.action" >
  		<s:textfield name="username" label="username"></s:textfield>
  		<s:password name="password" label="password"></s:password>
  		<s:password name="repassword" label="repassword"></s:password>
  		<s:textfield name="age" label="age"></s:textfield>
  		<s:textfield name="birthday" label="birthday"></s:textfield>
  		<s:textfield name="graduation" label="graduation"></s:textfield>
  		<s:submit value="submit"></s:submit>
  		<s:reset value="reset"></s:reset>
  	</s:form>
  </body>
</html>

2.modelDrivenSuccess.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>模型驱动类结果页面</title>
  </head>
  
  <body>
   <table align="center" border=1 width="50%">
    		<tr>
    			<td>username</td>
    			<td>${requestScope.username }</td>
    		</tr>
    		<tr>
    			<td>password</td>
    			<td>${requestScope.password }</td>
    		</tr>
    		<tr>
    			<td>repassword</td>
    			<td>${requestScope.repassword }</td>
    		</tr>
    		<tr>
    			<td>age</td>
    			<td>${requestScope.age }</td>
    		</tr>
    		<tr>
    			<td>birthday</td>
    			<td>${requestScope.birthday }</td>
    		</tr>
    		<tr>
    			<td>graduation</td>
    			<td>${requestScope.graduation }</td>
    		</tr>
    </table>
  </body>
</html>

3.User.java(POJO)
package com.hitsoft.model;
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;
	}
	
}


4.ModelDrivenAction类
package com.hitsoft.action;
import com.hitsoft.model.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
//Preparable是在execute()方法调用之前执行的
@SuppressWarnings("unchecked")
public class ModelDrivenAction extends ActionSupport implements ModelDriven<User>,Preparable{
	private User user = new User();
	public User getModel() {
		return user;
	}
	
	public String execute(){
		System.out.println("execute invoked!");
		return SUCCESS;
	}

	public void prepare() throws Exception {
		System.out.println("prepare invoked!");
	}
}

5.struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="struts2"  extends="struts-default">
	<action name="modeldriven" class="com.hitsoft.action.ModelDrivenAction" >
		<result name="success">/modelDrivenSuccess.jsp</result>
		<result name="input">/modelDriven.jsp</result>
	</action>
    </package>
</struts>

6.访问地址:
http://localhost:8080/struts2/modelDriven.jsp


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics