弹簧形式选择标记不突出显示多个项目

时间:2016-03-31 20:35:24

标签: spring jsp spring-mvc drop-down-menu spring-form

我已经设立了一个Spring" select"用于输入搜索参数并显示结果的jsp页面上的表单标记。 Spring表单select tage允许用户选择多个项目,因为" multiple = true"选项。我在查看结果显示时突出显示已选择多个项目时遇到问题。

如果用户选择单个项目,它会正常工作,它会在重新显示表单时突出显示(输入的搜索参数仍显示在页面的上半部分,搜索结果显示在下半部分。)

选择了单项:

Single Item Selected

在响应显示中仍然显示单个项目:

Single item still shown as selected on response display

但是,如果选择了多个项目,则结果显示中的多个选项都不会突出显示:

选择了多个项目:

Multiple Items selected:

结果显示中未显示多个项目:

Multiple items not shown in results display

以下是相关的jsp代码:

动作命令:

<form:form action="/${localePath}/search/ssSearch.do" method="post"  modelAttribute="searchCommand" onsubmit="return trackSearch();">

选择代码:

<tr valign="top"><td>Test Items</td><td>
<form:select path="testItems" multiple="true" size="4">
        <form:option value="">All Items</form:option>
        <form:options items="${testItems}" />
</form:select>

Java代码:

这是&#34; testItems&#34; SearchCommand对象中的代码:

private String testItems;

    public String getTestItems() {
        return testItems;
    }

    public void setTestItems(String testItems) {
        this.testItems = testItems;
    }

这是控制器请求代码:

@RequestMapping(value = "/*/search/ssSearch.do", method=RequestMethod.POST) 
public String searchResults( HttpServletRequest request, 

@ModelAttribute("searchCommand") SearchCommand command,  ModelMap map)
            throws Exception 
    {
        return searchResultsDo(command,  map, request);
    }

以下是用于填充testItems的代码:

    Map<String, String> testItemsMap = new TreeMap<String, String>();
    testItemsMap.put("item1 name", "item 1 value");
    testItemsMap.put("item2 name", "item 2 value");
    testItemsMap.put("item3 name", "item 3 value");
    testItemsMap.put("item4 name", "item 4 value");
    testItemsMap.put("item5 name", "item 5 value");
    testItemsMap.put("item6 name", "item 6 value");
    testItemsMap.put("item7 name", "item 7 value");
    testItemsMap.put("item8 name", "item 8 value");

    map.addAttribute("testItems", testItemsMap);

如何在响应页面上突出显示多个选定项目?

1 个答案:

答案 0 :(得分:0)

答案是在代码中收集所选项目的地图,然后在jsp中引用它,如下所示:

                <form:select path="testItems2" multiple="true" size="4">
                     <form:option value="">All Items</form:option>              
                      <c:forEach items="${testItems2}" var="testItem2">
                       <c:choose>
                        <c:when test="${not empty items2SelectedMap[testItem2.key]}">
                          <form:option value="${testItem2.key}" selected="true">${testItem2.value}</form:option>
                          </c:when>
                          <c:otherwise>
                          <form:option value="${testItem2.key}" >${testItem2.value}</form:option>
                        </c:otherwise>                            
                       </c:choose>
                    </c:forEach>                            
                </form:select> 

Java代码:

        Map<String, String> testItemsMap2 = new TreeMap<String, String>();

        List<String> items2SelectedList = null;
        if(StringUtils.isNotBlank(command.getTestItems2())){
            String[] items2Selected = command.getTestItems2().split(",");
            items2SelectedList = Arrays.asList(items2Selected);
        }


        Map<String, String> items2SelectedMap = new HashMap<String, String>();
        if (items2SelectedList != null) {
            for (String item2 : items2SelectedList) {
                items2SelectedMap.put(item2, item2);
            }
        }
        map.addAttribute("items2SelectedMap", items2SelectedMap);