簡體   English   中英

動態檢索對象,Spring MVC,JSP

[英]Dynamically retrieve the object, Spring MVC, JSP

我有jsp頁面用於創建一個新問題(我的項目中的實體),使用這部分代碼:

   <div class="form-group">
        <nobr><label>Project</label></nobr>
        <c:if test="${!empty userProjects}">
            <sf:select path="projectId" cssClass="selectpicker">
                <c:forEach items="${userProjects}" var="project">
                    <sf:option value="${project.id}">${project.nameOfTheProject}</sf:option>
                </c:forEach>
            </sf:select>
        </c:if>
        <c:if test="${empty userProjects}">
            There are no projects
        </c:if>
    </div>

我選擇了加入當前問題的項目到這個選定的上述項目。 接下來在同一頁面上:

   <div class="form-group">
        <nobr><label>Who will fix the issue?</label></nobr>
        <c:if test="${!empty project.usersInTheCurrentProject}">
            <sf:select path="fixerId" cssClass="selectpicker">
                <c:forEach items="${project.usersInTheCurrentProject}" var="user">
                    <sf:option value="${user.id}">${user.firstName} ${user.lastName}</sf:option>
                </c:forEach>
            </sf:select>
        </c:if>
        <c:if test="${empty project.usersInTheCurrentProject}">
            There are no users
        </c:if>
    </div>

我之前需要選擇項目,為了從這個項目中獲取用戶列表,我該如何實現呢? 謝謝。

您需要使用ajax調用來獲取用戶列表和控制器以通過json響應返回列表。

<div class="form-group">
    <nobr><label>Project</label></nobr>
    <c:if test="${!empty userProjects}">
        <sf:select path="projectId" cssClass="selectpicker">
            <c:forEach items="${userProjects}" var="project">
                <sf:option value="${project.id}">${project.nameOfTheProject}</sf:option>
            </c:forEach>
        </sf:select>
    </c:if>
    <c:if test="${empty userProjects}">
        There are no projects
    </c:if>

<div class="form-group">
   <label>Who will fix the issue?</label>
    <c:if test="${!empty project.usersInTheCurrentProject}">
        <sf:select id="fixerId" path="fixerId" cssClass="selectpicker">
        </sf:select>
    </c:if>
    <c:if test="${empty project.usersInTheCurrentProject}">
        There are no users
    </c:if>
</div>

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


                    $('#projectId')
                                .change(
                                        function() {

                                            $
                                                    .getJSON(
                                                            '${getUsersByProject}',
                                                            {
                                                                projectId : $(
                                                                        this)
                                                                        .val(),
                                                                ajax : 'true'
                                                            },
                                                            function(data) {
                                                                var html = '<option value="">--Select Users--</option>';
                                                                var len = data.length;
                                                                for (var i = 0; i < len; i++) {
                                                                    html += '<option value="' + data[i].id + '">'
                                                                            + data[i].firstName + data[i].lastName
                                                                            + '</option>';
                                                                }
                                                                html += '</option>';

                                                                $(
                                                                        '#fixerId')
                                                                        .html(
                                                                                html);
                                                            });
                                        });


                    });
</script>

用於獲取用戶的控制器代碼。

public List<Users> getAllUsersByProjectId(Model model,
        @RequestParam long projectId) {
    List<User> userList = null;
    try {
        //service which will return list of users

    } catch (Exception ex) {
        model.addAttribute(Constants.EXCEPTIONSTRING, ex);
    }
    return userList;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM