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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值