简体   繁体   中英

Dynamically retrieve the object, Spring MVC, JSP

I have jsp page for create a new issue (Entity in my project), using this part of code:

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

I select the project for join current issue to this selected above project. Next on the same page here:

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

I need to get project selected before, for getting users list from this project, how can I implement that? Thanks.

You need to use ajax call to fetch list of users and controller to return list through json response.

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

Controller Code to fetch users.

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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