当前位置:编程学习 > JAVA >>

struts2 的date 校验器不起作用

     我测试struts2 的date校验器可是完全不起作用,代码如下:

表单页面(date.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
     <s:head/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>日期交易器</title>
</head>
<body>
   <center>
     <p/>
     <s:form action="date" method="post">
        <s:datetimepicker displayFormat="yyyy-MM-dd" label="生日" name="birthday"></s:datetimepicker>
        <s:submit value="提交"/>
     </s:form>
   </center>
</body>
</html>

DateAction.java

package tmp.action;

import java.sql.Timestamp;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class DateAction extends ActionSupport{
    private Timestamp birthday;

public Timestamp getBirthday() {
return birthday;
}

public void setBirthday(Timestamp birthday) {
this.birthday = birthday;
}
     
    public String execute(){
     return SUCCESS;
    }
}

DateAction-validation.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE validators PUBLIC 
 "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
 "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
 
 <validators>
   <field name="birthday">
     <field-validator type="date">
       <param name="min">1900-01-01</param>
       <param name="max">1991-12-30</param>
       <message>生日应该在1900-01-01到1991-12-30之间!</message>
     </field-validator>
   </field>
 </validators>

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_Login" extends="struts-default">
       <action name="validate" class="tmp.action.ValidateAction" method="login">
          <result name="input">/validate.jsp</result>
          <result name="success">/success.jsp</result>
       </action>
       
       <action name="requiredstring" class="tmp.action.Requiredstring">
          <result name="input">/requiredstring.jsp</result>
          <result name="success">/success.jsp</result>
       </action>
       
       <action name="required" class="tmp.action.Required">
         <result name="input">/required.jsp</result>
         <result name="success">/success.jsp</result>
       </action>
       
       <action name="int" class="tmp.action.IntAction">
         <result name="input">/int.jsp</result>
         <result name="success">/success.jsp</result>
       </action>
       
       <action name="date" class="tmp.action.DateAction">
         <result name="input">/date.jsp</result>
         <result name="success">/success.jsp</result>
       </action>
    </package>
  </struts>

运行:http://localhost:8080/ch5/date.jsp

界面如下:输入一个不在校验范围内的日期,居然没有校验,跳到了成功页面



--------------------编程问答-------------------- action中,应该这么写
private Date birthday;
补充:Java ,  Java EE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,