繁体   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