2011-11-15 9 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> 

フォームクラス:

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に載せました。

そして、あなたはあなたのエラーメッセージは、彼らが次の使用に関するフィールドの横に表示させたい場合は:「CUSTNAME」は、あなたが作成したときにエラーメッセージを与えた名前です

<html:errors property="custName" /> 

それはあなたのフォームにあります。

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