如何使用Spring MVC将Java映射放入Optgroup选项?

时间:2022-10-02 23:55:29

I am using Spring MVC.

我使用的是Spring MVC。

I currently have a Map being passed from Java to my page in a variable called "userLocales" via request.setAttribute('userLocales', bluh bluh bluh)

我现在有一个从Java到我的页面的映射,在一个名为“userLocales”的变量中通过请求。setAttribute(userLocales,bluh bluh bluh)

I need to somehow get the strings in that variable to build a list of option elements in an optgroup select element. My initial thought would be to somehow turn this Map into a Javascript object somehow, and then add the strings to newly created option elements to be placed in the optgroup element.

我需要以某种方式获得该变量中的字符串,以在optgroup select元素中构建选项元素的列表。我最初的想法是,以某种方式将这个映射转换成一个Javascript对象,然后将字符串添加到新创建的选项元素中,以放置在optgroup元素中。

The optgroup element is static and already created. I just need the options with the string in them.

optgroup元素是静态的,已经创建了。我只需要有字符串的选项。

1 个解决方案

#1


5  

And I think I found the answer right after I posted this question. Wonderful

我想我在发布这个问题后找到了答案。美妙的

Source : http://www.imrantariq.com/blog/option-group-capability-for-spring-mvc-form-taglib/

来源:http://www.imrantariq.com/blog/option-group-capability-for-spring-mvc-form-taglib/

The “optgroup” attribute when set would first do a sort by whatever “expression” is defined in “optgroup”, and then would track the progression of iteration and insert the “optgroup” html start tag, and end tags where appropriate in the select list using the expression in “optgroup” as the label.

“optgroup”属性在设置时将首先按照“optgroup”中定义的任何“表达式”进行排序,然后跟踪迭代的进程并插入“optgroup”html开始标记,并在“optgroup”中使用“optgroup”中的表达式作为标签,在选择列表中适当的结束标记。

Unfortunately, currently spring mvc is not offering such functionality through its tags. This can be achieved through a nice logic given below. You need to send a hashMap from controller.

不幸的是,目前spring mvc并没有通过它的标签提供这样的功能。这可以通过下面给出的一个很好的逻辑来实现。您需要从控制器发送一个hashMap。

Map<String, ArrayList<String>>

In this map, string is key i.e. group name and arraylist is the number of values that lies in this group. Jsp code can be written in this way.

在这张地图中,字符串是关键,即组名和数组列表是这个组中的值的个数。Jsp代码可以以这种方式编写。

<form:select multiple="single" path="itemType" id="itemType">
    <form:option value="0" label="Select" />
    <c:forEach var="itemGroup" items="${itemTypeList}" varStatus="itemGroupIndex">
       <optgroup label="${itemGroup.key}">
           <form:options items="${itemGroup.value}"/>        
       </optgroup>
    </c:forEach>        
</form:select>

#1


5  

And I think I found the answer right after I posted this question. Wonderful

我想我在发布这个问题后找到了答案。美妙的

Source : http://www.imrantariq.com/blog/option-group-capability-for-spring-mvc-form-taglib/

来源:http://www.imrantariq.com/blog/option-group-capability-for-spring-mvc-form-taglib/

The “optgroup” attribute when set would first do a sort by whatever “expression” is defined in “optgroup”, and then would track the progression of iteration and insert the “optgroup” html start tag, and end tags where appropriate in the select list using the expression in “optgroup” as the label.

“optgroup”属性在设置时将首先按照“optgroup”中定义的任何“表达式”进行排序,然后跟踪迭代的进程并插入“optgroup”html开始标记,并在“optgroup”中使用“optgroup”中的表达式作为标签,在选择列表中适当的结束标记。

Unfortunately, currently spring mvc is not offering such functionality through its tags. This can be achieved through a nice logic given below. You need to send a hashMap from controller.

不幸的是,目前spring mvc并没有通过它的标签提供这样的功能。这可以通过下面给出的一个很好的逻辑来实现。您需要从控制器发送一个hashMap。

Map<String, ArrayList<String>>

In this map, string is key i.e. group name and arraylist is the number of values that lies in this group. Jsp code can be written in this way.

在这张地图中,字符串是关键,即组名和数组列表是这个组中的值的个数。Jsp代码可以以这种方式编写。

<form:select multiple="single" path="itemType" id="itemType">
    <form:option value="0" label="Select" />
    <c:forEach var="itemGroup" items="${itemTypeList}" varStatus="itemGroupIndex">
       <optgroup label="${itemGroup.key}">
           <form:options items="${itemGroup.value}"/>        
       </optgroup>
    </c:forEach>        
</form:select>