簡體   English   中英

如何將JSP頁面包含到自定義標簽中

[英]How to include a JSP page into a custom tag

我正在創建一個自定義標記指令,在該指令中我必須從數據庫中打印數據表。

到目前為止,我已經創建了一個JSP頁面

 <%@taglib uri="/WEB-INF/tlds/fact.tld" prefix="veritis"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>       
        <h1><veritis:print name="emp" /></h1> 
    </body>
</html>

和一個tld文件

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
 <tlib-version>1.0</tlib-version>
 <short-name>factorial</short-name>
  <tag>
      <name>print</name>
      <tag-class>com.veritis.jsp.FactTag</tag-class>
      <body-content>empty</body-content>
    <attribute>
        <name>name</name>
        <required>false</required>
    </attribute>
  </tag>
</taglib>

一個Class文件,使用其他Class文件從數據庫中獲取數據。 此類文件還擴展了TagSupport以覆蓋所需的tld方法

public class FactTag extends TagSupport{
    String value="emp";
    public String getValue() {
        return value;    }

    public void setValue(String value) {
        this.value = value;
    }
    public int doStartTag(){
        return Tag.SKIP_BODY;
    }
    public int doEndTag() throws JspTagException{

        String emp1=getValue();

       try{
           //The following line is to get the JSP Writer Object
           //similar to PrintWriter in Servlet
           JspWriter out=pageContext.getOut();

           TableData tbd=new TableData();
           List<Employee> listOfEmp=tbd.getAllEMployees();
           for(Employee emp:listOfEmp)
           {
               System.out.println(emp);
           }
       }catch(Exception e){}
       return Tag.SKIP_PAGE;
    }

}

現在我的List<emp>有數據,我想將其打印在JSP文件中。 我可以通過使用out對象來打印出來,但我的要求是不要使用out對象,而是通過包括其他JSP文件或任何其他方式來打印。

注意:盡管我正在從JSP中獲取input(emp) ,但我暫時不使用它。 而是我正在生成一個靜態查詢來獲取數據

您可以使用JSTL c:forEach標記遍歷Employee列表。 如果您不想使用基於Struts2或Spring MVC的servlet的框架,則可以使用原始servlet。

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

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TableData tbd=new TableData();
        List<Employee> listOfEmp=tbd.getAllEMployees();        
        request.setAttribute("listOfEmp", listOfEmp);

        RequestDispatcher rd = getServletContext().getRequestDispatcher("/emloyee.jsp");
        rd.forward(request, response);
    }

}

employee.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP Page</title>
<style>
table,th,td
{
border:1px solid black;
}
</style>
</head>
<body>
<%-- Using JSTL c:forEach and c:out to loop a list and display items in a table --%>
<table>
<tbody>
<tr><th>ID</th><th>Name</th><th>Role</th></tr>
<c:forEach items="${requestScope.listOfEmp}" var="emp">
<tr><td><c:out value="${emp.id}"></c:out></td>
<td><c:out value="${emp.name}"></c:out></td>
<td><c:out value="${emp.role}"></c:out></td></tr>
</c:forEach>
</tbody>
</table>
<br><br>
</body>
</html>

您還可以使用包含表的JSP片段頁面,並將其包含在主頁中。

employee.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP Page</title>
<style>
table,th,td
{
border:1px solid black;
}
</style>
</head>
<body>
<c:import url="employee_table.jsp"/>
<br><br>
</body>
</html>

employee_table.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%-- Using JSTL c:forEach and c:out to loop a list and display items in a table --%>
<table>
<tbody>
<tr><th>ID</th><th>Name</th><th>Role</th></tr>
<c:forEach items="${requestScope.listOfEmp}" var="emp">
<tr><td><c:out value="${emp.id}"></c:out></td>
<td><c:out value="${emp.name}"></c:out></td>
<td><c:out value="${emp.role}"></c:out></td></tr>
</c:forEach>
</tbody>
</table>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM