【问题标题】:Spring JSP Checkboxes on List Object列表对象上的 Spring JSP 复选框
【发布时间】:2016-01-27 17:16:38
【问题描述】:

我正在尝试在列表对象上使用<checkboxes> 标记。但是,尽管阅读了 mykong 教程并在其他地方进行了搜索,但我无法弄清楚这是如何完成的。

所以这就是我想要做的: 我有一堂课像

 class Person{

    List<IceCreams> creams;

    }

所以现在我想给我的用户一个表格,让他可以选择他喜欢的冰淇淋。

控制器:

@Controller
public class IceCreamController{

@RequestMapping(value="icecream", method=RequestMethod.GET)
public String showPage(Model model){
Person person = repository.getPerson(); //Returns a Person, "creams" is not empty
model.addAttribute("creams", person.getIceCreams();
}

@RequestMapping(value="icecream", method=RequestMethod.POST)
public String showPage( @ModelAttribute("teilnehmer") List<IceCreams> likedCreams, Model model){
//do something with selected iceCreams
}

现在我不明白如何在 JSP 中继续。我知道我必须使用 checkboxes 标签,但我不知道它在提交时返回什么或者我是否正确使用它。

<form:form>
<form:checkboxes path="creams" items="${creams}"/>
<input type="Submit" value="Submit">
</form:form>

那么问题来了:我在JSP中写了什么,什么会返回给控制器?

在评论后添加: 冰淇淋类:

  public class IceCream{
   private long id;
   private String creamName;

//+getter/setter }


编辑:在一个有用的答案之后,我尝试了这个: 将它们添加到模型中:

model.addAttribute("person", person);
     model.addAttribute("creams", person.getCreams()); 

在 JSP 中我做了

<form:checkboxes  path="teilnehmer"
                      items="${creams}"
                      itemValue="id"
                      itemLabel="creamName"
                      />

所以在 POST 方法中,我采用了 ModelAttribute Person。

添加到控制器:

@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
   binder.registerCustomEditor(IceCream.class, new IceCreamsPropertyEditor());

还有新的编辑器类:

public class ContactsPropertyEditor extends PropertyEditorSupport{

    @Autowired
    IceCreamRepository creamrep;

   @Override
   public void setAsText(String text) throws IllegalArgumentException {

         Integer creamId = new Integer(text);
         IceCream cream = creamrep.findOne(creamId);
         super.setValue(con);

   }
}

不幸的是,结果是错误 400。

【问题讨论】:

  • 你能发布你的IceCreams 课程吗
  • 你能发布更新的控制器吗?
  • 请修正代码中的拼写错误,例如联系人 contactId 和 superSetvalue(con) 没有意义。
  • 已更改,抱歉。新控制器实际上只是旧控制器,只是将 person 对象添加到模型中。

标签: spring jsp spring-mvc


【解决方案1】:

首先,您不能绑定到原始列表。您需要绑定到包装列表的对象:在您的情况下,这是 Person 的一个实例,而不是 List creams。

因此,将 Person 放入模型中。使用@ModelAttribute 方法,以便框架在提交时重新加载同一个人并设置值。我们很可能希望展示所有可用的冰淇淋以供选择。

@RequestMapping(method=RequestMethod.GET)
public String loadForEdit(){
    return "";
}

@RequestMapping(method=RequestMethod.POST)
public String save(@ModelAttribute("person") Person person){
    repository.savePerson(person);  

    return "";
}

//called by the framework on 'get' to load the person you wish to edit
//called by the framework on on 'post' to get the same instance for binding
//send personId as a hidden form element in the form
@ModelAttribute("person")
public Person getPerson(@RequestParam int personId){
    return repository.getPerson(personId);  
} 

@ModelAttribute("iceCreams")
public List<String> getAvailableIceCreams(){
    return repository.findAll();    
}

其次,框架不能自动将提交的表单参数转换为 IceCream 的实例。为此,您需要考虑使用转换器,但这是另一个问题。见这里:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html

鉴于上述情况,我们可以通过将集合类型更改为 String 来获得一个更简单的示例:

class Person{
    List<String> creams;
}

JSP 应该简单地变成:

<form:form modelAttribute="person">
    <!-- bind to the creams property of person -->
    <!-- create check boxes for all available ice creams -->
    <!-- any already in person.creams should be automatically checked -->
    <form:checkboxes path="creams" items="${iceCreams}" />
    <input type="hidden" value="${person.id}" name="personId"/>
    <input type="Submit" value="Submit">
</form:form>

一旦您熟悉了转换器,您就可以转换为绑定到 IceCream 实例,但这是一个太宽泛的主题。但是,在您的 JSP 中,您应该只需要更新复选框标记,如下所示:

<form:checkboxes path="creams" items="${iceCreams}"  itemValue="id" itemLabel="labelName"/>

其中 value 是将提交给服务器的属性,转换器将使用它来创建正确的实例(例如,保存在数据库中的项目的 ID),label 是用于显示的属性.

【讨论】:

  • 我想学对,所以我尝试了“复杂”的方法。在问题中更新了它
猜你喜欢
  • 2011-02-02
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多