【问题标题】:jsp dropdown not auto-selecting correct valuejsp下拉菜单没有自动选择正确的值
【发布时间】:2019-10-17 15:08:05
【问题描述】:

我在 jsp 页面上有一个主要是自动填充的下拉列表。提交表单后,当页面重新加载时,搜索结果会显示在下表中,并且所有下拉列表和其他字段条目都会像提交之前一样重新填充。

除了这个值<option value="A"><spring:message code="code.label.ANE" /></option>。它不像其他人。它不是从数据库动态创建的,因为它基本上只是将多个结果合并为一个,因此不是有效的数据库代码。其他人将正确地重新选择,但不是这个。

我假设<option value="">${Select}</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>

【问题讨论】:

    标签: java spring jsp


    【解决方案1】:

    在jsp中添加以下内容

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

    在控制器中

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

    当控制器现在传递所选值时,当所选值是未自动选择的一个案例时,它将在加载页面后选择它。

    这不是最好的解决方案,但确实有效。

    【讨论】:

      【解决方案2】:

      有一个更好的解决方案,不需要更改jsp。虽然最初的修复是最小的,但这个更好,但涉及更多。

      由于控制器为此有两个部分,一个是普通页面,一个是执行表单/搜索的部分,这两个部分都需要更改。

      所以产生属性的原始语句是

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

      改为

          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);
      

      所以上面为报表创建了一个新的列表元素并添加它。

      这可以通过我在一两周前学到的东西进一步改进,那就是在 requestMapping 之外的控制器中定义 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;
          }
      

      访问会使用

          "${extraList}"
      

      要真正改进它,更好的名字会有所帮助,哈哈。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-11
        • 2018-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多