【问题标题】:form creation in spring mvc issuespring mvc问题中的表单创建
【发布时间】:2014-06-09 12:07:06
【问题描述】:

我有两节课

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

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

现在我想为 User.java 创建表单

我的 form.jsp

<form:form method="POST" action="saveEmployee.html" commandName="user" name="register-form" id="register-form" cssClass="smart-green">

<form:select path="hobbies" multiple="true" size="3">
     <form:option value="1">Cricket</form:option>
     <form:option value="2">Computer Games</form:option>
     <form:option value="3">Tennis</form:option>
     <form:option value="4">Music</form:option>
</form:select>
</form:form>

myController.java

@RequestMapping(value = "/saveEmployee.html", method = RequestMethod.POST)
    public ModelAndView addEmployee(
            @ModelAttribute("user") User user BindingResult result) {           
        System.out.println(user.getChoice()); // giving null    
    //   usrDao.saveUser(user);
return new ModelAndView("redirect:add.html", model);
}

如何从我的表单中获取 List 的值,以便获取该值?

【问题讨论】:

  • 重命名@ModelAttribute("SpringWeb") 为@ModelAttribute("user") 然后试试
  • 我想问我如何从那里得到 List .. 我得到 List 爱好;但我想把它作为 List ;
  • 对于初学者来说,2-Games 不是有效的 int,因此即使转换有效也会产生异常。 HTTP只知道字符串不多不少,其他任何东西都需要转换。创建一个ConverterPropertyEditor,它知道如何从String 转换为Hobby。见spring reference guide
  • @ashish :: 使用 for 循环迭代您的列表,并通过创建 arrayList 的对象将每个爱好添加到列表中
  • @NiravPrajapati 好的...那么我将如何在我的表单中映射这个值??

标签: java spring spring-mvc


【解决方案1】:

将多选值绑定到 POJO 对象列表的解决方案在 CustomCollectionEditor 类下完成。这在绑定复杂数据类型(例如您的情况)时很重要。

在您的控制器类 myController.java 中添加以下代码:

@InitBinder
protected void initBinder(WebDataBinder binder)   
{
   binder.registerCustomEditor(List.class, "hobbies", new CustomCollectionEditor(List.class)
    {
       @Override
       protected Object convertElement(Object element)
        {
           Long id = null;
           String name = null;
           if(element instanceof String && !((String)element).equals(""))  
           {
              //From the JSP 'element' will be a String
              try{
                   id = Long.parseLong((String) element);
                 }
              catch (NumberFormatException e) {
                    e.printStackTrace();
                 }
            }
           else if(element instanceof Long) 
           {
              //From the database 'element' will be a Long
              id = (Long) element;
           }
           // Here you can get Hobby object from database based on the id you have got.
           //You any other way you can get hobbyName and set in hobby object and return it
           Hobby h = new Hobby();
           h.setId(Integer.parseInt(String.valueOf(id)));
           h.setHobbyName(name);
           return h;
     }
  });  
}  

更多详情参考链接:
SpringMVC bind Multi Select with object in Form submit
Multiple Select in Spring 3.0 MVC

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2021-05-18
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多