无法在spring mvc application

时间:2017-11-21 06:02:51

标签: spring spring-mvc

使用spring mvc,我正在开发一个允许用户选择多种技能的页面(使用多选)。我能够显示技能。但是当我选择技能并点击提交时,我看到下面的错误。

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'candidate' on field 'skills': rejected value [1,3,4]; codes [typeMismatch.candidate.skills,typeMismatch.skills,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [candidate.skills,skills]; arguments []; default message [skills]]; default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 'skills'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.cts.form.SkillForm] for property 'skills[0]': no matching editors or conversion strategy found]
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:113)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)

以下是我的代码:

public class CandidateForm {

private String name;
..........
private List<SkillForm> skills;
.. setters ad getters

}

public class SkillForm {
    private Integer id;
    private String name;
    private String description;
    private String isSelected;
  ......... setters and getters
}

--- jsp

 <div class="form-group col-md-6 col-sm-6">
                            <form:label for="skills" path="skills">Candidate Primary Skills *</form:label>
                            <form:select multiple="true" path="skills" class="form-control input-sm" id="skills" >
                                <c:forEach var="item" items="${candidate.skills}">
                                    <form:option selected="${item.isSelected}" value="${item.id}" >${item.name}</form:option>
                                </c:forEach>
                            </form:select>
                        </div>

格式化

@Component
public class SkillFormatter implements Formatter<SkillForm>{

    public SkillFormatter() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public String print(SkillForm object, Locale locale) {
        // TODO Auto-generated method stub
        return object.getId().toString();
    }

    @Override
    public SkillForm parse(String text, Locale locale) throws ParseException {
        // TODO Auto-generated method stub
        SkillForm form = new SkillForm();
        form.setId(Integer.parseInt(text));
        return form;
    }

bean配置

<context:component-scan base-package="com.cts.formatter" />
    <beans:bean id="conversionService"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <beans:property name="formatters">
            <beans:set>
                <beans:ref bean="skillFormatter" />
            </beans:set>
        </beans:property>
    </beans:bean>

    <annotation-driven conversion-service="conversionService" />

控制器

 @RequestMapping(value = "/candidate/submit", method = RequestMethod.POST) 
        public String submitNewCandidate(@ModelAttribute("candidate") CandidateForm form, ModelMap model,BindingResult bindingResult) {
            if(logger.isDebugEnabled()){
                logger.debug("createCandidate is executed!");
            }
            -----------------------
            return "newCandidate";
        }

我有page作为参考。你有没有人提供一些输入,为什么,即使我已配置格式化程序,它不被调用?我应该在控制器方法中做什么吗?..有没有更好的实现?

0 个答案:

没有答案