简体   繁体   English

jsp 下拉菜单不能自动选择正确的值

[英]jsp dropdown not auto-selecting correct value

I have a mostly auto-populated drop-down list on a jsp page.我在 jsp 页面上有一个主要是自动填充的下拉列表。 When the form is submitted the search results get displayed in a table below when the page reloads, and all the drop-down's and other field entries are repopulated as they were before the submit.提交表单后,当页面重新加载时,搜索结果会显示在下表中,并且所有下拉列表和其他字段条目都会像提交之前一样重新填充。

Except this one value <option value="A"><spring:message code="code.label.ANE" /></option> .除了这个值<option value="A"><spring:message code="code.label.ANE" /></option> Its not like the others.它不像其他人。 It wasn't dynamically created from the database because its basically just multiple results rolled into one and thus isn't a valid database code.它不是从数据库动态创建的,因为它基本上只是将多个结果合并为一个,因此不是有效的数据库代码。 The others will correctly get re-selected, but not this one.其他人将正确地重新选择,但不是这个。

I assume that <option value="">${Select}</option> only works because its the first value and thus gets selected by default.我假设<option value="">${Select}</option>仅起作用,因为它是第一个值,因此默认情况下被选中。 Cant actually test it because that value doesn't support any searches.无法实际测试它,因为该值不支持任何搜索。

No, my value cant become the new default because its unlikely to be the only custom report option.不,我的值不能成为新的默认值,因为它不太可能是唯一的自定义报告选项。

    <div class="col-md-4">
        <label>
            <spring:message code="common.label.lifeCycle" />
        </label>
        <form:select path="hrmsLifeCycleCode" id="hrmsLifeCycleCode" cssClass="form-control">
            <spring:message code="field.select" var="Select" />
            <option value="">${Select}</option>
            <option value="A"><spring:message code="code.label.ANE" /></option>
            <form:options items="${hrmsLifeCycle}" itemValue="codeValue" itemLabel="longDesc" />
        </form:select>
    </div>

In the jsp add the following在 jsp 中添加以下内容

    $(document).ready(function() {
        <c:if test="${querySelect == 'A'}">
            $("#hrmsLifeCycleCode").val('A'); 
        </c:if>
    });

in the controller在 controller

    model.addAttribute("querySelect", hrmsLifeCycle.getHrmsLifeCycleCode());

As the controller is now passing the selected value, when the selected value is this one case thats not being auto-selected it will select it after the page has been loaded.由于 controller 现在正在传递所选值,当所选值是这种未自动选择的情况时,它将在页面加载后 select 它。

This isnt the best solution but it worked.这不是最好的解决方案,但它有效。

There is a better solution that does not require a change to the jsp.有一个更好的解决方案,不需要更改 jsp。 While the original fix was minimal, this one is better but slightly more involved.虽然最初的修复是最小的,但这个更好但稍微复杂一些。

As the controller has two sections for this, the normal page, and one where the form/search has been performed, both sections needed to be altered.由于 controller 有两个部分,普通页面和一个执行表单/搜索的部分,这两个部分都需要更改。

So the original statement which produces the attribute was所以产生属性的原始语句是

   model.addAttribute("hrmsLifeCycle", GeneralCodeManager.getInstance().getList(GeneralCodeManager.LIFECYCLE_PROC_DESC_CTBL_HRMS_ONLY));

is instead changed to改为改为

    List<GeneralCode> something = new ArrayList<GeneralCode>(); 
    GeneralCode newCode = new GeneralCode(-1, STRING_NEW_REPORT_ID, "-1", "Report Name");
    something.add(newCode);
    List<GeneralCode> extraList = GeneralCodeManager.getInstance().getList(Report_ID);
    extraList.add(something);
    model.addAttribute("hrmsLifeCycle", extraList);

So the above creates a new list element for the report and adds it.所以上面为报告创建了一个新的列表元素并添加它。

This can further be improved by something I learned about a week or two ago, and thats defining modelAttributes in the controller outside of the requestMapping.这可以通过我在一两周前了解到的东西进一步改进,那就是在 requestMapping 之外的 controller 中定义 modelAttributes。

    @ModelAttribute("hrmsLifeCycle")
    List<GeneralCode> something = new ArrayList<GeneralCode>(); 
    GeneralCode newCode = new GeneralCode(-1, STRING_NEW_REPORT_ID, "-1", "Report Name");
    something.add(newCode);
    List<GeneralCode> extraList = GeneralCodeManager.getInstance().getList(Report_ID);
    extraList.add(something);

    return extraList;
    }

and to access would use并访问将使用

    "${extraList}"

And to really improve it, better names would help, lol.为了真正改进它,更好的名字会有所帮助,哈哈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM