有没有更好的方法来编写这个JSP自定义标记?

时间:2022-11-26 10:33:54

I'm creating a JSP .tag file that will handle this use case:

我正在创建一个JSP .tag文件来处理这个用例:

<my:safeParam paramName="param1" defaultValue="testvalue"/>

Where the behavior will be to take a request parameter, escape its value for "safe" usage, and place that escaped value back on some scope (e.g. request) under the same name as the parameter (although it could be another name).

行为将采取请求参数,转义其值为“安全”使用,并将该转义值放回某个范围(例如请求),其名称与参数相同(尽管它可能是另一个名称)。

I have an implementation that works, but I've got scriptlet in there because I couldn't find a way to use variable variable names in just JSTL. But I'm no JSTL wizard, so I thought I'd see if there's a syntax/approach I'm missing. Here's the working safeParam.tag file:

我有一个可行的实现,但我在那里有scriptlet因为我找不到在JSTL中使用变量变量名的方法。但我不是JSTL向导,所以我想我会看到是否存在我缺少的语法/方法。这是工作safeParam.tag文件:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<%@ attribute name="paramName" required="true" %>
<%@ attribute name="defaultValue" %>

<%
    String name = (String) pageContext.getAttribute("paramName");
%>

<c:if test="${not empty defaultValue}">
<%
    request.setAttribute(name, pageContext.getAttribute("defaultValue"));
%>
</c:if>

<c:if test="${not empty param[paramName]}">
    <c:set var="escaped" value="${fn:escapeXml(param[paramName])}"/>
<%
    request.setAttribute(name, pageContext.getAttribute("escaped"));
%>
</c:if>

(I sure wish EL was escaped automatically.)

(我当然希望EL自动转义。)

2 个解决方案

#1


0  

<c:if test="${empty paramName}">
    ${defaultValue}
</c:if>
<c:if test="${not empty paramName}">
    <c:out value="${paramName}" escapeXml="true"/>
</c:if>

#2


0  

I probably won't use this approach because it reduces the conciseness I was seeking with this custom tag, but just to document it as an option... (I'm not sure if this is what Frank Yeung is getting at.)

我可能不会使用这种方法,因为它降低了我用这个自定义标签寻求的简洁性,但只是将其作为一个选项进行记录......(我不确定这是否是Frank Yeung所得到的。)

I could make the tag simply output the default-or-escaped parameter value, then make the user of the tag wrap that in a <c:set>.

我可以让标签只输出default-or-escaped参数值,然后让标签的用户在 中包装。

Tag:

<c:choose>
<c:when test="${empty param[paramName]}">
    ${defaultValue}
</c:when>
<c:otherwise>
    <c:out value="${param[paramName]}"/>
</c:otherwise>
</c:choose>

JSP:

<c:set var="myVariable">
    <my:safeParam paramName="folder" defaultValue="homeFolder"/>
</c:set>

But really my goal has been to do everything inside the tag.

但我的目标确实是在标签内做所有事情。

#1


0  

<c:if test="${empty paramName}">
    ${defaultValue}
</c:if>
<c:if test="${not empty paramName}">
    <c:out value="${paramName}" escapeXml="true"/>
</c:if>

#2


0  

I probably won't use this approach because it reduces the conciseness I was seeking with this custom tag, but just to document it as an option... (I'm not sure if this is what Frank Yeung is getting at.)

我可能不会使用这种方法,因为它降低了我用这个自定义标签寻求的简洁性,但只是将其作为一个选项进行记录......(我不确定这是否是Frank Yeung所得到的。)

I could make the tag simply output the default-or-escaped parameter value, then make the user of the tag wrap that in a <c:set>.

我可以让标签只输出default-or-escaped参数值,然后让标签的用户在 中包装。

Tag:

<c:choose>
<c:when test="${empty param[paramName]}">
    ${defaultValue}
</c:when>
<c:otherwise>
    <c:out value="${param[paramName]}"/>
</c:otherwise>
</c:choose>

JSP:

<c:set var="myVariable">
    <my:safeParam paramName="folder" defaultValue="homeFolder"/>
</c:set>

But really my goal has been to do everything inside the tag.

但我的目标确实是在标签内做所有事情。