由於您使用jsp作爲視圖技術,使用核心標籤來決定是否要根據訪問級別顯示綠色勾號或紅色叉號。
訪問this site瞭解更多關於核心標籤的使用。不要忘記在項目類路徑中包含jstl.jar和standard.jar文件。它們是支持jstl的必要庫。
看起來你的應用程序是使用spring框架開發的,所以我會試着僅以這種方式解釋它。
你的JSP代碼將是這樣的:它命名爲userlist.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!doctype>
<html>
<head>
<script src="${pageContext.request.contextPath}/js/jquery-1.3.2.min.js" type="text/javascript></script>
<script src="${pageContext.request.contextPath}/js/jquery.dd.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/dd.css" />
</head>
<body>
<form:select id="userNames" path="userName" tabindex="10">
<form:option value="Select User">Select User</form:option>
<c:forEach begin="${userlist begin index (0)}" end="${userlist size}" var="i">
<c:choose>
<c:when test="${userNameList.user.accessLevel == 1}">
<form:option style="background-image:url(greentick.png);" value="${userNameList.user.userName}">${userNameList.user.userName}</form:option>
</c:when>
<c:otherwise>
<form:option style="background-image:url(redcross.png);" value="${userNameList.user.userName}">${userNameList.user.userName}</form:option>
</c:otherwise>
</c:choose>
</c:forEach>
</form:select>
</body>
</html>
現在你將有一個控制器,它會調用一些動作之後被調用,它將返回這個JSP以及userNameList。 以下是示例UserController。java的
@Controller
public class UserController {
@RequestMapping(value = "/showUsers", method = RequestMethod.GET)
public String showUserInfo(Model model) {
// here you prepare the userList, the list of Users along with information
// here User can be fetched from DB & values stored in User DTO and then DTO in the list
List<User> userNameList = new ArrayList<User>();
userNameList.add(User DTO objects go here);
model.addAttribute("userNameList", userNameList);
return "userlist"; // remember this is our jsp name
}
}
&用戶DTO可以是這樣的。 下面是樣品User.java
public class User {
private String userName;
private int accessLevel;
// setters & getters of variables
}
這不能完全闡明答案。我盡力解釋。 你試試這個。它應該工作。
你已經收到我的答案。那爲什麼還有其他的賞金呢? –