使用javascript中的索引获取spring模型属性列表元素 [英] getting a spring model attribute list element using an index from javascript

查看:89
本文介绍了使用javascript中的索引获取spring模型属性列表元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果有人在其他地方询问过,但是我已经四处寻找,找到了一些答案,但不是一个完整的例子,我仍然怀疑这个。

Sorry if this has been asked somewhere else, but I have looked all around, found some answers, but not a complete example and I am still in doubts on this one.

所以,我正在从我的Spring控制器添​​加一个Autopopulating列表到我的jsp,我想在我的javascript / jquery函数中的列表中添加项目。可能吗?

So, I am adding an Autopopulating list from my Spring controller to my jsp and I would like to add items on the list inside my javascript/jquery function. Is it possible?

我尝试了下面的代码来测试功能但它没有用(列表元素根本没有显示在生成的html中)。所以我不确定我是否搞乱了javascrit / spring / jsp语法或者是不可能。

I tried the code below to test the functionality but it did not work (the list elements did not show up at all at the generated html). So Im unsure if Im messing up with the javascrit/spring/jsp syntax or if it is just not possible.

这是代码:

控制器代码:

@RequestMapping(value="/create_custobject.html",method = RequestMethod.GET)
public ModelAndView showCreateCustObjectPage() {
    Map<String, Object> model = new HashMap<String, Object>();

    CreateObjectForm form = new CreateObjectForm();
    model.put("createObjectform", form);

    return new ModelAndView("create_custobject", model) ;

}

表格代码:

public class CreateObjectForm {

      private AutoPopulatingList<Criteria> ruleArray = new AutoPopulatingList<Criteria>(Criteria.class);

     public AutoPopulatingList<Criteria> getRuleArray() {
    return ruleArray;
        }

         public void setRuleArray(AutoPopulatingList<Criteria> ruleArray) {
    this.ruleArray = ruleArray;
        }

         public CreateObjectForm() {}
      }

标准代码:

public class Criteria{

   String attribute;

    String operator;
       //... constructor + getters and setters
}

javascript / jquery代码(与jsp在同一页面上):

javascript/jquery code (on the same page as the jsp one):

<script type="text/javascript">
$(document).ready(function(){
    //startup functionality

 var i = 0;
 document.getElementById("addCriteria").onclick = function() {

         $("#msgid").html("${ruleArray[i].attribute}");

        ${ruleArray[i].attribute} = $('#attributeValue').val();             
        ${ruleArray[i].operator} = $('#operatorValue').val(); 

                    i++;            

      }
   }


推荐答案

表单中的现有项目使用jstl作为

for existing items in your form use jstl as

<c:forEach items="${form.items}" var="item" varStatus="status" >
<span class="count" > 
<form:input   path="items[${status.index}].field" />


这将呈现这样的形式

<form id = "idform" >
<span class="count" > 
    <input   name="items[0].field"  id="items0.field" />
</span>
</form>

然后你只需添加javascript新表格行与相应的索引

then you simply add with javascript new form "lines" with coresponding indexes

例如

 var is = $('.count').size()
 $('#idform span:last').after('<span class="count" ><input name="items[' + is + '].field"' + is + '.field" /></span>')

我认为如果你使用的是弹簧3 +你不需要使用AutopopulatingList,任何集合都应该足够了。

I think that if you are using spring 3 + you don't need to use AutopopulatingList , Any collection should be enough.

这篇关于使用javascript中的索引获取spring模型属性列表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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