无法在spring 3.0 mvc中获取列表值

时间:2014-06-10 05:35:17

标签: java spring spring-mvc spring-3

我的 regForm.jsp 是:

    <form:form action="save.html">
    form:label path="name">Name *</form:label>
    <form:input path="name" />
    <form:label path="hobbies">Hobbies *<br />use (ctrl) botton to multiple select </form:label>
    <form:select path="hobbies" id="hobbies" multiple="true"
                                items="${hobList}" itemValue="id" itemLabel="hobbyName">
    </form:select>
    <input type="submit" value="Submit" onclick="addFormValidate()" />
    </form>

我的

public class User {
    private int id;     
    priavte List<Hobby> hobbies;
    //setter getter
}

public class Hobby {
    private int id;
    private String hobbyName;
    //setter getter
}

myController.java

@RequestMapping(value = "/save.html", method = RequestMethod.POST)
    public ModelAndView addEmployee(
            @ModelAttribute("user") User user BindingResult result) {           
        System.out.println("name"+user.getName() );



    System.out.println(employee.getHobbies()); // getting null  
    //   usrDao.saveUser(user);
return new ModelAndView("redirect:add.html", model);
}

我怎样才能从<我的表单中获取列表的值,以便我可以获得该值? **我的代码有什么问题? **

3 个答案:

答案 0 :(得分:1)

您需要一个转换器,将Hobby id转换为hobby

@See:Spring Reference, chapter 7.5 Spring 3 Type Conversion

package org.springframework.core.convert.converter;

public class StringToHobbyConverter implements Converter<String, Hobby> {

    Hobby convert(String source) {
       return loadHobbyById(Integer.valueOf(source));
    }

}

答案 1 :(得分:1)

这是代码----

package com.programsji.converter;

    import org.springframework.core.convert.converter.Converter;

    import com.programsji.bo.Hobby;

    public class StringToHobbyConverter implements Converter<String, Hobby> {

        @Override
        public Hobby convert(String str) {
            // I Am Using Fix Hobbies Here, You Can Add Your Own Method to Get
            // Hobbies By ID
            Hobby hobby = null;
            if (str.equals("1")) {
                hobby = new Hobby(1, "First Hobby");
            } else if (str.equals("2")) {
                hobby = new Hobby(2, "Second Hobby");
            }
            return hobby;
        }

    }

注册......

<annotation-driven conversion-service="conversionService" />
    <beans:bean id="conversionService"
        class="org.springframework.context.support.ConversionServiceFactoryBean">
        <beans:property name="converters">
            <beans:list>
                <beans:bean class="com.programsji.converter.StringToHobbyConverter" />
            </beans:list>
        </beans:property>
    </beans:bean>

它正在我的电脑上工作。如果您需要,我可以给您发送源代码。 关于conversionService注册,您可以查看此示例:

http://www.programsji.com/spring/converter

答案 2 :(得分:1)

您需要更改用户模型:

public class User {
    private int id;     
    priavte List<Hobby> hobbies;
    //setter getter
}

为:

public class User {
    private int id;     
    priavte List<Integer> hobbies;
    //setter getter
}

然后您将获得业余爱好列表 ID ,您可以循环浏览列表,并根据需要为每个ID创建一个新的 Hobby 对象。希望它有所帮助!!