SpringMVC自定义集合编辑器不将数据返回到Jsp [英] SpringMVC Custom Collection Editor Not Returning Data To Jsp

查看:101
本文介绍了SpringMVC自定义集合编辑器不将数据返回到Jsp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在春季绑定了一个多选择列表,该项目没有从DAO获取其数据,而数据是从另一个选择选项列表中添加的.用户单击一个按钮,然后使用jquery将数据发送到多选选项列表.

I am binding a multi select list in spring the item does not get its data from the DAO the data is added from another select option list. The user clicks a button and the data is sent to the multi select option list using jquery.

发布表单时,由于该项目的数据类型复杂,因此不会进行数据绑定,因此我注册了CustomEditor并将其附加到@initbinder.

When the form is posted databinding does not happen for the item since its a complex data type so i registered a CustomEditor and attached it to the @initbinder.

已编辑 我已经更新了代码, CollectionEditor 现在将一个公民列表返回到视图,但是我无法获取列表中的数据来填充选择选项.我正在尝试将元素添加到列表中,但是从服务器返回时,jsp仍然选择null.

EDITED I have updated the code the CollectionEditor is now returning a list of citizens back to the view however i am unable to get the data in the list to fill the select option. I am trying to add elements to the list however the jsp still selects remain null when return form the server.

下面是代码:

CustomCollectionEditor

@InitBinder("crime")    
    protected void initBinder(WebDataBinder binder, HttpServletRequest request, ServletRequestDataBinder victimbinder){
     victimbinder.registerCustomEditor(List.class, "victims", new CustomCollectionEditor(List.class){
         protected Object convertElement(Object element){

             Citizens victims = new Citizens();

             String ssNumber = "";

             if (element instanceof String){
                ssNumber = (String) element;

             }                      

             logger.debug("element is ;" +element);

             try {

                int socialSecurityNumber = Integer.parseInt(ssNumber);
                victims = citizenManager.getCitizen(socialSecurityNumber);

            } catch (NumberFormatException e) {                     
                logger.error(e.getMessage());
            } catch (Exception e) {
                logger.error(e.getMessage());
            }

             return victims;        
         }

    }); 

从控制器中DAO填充的Jsp

当单击按钮时,它包含DAO类填充的数据,它将列表中的数据附加到绑定到POJO的其他列表上

This contains data filled form DAO class when the button is clicked it takes the data from the list on appends it to the other list under which is bind to the POJO

<label>Victims List</label><buttonid="addVictimBtn">/button>
<form:select path="" id="dbvictims" title="Victims Of Crime" class="victimLst">
<form:options items="${dbvictims.dbvictimList}" itemValue="socialSecurityNumber" itemLabel="name"/>
</form:select>  

Jsp选择绑定到POJO的项目

<label>Victims In Crime</label><button id="removeVictimBtn">-</button> 
<form:select path="victims" id="victims" title="Victims Of Crime"  multiple="multiple" class="victimLst">
<form:options items="${victimList}" itemValue="socialSecurityNumber" itemLabel="name"/>
</form:select><form:errors path="victims" class="errors" />                            

推荐答案

此问题的解决方案非常简单,所有工作已在 CustomCollectionEditor 中完成.当绑定诸如上述的复杂数据类型时,这一点很重要.可能还有其他方法可以做到这一点,但是我发现这是一种非常干净和简单的方法.

The Solution to this issue was very simple all of the work was already done in the CustomCollectionEditor. This is important when binding complex data types such as above. There may be other approaches to doing this however i find this to be a very clean and simple approach.

return语句非常重要,因为它绑定到视图中元素的item属性. CustomCollectionEditor 返回对象列表(受害者). DAO 的使用从数据库中获取对象.这很重要,因为帖子仅发送选择值而不发送标签,因此我们重建列表并重新发送到视图.

The return statement is very important since it binds to the item attribute of the element in the view. CustomCollectionEditor return a list of objects (victims) The use of the DAO gets the object from the database. This is important since the post only sends the select value not the label, hence we reconstruct the list and resend to the view.

我省略的部分是将List Object从控制器传递回视图.

The part of this that i omitted was passing the List Object from the controller back to the view.

控制器

@RequestMapping(value="save.htm", method = RequestMethod.POST)
    public ModelAndView handleSave(@Valid @ModelAttribute Crime crime, 
    BindingResult result,
    ModelMap m,
    Model model) throws Exception {


    if(result.hasErrors()){
           model.addAttribute("victimList",crime.getVictims());

    return new ModelAndView("*Your View*");
...............

这篇关于SpringMVC自定义集合编辑器不将数据返回到Jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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