Struts的輸入驗證框架Validator使用

1、首先保證要做輸入驗證的Action實現了validateable接口(如果Action已經繼承了actionSupport則不用)

2、有兩種方法使用Validator框架,一個是實現validate()方法,另一種是通過xml配置,以下是介紹配置的方法(因為內置的驗證方法多)

  1、在要做輸入驗證的action類的同一目錄下創建ActionClassName-validation.xml配置文件

     字段校驗器風格:

        注意:被校驗的字段名與前端表單的字段名一致

<validators>  
  <field name="被校验的字段">  
     <field-validator type="校验器的类型">  
        <param name="参数名">参数值</param>  
         <message> 提示信息</message>  
     </field-validator>  
  </field>  
  
  <!--下一个要验证的字段-->  
    
</validators>  

    非字段校驗器風格

<valiators>  
  <validator type="校验器类型名">  
     <!--fieldName固定的 N必须大写-->  
     <param name="fieldName">需要被校验的字段</param>  
      <!--下面的param元素可以有0个或者多个-->   
     <param name="参数名">参数值</param>  
    <message key="I18NKey">提示信息</message>  
  </validator>  
</validators>  

    校驗器類型

  示例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
  "-//Apache Struts//XWork Validator 1.0.3//EN"
  "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">   

<validators>
    <field name="uName">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>請輸入用戶名</message>
            <!-- 若要使用國際化提示  -->
            <!-- <message key="uName.required"></message> -->
        </field-validator>
    </field>
    <field name="uPassword">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>請輸入密碼</message>
        </field-validator>
    </field>
</validators>

  2、在struts.xml配置文件中,對應action要有input碼(<result name="input">/要跳轉的頁面,如/login.jsp</result>)

    示例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
    
<struts>
    <!-- 配置動態方法調用所需配置  -->
    <constant name="struts.devMode" value="true"></constant>
    <constant name="struts.enable.DynamicMethodeInvocation" value="true"></constant>
    <package name="MyPackage" namespace="/" extends="struts-default">
        <!-- 動態調用配置  -->
        <global-allowed-methods>login, registe</global-allowed-methods>
        <action name="UserAction_*" class="com.strutsWork.web.UserAction" method="{1}">
            <result name="success">/index.jsp</result>
            <result name="fair" type="redirect">/fairPage.jsp</result>
            <result name="input">/login.jsp</result>
        </action>
    </package>
</struts>

  最後,在以上示例中的/login.jsp中,要顯示message的方法

    先在jsp中添加taglib  --->  <%@ taglib uri="/struts-tags" prefix="s"%>

    顯示: 直接看示例

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <s:form action="/strutsWork/UserAction_login" method="post" validate="true">
        <label>用戶名:</label><input type="text" name="uName"/><br>
        <label>密碼:</label><input type="password" name="uPassword"/><br>
        <s:fielderror/>
        <input type="submit" value="提交"/>
    </s:form>
  </body>
</html>

   注意紅色部分

效果展示

 

posted @ 2019-03-25 21:24  HikigayaHachiman  阅读(157)  评论(0编辑  收藏  举报