2011-11-15 63 views
1

我正在学习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> 

Form类:

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; 
    } 
} 
} 

回答

3

默认的资源文件名是ApplicationResources.properties

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

<message-resource parameter="MessageResources" null="false" /> 
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);