现在的位置: 首页 > 综合 > 正文

ActionForm的Date处理问题

2013年08月30日 ⁄ 综合 ⁄ 共 2730字 ⁄ 字号 评论关闭

基于Struts做应用程序开发的时候,对日期数据的处理,这个是最常见的,由于Struts默认是不支持自动将网页POST的数据自动转化成Date型数据,所以,很多程序员就直接在ActionForm当中直接使用String,然后在Action当中再进行字符串跟日期数据的处理。那假如在ActionForm当中是使用Date类型呢?会出现什么问题?能用什么样的解决办法?我们先看一下在ActionForm当中会出现的问题先。首先,我们先创建一个新的Struts工程,创建一个新的ActionForm,取名叫UserForm,UserForm的实现代码如下: 程序代码

 package zizz.struts; import java.util.Date;

import org.apache.struts.action.ActionForm;

/** * UserForm对应着提交页面的网页表单信息 * @author chen yuzhe * */

public class UserForm extends ActionForm{

/** * serialVersionUID */

private static final long serialVersionUID = -742922986494711669L;

 private int id;

 private String name;

 private int age;

private String loginId;

private Date birthday;

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 int getId() { return id; }

public void setId(int id) { this.id = id; }

public String getLoginId() { return loginId; }

public void setLoginId(String loginId) { this.loginId = loginId; }

public String getName() { return name; }

public void setName(String name) { this.name = name; } }

第二,编写user_form.jsp的表单页面,user_form.jsp的代码如下: 程序代码

<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>用户信息表单</title>
</head>
<body>
    
<div><strong>创建新用户</strong></div>
    
<html:form action="/userSave">
        
<table width="50%" border="1">
            
<tr>
                
<td>编号</td>
                
<td><html:text property="id"/></td>
            
</tr>            
            
<tr>
                
<td>姓名</td>
                
<td><html:text property="name" /></td>
            
</tr>            
            
<tr>
                
<td>登录号</td>
                
<td><html:text property="loginId" /></td>
            
</tr>    
            
<tr>
                
<td>年龄</td>
                
<td><html:text property="age" /></td>
            
</tr>                    
            
<tr>
                
<td>生日</td>
                
<td><html:text property="birthday" /></td>
            
</tr>            
        
</table>
        
<hr>
        
<html:submit></html:submit>
        
<html:reset></html:reset>
    
</html:form>

</body>
</html>

 

第三,配置struts-config.xml文件,struts-config.xml文件配置如下: 程序代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  
<data-sources />
  
<form-beans>
      
<form-bean name="userForm" type="zizz.struts.UserForm"></form-bean>
  
</form-beans>
  
<global-exceptions />
  
<global-forwards />
  
<action-mappings>
      
<action path="/userSave" name="userForm" validate="false" scope-->

作者:

抱歉!评论已关闭.