1

Spring 3,Portlet API

我无法在 jsp 页面上显示验证错误消息,尽管bindingResult.hasErrors()在我的操作映射中返回 true,以下是我的代码。任何帮助表示赞赏。

以下是我的 JSP 代码

<portlet:actionURL var="searchDeviceResultsURL">
    <portlet:param name="doAction" value="searchDeviceResults" />
</portlet:actionURL>

<form:form method="post" action="${searchDeviceResultsURL}">
<div>
<form:errors path="*" />
</div>
...

<input id="sourceId" /> 
...
</form:form>

以下是我的控制器

@RenderMapping
    public String home(RenderResponse response) {
        return "startup";
    }


    @ModelAttribute("searchForm")
    public SearchForm getCommandObject() {
        return new SearchForm();
    }

    @RenderMapping(params = "doAction=showDeviceResults")
    public String showResults() {
        //return device result JSP name

        return "device_results";
    }

    @ExceptionHandler({ Exception.class })
    public String handleException() {
        return "error";
    }

    /*
     * On submit form this action searches for available devices
     */
    @ActionMapping(params = "doAction=searchDeviceResults")
    public void searchResults(@ModelAttribute(value = "searchForm") SearchForm searchForm, BindingResult bindingResult, 
            ActionResponse response, SessionStatus sessionStatus) {

        searchFormValidator.validate(searchForm, bindingResult);

        if (!bindingResult.hasErrors()) {

            response.setRenderParameter("doAction", "showDeviceResults");
            sessionStatus.setComplete();
        }

    }

以下是我的验证器

public class SearchFormValidator implements Validator {

    public boolean supports(Class<?> klass) {
        return SearchForm.class.isAssignableFrom(klass);
    }

    public void validate(Object target, Errors errors) {
        //SearchForm searchForm = (SearchForm)target;
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "serviceId", "search.service.field.error");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "sourceId", "search.source.field.error");
        //errors.rejectValue("serviceId", "search.service.field.error");
        //errors.rejectValue("sourceId", "search.source.field.error");  
    }
}

以下是我的弹簧配置

<bean id="searchFormValidator" class="com.comcast.triage.form.SearchFormValidator" />

<bean
    class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean
            class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="validator" ref="searchFormValidator" />
        </bean>
    </property>
</bean>

在我的 portlet.xml 文件中,我有以下运行时选项

<container-runtime-option>
        <name>javax.portlet.actionScopedRequestAttributes</name>
        <value>true</value>
    </container-runtime-option>
    <container-runtime-option>
        <name>javax.portlet.renderHeaders</name>
        <value>true</value>
    </container-runtime-option>
    <container-runtime-option>
        <name>javax.portlet.escapeXml</name>
        <value>false</value>
    </container-runtime-option>
4

1 回答 1

0

做了两个改变,事情开始起作用了

1- form tag
2- form input tag

    <form:form name="searchDeviceForm" commandName="searchForm" method="post" action="${searchDeviceResultsURL}">

<form:input path="accNum" /> 
于 2013-05-31T14:29:09.833 回答