jstl自定义标签

jstl标签可以 方便页面的调用,通过直接访问页面就可以调用函数返回数据并进行页面显示
1、新建后台调用的java类:
package com.jrcz.aura.util;

import java.util.List;
import org.springframework.stereotype.Component;
import com.jrcz.aura.model.Employee;
import com.jrcz.aura.service.EmployeeManager;

@Component
public class EmployeeFunction {
 private static EmployeeManager employeeManager;
 
 public static  List<Employee> getEmp(){
  int deptid = 1;
  List<Employee> list = null;
//  List<Employee> list = employeeManager.getEmpByDeptID(deptid);
//  System.out.println("list:"+list.size());
  System.out.println("jstl测试");
  return list;
 }

 public static void setEmployeeManager(EmployeeManager employeeManager) {
  EmployeeFunction.employeeManager = employeeManager;
 }
}

2、写tld文件:
<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  version="2.0">
   
  <tlib-version>1.0</tlib-version>
  <short-name>gpb_jstl</short-name>
  <uri>http://www.jrcz.com/gpb_jstl/functions</uri>
 
  <function>
    <name>getEmp</name>
    <function-class>com.jrcz.aura.util.EmployeeFunction</function-class>
    <function-signature>java.util.List getEmp()</function-signature>
  </function>
   
</taglib>
3、在web.xml文件中配置:
 <!-- 配置jstl标签 -->
 <jsp-config>
  <taglib>
  <taglib-uri>http://www.jrcz.com/gpb_jstl/functions</taglib-uri>
  <taglib-location>/WEB-INF/gpb_jstl.tld</taglib-location>
  </taglib>
 </jsp-config>

4、jsp页面调用:
在页面头部引用
<%@ taglib uri="http://www.jrcz.com/gpb_jstl/functions" prefix="gpb_jstl" %>
页面显示层
  <c:forEach items="${gpb_jstl:getEmp()}" var="model" varStatus="st">
      <tr valign="middle" <c:if test="${st.index % 2 != 0}">bgcolor="#e6f7fe"</c:if><c:if test="${st.index % 2 == 0}">bgcolor="#f7fcff"</c:if>>
        <td align="center"><input type="checkbox" name="items" value="empid=${model.empid}&" id="items" /></td>
        <td >${model.empid}</td>
        <td >${model.ename}</td>
        <td >${model.salary}</td>
        <td >${model.dept.dname}</td>
        <td >${model.location}</td>
      </tr>
      </c:forEach>

你可能感兴趣的:(jsp,function,list,Class,input,encoding)