我有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>
我之前需要选择项目,为了从这个项目中获取用户列表,我该如何实现?感谢。
答案 0 :(得分:0)
您需要使用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;
}