Struts2验证仅不适用于正则表达式 [英] Struts2 validation doesn't work only with regex

查看:75
本文介绍了Struts2验证仅不适用于正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

验证与所需的字符串(和电子邮件,我已经测试了这些)一起很好地工作了.但是当我尝试使用正则表达式时,验证无效.

The validation works fine with the required string (and email, I tested just these). But when I try to use regex, the validation has no effect.

<!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="cognome">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Cognome è un campo obbligatorio</message>
        </field-validator>
    </field>
    <field name="telefono">
        <field-validator type="requiredstring">
            <message key="errors.required" />
        </field-validator>
        <field-validator type="regex">
            <param name="expression">[0-9]{9,15}</param>
            <message>Il numero di telefono deve essere minimo di 9 cifre
            </message>
        </field-validator>
    </field>

</validators>

telefono 必须包含9到15个数字.当字段丢失时,这会产生错误;如果插入 telefono 之类的"123",则不会出现错误.

telefono must have between 9 and 15 numeric digits. This give error when the fields are missing, and no error if I insert a telefono like "123".

jsp:

<%@ page language="java" contentType="text/html;"
     import="java.util.*,it.almaviva.bean.*,it.almaviva.delegate.*;"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="${pageContext.request.contextPath}/css/stile1.css" rel="stylesheet" type="text/css" />
<title>Registrazione account</title>
</head>

<body>
<jsp:include page="header.jsp"/> 
<s:actionerror />

<s:form name="formDatiUtente" action="inviaRichiesta.action" method="post" theme="simple" validate="true">

<center>
<s:fielderror></s:fielderror>
<table width="48%" class="LISTA" border="0" cellPadding="3" cellSpacing="5" align="center">
                <tr>
                    <td width="35%">
                        <p class="testodx">
                            <s:text name="label.cognome" />
                        </p>
                    </td>
                    <td>
                        <p class="testosx">
                            <s:textfield name="cognome" id="idCognome"
                            size="30" value="%{anagraficaVDR.cognome}" />
                        </p>
                    </td>
                </tr>

                <tr>
                    <td>
                        <p class="testodx"><s:text name="label.telefono_Ufficio_reparto" /></p>
                    </td>
                    <td>
                        <s:textfield name="telefono" id="idTelefono_Ufficio_reparto" size="30" value="%{anagraficaVDR.telefono}"/>
                    </td>
                </tr>

</table>
<br>

<s:if test="!gestioneAmministratore">
    <s:submit method="execute" cssClass="bottone" key="label.invia" align="center" />
</s:if>    
</center>  

</s:form>
</body>
</html>

另一个次要问题是,如果我删除此内容:

another secondary problem is that if I remove this:

<s:fielderror></s:fielderror>

不显示错误消息.这很奇怪,因为例如此处,此字符串丢失并且错误消息正确显示(每个字段下我都喜欢).

the error messages are not displayed. That it's strange, because for example here this string is missing and the error messages are showed correctly (each under every field, thing that I prefer).

另一个次要问题是,如果我不插入字段而是按提交,则会收到(正确)错误,但是字段中的所有值都将被删除,而我宁愿保留所有值(如上一个) 我已链接)的示例.

Another secondary problem is that if I doesn't insert a field and I press submit, I get (correctly) an error, but all the values in the fields are erased, instead I prefer to keep all (like in the previous example that I linked).

推荐答案

参数应为regexExpression,而不是expression.

http://struts.apache.org/development/2.x/docs/regex-validator.html

要显示特定字段的错误,请使用fieldName属性,例如

To display a specific field's error, use the fieldName attribute, e.g.,

<s:fielderror fieldName="cogname" />

请注意,<s:form>validate属性与客户端验证有关.它与服务器端的验证无关.

Note that <s:form>'s validate attribute regards client-side validation; it has nothing to do with validation on the server side.

这篇关于Struts2验证仅不适用于正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆