首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

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

2012-08-08 
struts2驱动模型的使用(Action类实现ModelDriven接口)struts2驱动模型的使用实例:1.modelDriven.jsp%@ pa

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" ><result name="success">/modelDrivenSuccess.jsp</result><result name="input">/modelDriven.jsp</result></action>    </package></struts>

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


热点排行