繁体   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