1

両方のフィールドに入力する必要がある 2 項目のフォームがあります。注釈付きの検証を使用していて、エラーメッセージが表示されますが、フィールド名が「空白ではない可能性があります」ではなく、「空白ではない可能性があります」というフィールド名が出力されません。

何か案は?これは非常に簡単な例です:

@Controller
@SessionAttributes
 public class HomeController
{

private static final Logger logger  = LoggerFactory
                                            .getLogger(HomeController.class);

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model)
{
    logger.info("Welcome home! The client locale is {}.", locale);
    model.addAttribute("homeformitem", new HomeFormItem());
    return "home";
}

@RequestMapping(value = "/", method = RequestMethod.POST)
public String home(
        @ModelAttribute("homeformitem") @Valid HomeFormItem item,
        BindingResult result)
{
    logger.info("Posting form");
    logger.info(item.toString());
    if (result.hasErrors())
    {
        return "home";
    } else
    {
        logger.info("No Errors");
        return "done";
    }
}
}

これがフォームです。エラー メッセージは表示されますが、フィールド名は表示されません。

フォーム検証!

    <f:form method="post" modelAttribute="homeformitem">
    <fieldset>
        <legend>Fields</legend> 
    <table>
        <tr><td colspan="3"><f:errors path="*"/></td></tr>
        <tr>
            <td><f:label path="one" for="one">One: </f:label></td>
            <td><f:input path="one" /></td>
            <td><f:errors path="one" cssClass="errorblock"/></td>
        </tr>
        <tr>
            <td><f:label path="two" for="two">Two: </f:label></td>
            <td><f:input path="two" /></td>
            <td><f:errors path="two" cssClass="errorblock"/></td>
        </tr>
        <tr>
            <td colspan="3"><input type="submit" value="Test" /></td>
        </tr>
    </table>
</fieldset>
</f:form>

4

1 に答える 1

2

validation.properties次のようなエントリを持つファイルが必要です/WEB-INF/messages(例):

NotEmpty.homeFormItem.one=The field {0} is empty

また、検証メッセージを解決するために Spring Bean を追加することを忘れないでください。

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/messages/validation" />
</bean>
于 2013-04-19T13:36:26.303 に答える