0

我有一个 List 集合,其中包含 DB 中的经理和员工姓名列表。And I have another list which holds project list in a dropdownlist... When one project is selected then table must be created according to this projecet's emloyees and manager in a table... For example...

从下拉列表中选择的 AndroidApp 项目 然后它应该创建一个表,其中包含像这样的经理和员工的姓名... Html 表... 我尝试使用 jstl 创建,但我做不到。

|manager| Employee|

 Susan   |  John

 Susan   |   Joe

 Susan   |  Megan

我正在使用 Java 平台开发 web-app 项目。我有 Servlet、JSP、DB 来做到这一点。我对列表等进行了编码。我认为此时我的问题是 html 编码或其他可能是什么?我的代码...

相关的 JSP 页面是项目...

  <%@page import="com.eteration.leavesystem.model.Person"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ page session="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="">
        <select name="projects" multiple>
            <option value="volvo">Volvo</option>
            <option value="saab">Saab</option>
            <option value="opel">Opel</option>
            <option value="audi">Audi</option>
        </select> <input type="submit" value="Kaydet">
    </form>
    <br>

    <script
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">

    </script>
    <script>
        $(function(){
            $("#addProject").click(function(){
                $(this).next().slideToggle();
            });
        });
    </script>
    <button id="addProject">Proje Ekle</button>

    <div style="display:none;">
        <form action="./UserProjectServlet" method="POST">
            <input type="text" name="AddedProject" /><br/>
            <button class="hidden">Ekle</button>
        </form>
    </div>
<form action="./PopulateProjectServlet" method="get">
<select name="selectedProject" id="selectedProject" onchange= "this.form.submit()" >
    <c:forEach items="${projects}" var="project">
        <option value="${project.code}">${project.desc}"</option>
    </c:forEach>
    <table border="1" cellpadding="5">
            <caption><h2>List of users</h2></caption>
            <tr>
                <th>Manager</th>
                <th>Employees</th>         
            </tr>
            <c:forEach items="${projectTable}" var="projectTable">
                <tr>
                    <td><c:out value="${projectTable.pm.name}" /></td>
                    <td><c:out value="${projectTable.e.name}" /></td>
                </tr>
            </c:forEach>
        </table>
</select>
</form>
</body>
</html>

我成功地将项目列出到下拉列表并从中获取选定的值 id。然后我做了 onchange 方法来列出有名字的项目信息。

我的结果类列出了项目表信息(名称)

public class ProjectResultTableView {

    static mysqlCon con=new mysqlCon();

    public static List<ProjectTable> getProjectInfo(String projectId) throws SQLException {

        List<ProjectTable> projectTableList = new ArrayList<ProjectTable>();
        String query = "SELECT distinct e.name AS employee_name, pm.name AS manager_name FROM project_employee AS pe LEFT OUTER JOIN employee AS e ON e.employee_id = pe.employee_id LEFT OUTER JOIN project AS p ON p.project_id = pe.project_id LEFT OUTER JOIN employee AS pm ON pm.employee_id = p.manager_id WHERE pe.project_id =" +projectId+ " ;";

        Statement stmt = con.getConnection().createStatement();
        ResultSet rs = stmt.executeQuery(query);

        if(rs!=null){

        while (rs.next()) {

            projectTableList.add(new ProjectTable(rs.getString("pm.name"),rs.getString("e.name")));

        }

        }


        con.getConnection().close();
        return projectTableList;
    }


}

我的伺服器

@WebServlet("/ProjectResultServlet")
public class ProjectResultServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public ProjectResultServlet() {
        super();

    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



        try {
            String selectedProject = request.getParameter("selectedProject");   

            List<ProjectTable> projectTable = ProjectResultTableView.getProjectInfo(selectedProject);
            request.setAttribute("projectTable", projectTable); 
            request.getRequestDispatcher("/UserHome/Projects.jsp").forward(request, response);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




        request.getRequestDispatcher("/UserHome/Projects.jsp").forward(request, response);
    }

}

所以我认为它与我的 html 代码中的 jstl 或某些东西有关。如果还有更多错误请帮助我.. 我的问题是如何在不提交按钮的情况下根据所选项目创建表格。我想使用 onchange 或类似的东西做同样的事情:) 在此先感谢...

4

1 回答 1

0

由于您已经在使用 Jquery,因此您可以捕获 onchange 事件。使用 selectedproject 执行 Ajax 访问 servlet 并检索新表。将结果数据加载到 DIV 中。

http://api.jquery.com/jQuery.get/

于 2013-08-22T08:17:26.353 回答