简体   繁体   English

将JSP变量传递给html标记属性的最佳实践是什么?

[英]What are the best practices for passing JSP variables to html tag's attributes?

I have a code like <div id="test<%=j%>" class="test" > </div> and my editor shows problems with the matching divs. 我有一个代码,如<div id="test<%=j%>" class="test" > </div> ,我的编辑器显示匹配的div的问题。 I suspect this is responsible for a lot of ghosts in my code where values are not populated in beans, etc. Is this a correct way of doing this? 我怀疑这是我的代码中很多鬼的原因,其中值没有填充bean等。这是一个正确的方法吗? Any guidelines or best practices that I should follow? 我应该遵循的任何指导方针或最佳做法?

This code is syntactically correct. 此代码在语法上是正确的。 The problem is your editor, which doesn't handle JSP scriptlet expressions. 问题是你的编辑器,它不处理JSP scriptlet表达式。

But it's using scriplet expressions, which should not be used anymore (for years). 但它使用的是scriplet表达式,不应再使用(多年)。 Use the JSP expression language, the JSTL, and other custom tag libraries. 使用JSP表达式语言,JSTL和其他自定义标记库。

Avoid any scriptlet in the JSPs. 避免在JSP中使用任何scriptlet。 Java code should be is Java classes (servlets or actions of your preferred MVC framework). Java代码应该是Java类(servlet或首选MVC框架的操作)。

<div id="test${j}" class="test"> </div>

Best practices for JSP 2.0 and later recommend using JSTL in conjunction with Unified Expression Language . JSP 2.0及更高版本的最佳实践建议将JSTL统一表达式语言结合使用。

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
...
<c:set var="j" value="0">
<div id="test${j}" class="test" > </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM