使用jstl out标签真的是最佳实践吗?

时间:2022-11-26 07:34:03

I remember working on a project with a group of developers and they always wanted static html text to be inside of an out tag (<c:out value="words" />). I don't remember why this was the case.

我记得我和一组开发人员一起做过一个项目,他们总是希望静态html文本放在out标记的内部( )。我不记得为什么会这样。

Is this really a best practice when building jsp pages? What are the advantages/disadvantages of such an approach?

在构建jsp页面时,这真的是最佳实践吗?这种方法的优点/缺点是什么?

2 个解决方案

#1


10  

It is a terrible idea for static text. You then have no barrier as to what is static and what is dynamically generated.

对于静态文本来说,这是一个糟糕的想法。那么,对于什么是静态的,什么是动态生成的,就没有障碍了。

Besides which, on Servlet Spec 2.3+ you can have dynamic text mixed with static text as:

此外,在Servlet Spec 2.3+中,您可以将动态文本与静态文本混合为:

This is static, not ${dynamic} text.

这是静态的,不是${dynamic}文本。

The only reasons to use c:out tags, in my experience:

根据我的经验,使用c:out标签的唯一原因是:

  1. You're using an older servlet spec, and need them to output DYNAMIC text in some fashion

    您正在使用一个旧的servlet规范,并需要它们以某种方式输出动态文本

  2. You want to escape HTML output to avoid using <>, etc, replacing ampersands with their control codes, etc.

    您希望转义HTML输出,以避免使用<>等,用它们的控制代码替换符号,等等。

Otherwise, having them use static text confuses the programmer or maintainer...now where did I put that EL? It was in a c:out tag...but so was fifty other lines of static text!

否则,让他们使用静态文本会让程序员或维护者感到困惑……我把EL放在哪里了?在c:out标签中……但是另外50行静态文本也是如此!

#2


3  

If you're just printing out plain text it's better to do it in HTML. The advantage of the c:out tag is that you can evaluate expressions inside the tag.

如果你只是打印纯文本,最好是用HTML。c:out标记的优点是可以计算标记内部的表达式。

<c:out value="Hello ${user.firstName} ${user.lastName}"/>

#1


10  

It is a terrible idea for static text. You then have no barrier as to what is static and what is dynamically generated.

对于静态文本来说,这是一个糟糕的想法。那么,对于什么是静态的,什么是动态生成的,就没有障碍了。

Besides which, on Servlet Spec 2.3+ you can have dynamic text mixed with static text as:

此外,在Servlet Spec 2.3+中,您可以将动态文本与静态文本混合为:

This is static, not ${dynamic} text.

这是静态的,不是${dynamic}文本。

The only reasons to use c:out tags, in my experience:

根据我的经验,使用c:out标签的唯一原因是:

  1. You're using an older servlet spec, and need them to output DYNAMIC text in some fashion

    您正在使用一个旧的servlet规范,并需要它们以某种方式输出动态文本

  2. You want to escape HTML output to avoid using <>, etc, replacing ampersands with their control codes, etc.

    您希望转义HTML输出,以避免使用<>等,用它们的控制代码替换符号,等等。

Otherwise, having them use static text confuses the programmer or maintainer...now where did I put that EL? It was in a c:out tag...but so was fifty other lines of static text!

否则,让他们使用静态文本会让程序员或维护者感到困惑……我把EL放在哪里了?在c:out标签中……但是另外50行静态文本也是如此!

#2


3  

If you're just printing out plain text it's better to do it in HTML. The advantage of the c:out tag is that you can evaluate expressions inside the tag.

如果你只是打印纯文本,最好是用HTML。c:out标记的优点是可以计算标记内部的表达式。

<c:out value="Hello ${user.firstName} ${user.lastName}"/>