jsp
  • jstl
  • el
  • 2012-05-31 79 views 8 likes 
    8

    我們使用tomcat urlrewrite插件來重寫出站URL和入站URL。爲此,您需要使用JSTL標籤。是否有一個相當於<c:url/>的JSTL EL(表達語言)

    這個偉大的工程爲清潔的網址和國際化,但是它產生醜陋的代碼,包括標籤,內標籤,就像這樣:

    <link href='<c:url value="/content/bootstrap/css/bootstrap.css" />' rel="stylesheet" /> 
    

    或:

    <a href='<c:url value="/nextPage.jsp"/>' />Next Page</a> 
    

    一種替代方法是使用一個變量,像這樣:

    <c:url value="/nextPage.jsp" var="nextPageUrl"/> 
    <a href='${nextPageUrl}' />Next Page</a> 
    

    這真的很乾淨,但冗長。

    是否有表達式語言友好的方式來做到這一點?

    我有點希望是這樣的:

    <a href='${url "/nextPage.jsp}' />Next Page</a> 
    

    感謝。

    +1

    '$ {} pageContext.request.contextPath/nextPage.jsp'?不適用於出站,但它使應用程序內的鏈接非常乾淨。 –

    +0

    雖然沒有解決目標。重點是通過Tomcat提供的整個出站鏈接格式化邏輯以及您已安裝的任何過濾器(如urlrewrite)來運行url。在這種情況下,我們正在做一些比絕對路徑更復雜的事情,我們正在重寫js文件以包含用於緩存清除的構建版本。 – JBCP

    回答

    0

    我最終想出的解決方案是使用自定義標籤來解決CSS和JS的問題。當然,類似的解決方案也可以應用於標籤,儘管我還沒有這方面的需求。

    <my:csslink url="/css/custom.css" /> 
    <my:script url="/script/myscript.js" /> 
    

    的CSS標籤看起來像這樣,JavaScript的一個顯然是非常相似:

    <%@tag language="java" pageEncoding="UTF-8"%> 
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
    
    <%@attribute name="url" required="true" type="java.lang.String" description="the absolute (to the webapp) path of the css file"%> 
    <c:url var="encUrl" value='${url}' /> 
    <link href='${encUrl}' rel='stylesheet' /> 
    

    不要忘了進口標籤在你的JSP:

    <%@ taglib prefix="my" tagdir="/WEB-INF/tags" %> 
    

    定製EL解決方案在某些情況下會更乾淨,但是如果您想要添加對其他參數或屬性的支持,標籤可能就是要走的路。

    2

    沒有什麼現成的,但沒有什麼能夠阻止你寫你自己的EL功能,因此使用這樣的事情:

    <a href='${myFn:url("/nextPage.jsp")}' />Next Page</a> 
    

    我不覺得它比標準C更具可讀性:url標記,但如果它必須接受參數名稱和值,則會更糟糕。

    請參閱http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html瞭解如何定義和使用EL功能。

    +0

    謝謝。我的目標是避免嵌套標籤。嵌套標籤會混淆IDE,並且很難讀取。我同意上面也不是很清楚,但它比標籤更好。我會研究它,謝謝。 – JBCP

    1

    哪裏有幾種方法。

    1)綁定基地變量在共同片段

    common.jspf

    <%@tag language="java" pageEncoding="UTF-8"%> 
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
    <c:url value="/" var="base"/> 
    

    或類似:

    <c:set var="base" value="${pageContext.request.contextPath}"/> 
    

    包括在每個頁面片段:

    <%@include file="base.jspf"%> 
    <script src="${base}/js/jquery.js"></script> 
    <link href="${base}/css/bootstrap.css" rel="stylesheet" type="text/css"> 
    <a href="${base}/index.html">Home</a> 
    

    2)使用${pageContext.request.contextPath}無處不在:

    <script src="${pageContext.request.contextPath}/js/jquery.js"></script> 
    <link href="${pageContext.request.contextPath}/css/bootstrap.css" rel="stylesheet" type="text/css"> 
    <a href="${pageContext.request.contextPath}/index.html">Home</a> 
    

    3)是我最喜歡的:添加過濾器,包括屬性基地:

    /src/java/company/project/web/filter/BaseFilter.java

    import java.io.IOException; 
    import javax.servlet.*; 
    
    /** 
    * Make sane JSP, instead of: 
    * <pre>&lt;a href="&lt;c:url value='/my/path/${id}.html'/&gt;"&gt;Title&lt;/a&gt;</pre> 
    * allow to use: 
    * <pre>&lt;a href="${ctx}/my/path/${id}.html"&gt;Title&lt;/a&gt;</pre> 
    */ 
    public class BaseFilter implements Filter { 
    
        @Override 
        public void init(FilterConfig fc) { 
        } 
    
        @Override 
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
         request.setAttribute("base", request.getServletContext().getContextPath()); 
         chain.doFilter(request, response); 
        } 
    
        @Override 
        public void destroy() { } 
    
    } 
    

    寄存器,濾波器web.xml

    <filter> 
        <filter-name>BaseFilter</filter-name> 
        <filter-class>company.project.web.filter.BaseFilter</filter-class> 
    </filter> 
    <filter-mapping> 
        <filter-name>BaseFilter</filter-name> 
        <url-pattern>*.htm</url-pattern> 
        <url-pattern>*.json</url-pattern> 
    </filter-mapping> 
    

    ,並使用乾淨的語法(如上面但沒有必要包括樣板片段):

    <script src="${base}/js/jquery.js"></script> 
    <link href="${base}/css/bootstrap.css" rel="stylesheet" type="text/css"> 
    <a href="${base}/index.html">Home</a> 
    

    我在該過濾器設置額外的屬性,爲在CSS/JS的開發和精簡版之間切換的示例:

    private String min; 
    
    @Override 
    public void init(FilterConfig fc) { 
        min = fc.getServletContext().getInitParameter("min"); 
        if (min == null) 
         min = fc.getInitParameter("min"); 
        if (min == null) 
         min = "min"; 
    } 
    
    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
        request.setAttribute("ctx", request.getServletContext().getContextPath()); 
        request.setAttribute("min", min); 
        chain.doFilter(request, response); 
    } 
    

    和相應的JSP代碼:

    <script src="${base}/js/jquery.${min}.js"></script> 
    <link href="${base}/css/bootstrap.${min}.css" rel="stylesheet" type="text/css"> 
    

    初始參數min可以不設置在當從外部設定到devmin於與回退,以縮小的版本CONTEX部署描述符WAR文件。

    4)使用scriplets

    <a href="<%=request.getContextPath()%>/index.html">Home</a> 
    
    相關問題