无法使表单验证正常工作

时间:2011-11-15 09:37:09

标签: struts

我正在学习Struts 1.1并尝试使用我的代码进行一些表单验证。

但是我在MessageResources.properties文件中描述的错误没有显示在JSP上。我尝试了很多选择,但无法实现。我附上了一些代码。

MessageResources.properties

error.name.required = Please mention your name.
error.email.incorrect = You E-Mail ID is Incorrect.
error.phone.numericError = Phone number should consist only of digits.
error.phone.lengthIncorrect = Phone number should be only of 10 digits.

的struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
                           "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
 <form-beans>
     <form-bean name="detailsForm" type="com.example.form.DetailsForm"/>
 </form-beans>
 <action-mappings>
   <action input="/detailsEntry.jsp" name="detailsForm" path="/DetailsForm" type="com.example.action.DetailsAction" validate="true">
      <forward name="success" path="/displayDetails.jsp"/>
      <forward name="failure" path="/failure.jsp"/>
  </action>
  </action-mappings>
 </struts-config>

表格类:

package com.example.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

public class DetailsForm extends ActionForm {

private String name;
private String email;
private String phone;

public String getName() {
    return name;
}

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

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
    this.name = null;
    this.email = null;
    this.phone = null;
}

@Override
public ActionErrors validate(ActionMapping mapping,
        HttpServletRequest request) {
    ActionErrors actionErrors = new ActionErrors();

    if (this.name.equals(null) || this.name.length() == 0) {
        actionErrors.add("name", new ActionError("error.name.required"));
    }

    return actionErrors;
}

private boolean isNumeric(String phoneNumber) {
    try {
        Integer.parseInt(phoneNumber);
        return true;
    }
    catch (NumberFormatException numberFormatException) {
        return false;
    }
}
}

2 个答案:

答案 0 :(得分:3)

默认资源文件名为ApplicationResources.properties

使用不同(或多个)资源文件需要struts-config.xml中的配置:

<message-resource parameter="MessageResources" null="false" />

答案 1 :(得分:3)

不要忘记将以下内容添加到您的jsp:

<html:errors />

您的错误消息将出现在您将此标记放在jsp上的位置。

如果您希望将错误消息显示在与其相关的字段旁边,请使用以下内容:

<html:errors property="custName" />

其中“custName”是您在表单中创建错误消息的名称:

ActionMessages errors = new ActionMessages();
errors.add("custName", new ActionMessage("custName.invalid"));
request.setAttribute(Globals.ERROR_KEY, errors);