是否有与< c:url/>等效的JSTL EL(表达语言)? [英] Is there an JSTL EL (Expression Language) Equivalent to <c:url/>

查看:67
本文介绍了是否有与< c:url/>等效的JSTL EL(表达语言)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用tomcat urlrewrite插件重写入站URL和入站URL.为此,您需要使用JSTL标记.

We use the tomcat urlrewrite plugin to rewrite outbound URLs as well as inbound ones. To do this, you need to use the JSTL tag.

这对于干净的url和i18n很有用,但是它会产生难看的代码,包括标签内的标签,如下所示:

This works great for clean urls and i18n, however it yields ugly code, including tags-within-tags, like this:

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

或:

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

一种选择是使用变量,如下所示:

One alternative is to use a variable, like this:

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

这真的很干净,但是很冗长.

This is really clean, but verbose.

有表达语言的友好方式吗?

Is there an expression-language friendly way to do this?

我有点希望像这样:

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

谢谢.

推荐答案

我最终想出的解决方案是使用自定义标签解决CSS和JS的问题.当然,尽管我还不需要类似的解决方案,也可以将其应用于标签.

The solution I finally came up with was to use custom tags to solve the problem for CSS and JS. A similar solution could be applied to tags too of course, although I haven't had the need yet.

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

CSS标记看起来像这样,JavaScript显然很相似:

The CSS tag looks like this, the Javascript one is obviously very similar:

<%@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中:

Don't forget to import the tags in your jsp:

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

在某些情况下,自定义EL解决方案会更干净一些,但是如果您想添加对其他参数或属性的支持,则可以使用标签.

The custom EL solution would be cleaner is some circumstances, but if you want to add support for additional parameters or attributes, a tag is probably the way to go.

这篇关于是否有与&lt; c:url/&gt;等效的JSTL EL(表达语言)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆