如何避免springmvc中的特殊字符

如何避免springmvc中的特殊字符,spring,Spring,您好,我正在为表单字段执行服务器端验证。请参阅我正确获取了所有错误消息,但如何避免每个输入字段使用%$^&*等特殊字符,以及如何在收到错误消息时使输入框边框变红 为了避免特殊字符,我必须使用ESAPI.validator.getValidInput 如何在验证器类中使用下面的try-catch代码来避免特殊字符 控制器 模型 公共类客户{ @NotEmpty @Email private String emailId; @Size(min=8,max=15)

您好,我正在为表单字段执行服务器端验证。请参阅我正确获取了所有错误消息,但如何避免每个输入字段使用%$^&*等特殊字符,以及如何在收到错误消息时使输入框边框变红

为了避免特殊字符,我必须使用ESAPI.validator.getValidInput 如何在验证器类中使用下面的try-catch代码来避免特殊字符

控制器

模型 公共类客户{

@NotEmpty   
    @Email
    private String emailId;

    @Size(min=8,max=15)
    private String password;


    @Size(min=8,max=15)
    private String confPassword;

    private int age;

    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfPassword() {
        return confPassword;
    }

    public void setConfPassword(String confPassword) {
        this.confPassword = confPassword;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}
验证器


在您的字段中,您可以使用javax.validation.constraints.Pattern注释,然后使用类似[\w]*的东西,这意味着只有字母数字字符。

要进行验证,请使用如下@Pattern注释:

    @Pattern(regexp = "^[a-zA-Z0-9.\\-\\/+=@_ ]*$")
    @NotEmpty   
    @Email
    private String emailId;
对于错误字段红色边框,为错误添加一个css类,并将该类的css样式放入jsp的头块或您拥有的css文件中

    <tr>
        <td>Enter your E-mail:</td>
        <td><form:input path="emailId" /></td>
        <td><form:errors path="emailId" cssClass="error" /></td>
    </tr>

    <style>
        .error {
            color: red;
            border: 1px solid red;
        }
    </style>
然后为Validator类中的每个输入添加以下内容,我只举一个例子

        try {
            if (!ESAPI.validator().isValidInput("ValidationOfPassword", password, "ValidInput", 200, false)) {
                errors.rejectValue("password","customer.password.missMatch");//replace your msg property in second param
            }
        } catch (Exception e) {
            //something gone wrong
            e.printStackTrace();
            errors.rejectValue("password","customer.password.missMatch");//replace your msg property in second param
        }

我认为您需要添加一些锚定,如^[\w]*$,以便它尝试将模式与整个字段匹配,而不仅仅是ITI的一部分。如果您使用maven,请将属性文件添加到参考资料目录中。我没有在struts项目中使用maven,我只是在project下添加
        <tr>
            <td>Enter your E-mail:</td>
            <td><form:input path="emailId" /></td>
            <td><form:errors path="emailId" cssStyle="color: #ff0000;" /></td>
        </tr>

        <tr>
            <td>Enter your Age:</td>
            <td><form:input path="age"/></td>
            <td><form:errors path="age" cssStyle="color: #ff0000;"/></td>
        </tr>

        <tr>
            <td>Enter your password:</td>
            <td><form:password path="password"  showPassword="true"/></td>
            <td><form:errors path="password" cssStyle="color: #ff0000;"/></td>
        </tr>

            <tr>
            <td>Confirm your password:</td>
            <td><form:password path="confPassword"  showPassword="true"/></td>
            <td><form:errors path="confPassword" cssStyle="color: #ff0000;"/></td>
        </tr>

        <tr>
            <td><input type="submit" name="submit" value="Click here to Register"></td>
        </tr>
    </table>
</form:form>
NotEmpty.customer.emailId=Email Id is required. 
Email.customer.emailId=valid email id is required.
Size.customer.password=Password should be minimum of 8 and maximum of 15 characters.
Size.customer.confPassword=Password should be minimum of 8 and maximum of 15 characters.
customer.age.empty = Age is required
customer.age.range.invalid = Age should be between 18 to 60
customer.password.missMatch = password and confirm password do not match
    @Pattern(regexp = "^[a-zA-Z0-9.\\-\\/+=@_ ]*$")
    @NotEmpty   
    @Email
    private String emailId;
    <tr>
        <td>Enter your E-mail:</td>
        <td><form:input path="emailId" /></td>
        <td><form:errors path="emailId" cssClass="error" /></td>
    </tr>

    <style>
        .error {
            color: red;
            border: 1px solid red;
        }
    </style>
Validator.ValidInput=^[a-zA-Z0-9.\\-\\/+=@_ ]*$
        try {
            if (!ESAPI.validator().isValidInput("ValidationOfPassword", password, "ValidInput", 200, false)) {
                errors.rejectValue("password","customer.password.missMatch");//replace your msg property in second param
            }
        } catch (Exception e) {
            //something gone wrong
            e.printStackTrace();
            errors.rejectValue("password","customer.password.missMatch");//replace your msg property in second param
        }