How to import my imports in JSP?

2019-08-02 16:38发布

问题:

All of my JSPs have this at the top:

<%@ page language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tag" %>

Besides being repetitive, I have had a few hard-to-find bugs because of occasional inconsistencies. For example, a few of my JSP files were missing, isELIgnored="false", causing untold havoc.

Is there any way to put these imports into a file and then import that file in each JSP, rather than listing all of these imports in each file?

回答1:

Yes, I always use an include.jsp such as:

<%@ page session="false"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="/spring-form" %>
<%@ taglib prefix="spring" uri="/spring" %>   
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>

and then reference that in the page using the import statement:

<%@ include file="/WEB-INF/jsp/include.jsp" %>