有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JSP/Jquery组合框下拉列表,通过图像从数据库动态加载

在我的web应用程序中,我需要显示从数据库动态加载的下拉列表
我需要从DB加载用户列表。每个用户的“访问级别”为1或2
当用户显示在下拉列表中时,他们的名字旁边必须有一个图像
(如“通行级别”1的“绿色”勾选)和(通行级别2的“红色十字”)
我已经检查了这个url http://www.marghoobsuleman.com/jquery-image-dropdown
但我需要根据从DB加载的访问级别显示图像
我想这可以通过JQuery/CSS实现

有人能告诉我怎么做吗?如果可能的话,请给出示例代码


共 (4) 个答案

  1. # 1 楼答案

    我以前做过类似的事情,我使用了JQuery flexbox插件。它使用起来很简单。我现在没有密码,但是在这里(http://flexbox.codeplex.com/)是一个网站,你可以在那里寻找如何实现它

  2. # 2 楼答案

    在网页所需的位置编写以下代码,以获取并列出用户列表

    rst=stmt.executeQuery("select * from userliat");
    <select id="users" name="users">;
    <%
    String imagePath;
    while(rst.next()){
      if(rst.getString("access_level") == "1"){
        imagePath = "greenTick.png";
      }
      else{
        imagePath = "redCross.png";
      }
    %>;
    <option value="<%= rst.getString("id") %>" title="<%= imagePath %> "><%= rst.getString("user_name") %></option>;
    <% } %>
    </select>
    

    在你的头部分包括以下javascript/CSS插件

    <script src="msdropdown/js/jquery-1.3.2.min.js" type="text/javascript></script>
    <script src="msdropdown/js/jquery.dd.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="msdropdown/dd.css" />
    

    在文档准备功能中使用下面的javascript(它位于标题部分):

    <script language="javascript">
        $(document).ready(function(e) {
            try {
                $("#users").msDropDown();
            } catch(e) {
                alert(e.message);
            }
        });
    </script>
    
  3. # 3 楼答案

    我没有JSP的知识,但是,我可以为您提供足够的信息,这样您就可以继续使用您的知识

    HTML部分:

    <html>
    <head>
    <title>Sample Title</title>
    <script src="msdropdown/js/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="msdropdown/js/jquery.dd.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="msdropdown/dd.css" />
    <script language="javascript">
        $(document).ready(function(e) {
            try {
                $("#webmenu").msDropDown();
            } catch(e) {
                alert(e.message);
            }
        });
    </script>
    </head>
    <body>
    <select name="webmenu" id="webmenu">
        //you will need to take this part into the loop of x being count of total users, and loop from first to last
        <option value="<% //output username lowercased here %>" title="<% if(accessLevel == 1){ //printout imagepath for accessLevel = 1 } else if(accessLevel == 2){ //printout imagepath for accessLevel = 2 %>"><% /*output username here*/ %></option>
    </select>
    </body>
    </html>
    
  4. # 4 楼答案

    由于您使用的是jsp作为查看技术,所以使用核心标记根据访问级别决定是显示绿色记号还是显示红色十字

    访问this site了解更多关于核心标签的用法。别忘了包括jstl。罐子和标准瓶。项目类路径中的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以及用户名列表。 下面是示例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可以是这样的。 下面是示例用户。java

    public class User {
        private String userName;
        private int accessLevel;
    
        // setters & getters of variables
    }
    

    这不是一个完整的答案。我已经尽力解释了。 你试试看。应该行得通