列表对象上的Spring JSP复选框 [英] Spring JSP Checkboxes on List Object

查看:122
本文介绍了列表对象上的Spring JSP复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在列表对象上使用<checkboxes>标记.但是,尽管阅读了mykong教程并在其他地方进行了搜索,但我仍然不知道该怎么做.

I am trying to use the <checkboxes> tag on a List Object. But despite reading the mykong tutorial and searching elsewhere, i can't figure out how that is done.

这就是我想要做的: 我有一个类似的课程

So here is what i want to do: I have a class like

 class Person{

    List<IceCreams> creams;

    }

所以现在我想给我的用户一个表格,他可以在其中选择他喜欢的IceCreams.

So now i want to give my User a form where he can choose which IceCreams he likes.

控制器:

@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标记,但是我不知道它在提交时返回什么或者我是否正确使用它.

Now i don't understand how to continue in the JSP. I know i have to use the checkboxes tag, but i do not know what it returns on submit or if i use it correctly.

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

所以问题是:我应该在JSP中编写什么内容,什么将返回给控制器?

So the question is: What do I write in the JSP and what will be returned to the controller?

在评论后添加: IceCream课程:

Added after comment: IceCream class:

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

//+获取器/设置器 }

//+getters/setters }

在提供了有用的答案后,我尝试了以下操作: 将它们添加到模型中:

After a helpful answer i tried this: Adding those to the model:

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

在JSP中,我做了

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

因此,在POST方法中,我采用了ModelAttribute Person.

So in the POST-Method i take a ModelAttribute Person.

已添加到控制器:

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

和新的Editor类:

and the new Editor class:

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.

Sadly the result is Error 400.

推荐答案

首先,您无法绑定到原始列表.您需要绑定到包装列表的对象:在您的情况下,它是Person的实例,而不是List的表壳.

Firstly, you cannot bind to a raw list. You need to bind to the object wrapping the list: in your case that is an instance of Person rather than the List creams.

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

So, put the Person in the model. Use a @ModelAttribute method so the framework will reload the same person on submit and set the values. Most likely we would want to present all available ice creams for selection.

@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的实例.为此,您需要考虑使用转换器,但这是另一个问题.看到这里:

Secondly, the framework cannot automatically convert from the submitted form parameters to instances of IceCream. To do that you will need to look at using a converter but that is another question. See here:

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

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

Given the above then we can get a simpler example working for now by changing the collection type to String:

class Person{
    List<String> creams;
}

然后,JSP应该简单地成为:

The JSP should then simply become:

<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中,您只需要更新以下复选框标签即可:

Once you are familiar with converters you can convert to bind to IceCream instances but that is too broad a topic. However in your JSP you should simply need to update the checkboxes tag as below:

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

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

where value is the property that will be submitted to the server and which will be used by your converter to create the correct instance (e.g. the ID of an items saved in a database) and label is the property to be used for display.

这篇关于列表对象上的Spring JSP复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆