指定JSTL< c:set>的区别通过属性和主体的价值 [英] Difference between specifying JSTL <c:set> value via attribute and body

查看:60
本文介绍了指定JSTL< c:set>的区别通过属性和主体的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚这两个JSTL <c:set>标记用法之间是否存在功能差异.假设我已通过以下方式将一些字符串常量导入了我的JSP页面:

I'm trying to figure out if there's any functional difference between these two uses of the JSTL <c:set> tag. Let's assume I have imported some string constants into my JSP page via something like:

<%@ page import="some.package.of.AppConstants"%>

因此,鉴于上述import指令,这两个示例之间有什么区别:

So, given the above import directive, what's the difference between these two examples:

  1. value属性中指定值:

<c:set var="SOME_VAR" value="${AppConstants.SOME_CONSTANT}" />

  • 指定标签正文中的值:

  • Specifying the value in tag body:

    <c:set var="SOME_VAR">
        <%=AppConstants.SOME_CONSTANT%>
    </c:set>
    

  • 第二个示例与第一个示例中,${SOME_VAR}的值是否存在差异?在这两种情况下,AppConstants.SOME_CONSTANT中是否存在任何特殊字符都会影响${SOME_VAR}的值?

    Is there any difference in what the value of ${SOME_VAR} will be in the second example versus the first example? Would the presence of any special characters in AppConstants.SOME_CONSTANT affect the value of ${SOME_VAR} in either of these cases?

    推荐答案

    属性中的jstl c:set值与标记主体之间的区别

    个技术差异.

    value保留原始值类型.例如,如果您提供了Integer或其他任何内容,它将保留为Integer.但是,主体基本上对值进行了toString()运算,因此实际上将任何类型都转换为String.在某些情况下这可能是有益的,例如:

    The value keeps the original value type. E.g., if you supplied Integer or whatever, it will stay Integer. The body, however, basically does a toString() on the value and thus essentially converts any type to String. This can be beneficial in some cases, e.g.:

    • Convert integer value to string using JSTL/EL
    • How to find length of an integer in JSP?
    • How to check isNumeric/IsNumber in JSTL
    • how to get the base url from jsp request object?

    回到具体的功能需求,第一个示例仅在您使用web.xml与Servlet 3.1匹配的webapp定位Servlet 3.1/EL 3.0容器(Tomcat 8等)时才有效.

    Coming back to the concrete functional requirement, the first example will only work if you're targeting a Servlet 3.1 / EL 3.0 container (Tomcat 8, etc) with a webapp whose web.xml matches Servlet 3.1.

    <c:set var="SOME_VAR" value="${AppConstants.SOME_CONSTANT}" />
    

    即,仅从EL 3.0开始才支持此功能.相对于 Scriptlet 方法,这绝对是首选方法.您应该不惜一切代价避免使用 Scriptlets .您只需要记住,该代码就不能与较早版本的目标运行时向下兼容.

    Namely, this is only supported since EL 3.0. This is definitely the preferred approach over the Scriptlet way. You should try to avoid Scriptlets at all cost. You only need to keep in mind that the code is not backwards compatible with older versioned target runtimes.

    • How to reference constants in EL?
    • How to avoid Java code in JSP files?

    这篇关于指定JSTL&lt; c:set&gt;的区别通过属性和主体的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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