Spring MVC 标签与自定义标签交互

发布于 2024-07-11 08:59:07 字数 641 浏览 6 评论 0原文

我有一个使用 Spring:form 标签将控件绑定到命令对象的 JSP。

我想修改它如下:如果[某些条件为真]则显示控件; 否则,只显示文本。 (示例:如果用户是管理员,则显示控件,否则仅显示文本。如果 Whatsit 仍打开进行修改,则显示控件,否则显示文本。)

换句话说,我想要这样:

<c:choose>
     <c:when test="SOME TEST HERE">
          <form:input path="SOME PATH" />
     </c:when>
     <c:otherwise>
          <p>${SOME PATH}</p>
     </c:otherwise>
</c:choose>

但我想要为每个领域(有很多)创建此内容的简单方法。

如果我创建一个自定义标签来生成上述文本(给定“SOME PATH”),Spring 自定义标签会被绑定吗?

我想我真正要问的是:我可以创建自定义标签来生成然后绑定的 Spring 自定义标签吗? 或者所有自定义标签(我的和 Spring 的)是否同时处理?

I have a JSP that is using Spring:form tags to bind controls to a command object.

I would like to modify it as follows: if [some condition is true] than display the controls; otherwise, just display the text. (Examples: if the user is an Admin, display the controls, otherwise just display the text. If the whatsit is still open for modification, display the controls, otherwise display the text.)

In other words, I want this:

<c:choose>
     <c:when test="SOME TEST HERE">
          <form:input path="SOME PATH" />
     </c:when>
     <c:otherwise>
          <p>${SOME PATH}</p>
     </c:otherwise>
</c:choose>

But I want an easy way to create this for every field (there are many).

If I create a custom tag to generate the above text (given "SOME PATH"), will the Spring custom tags get bound?

I guess what I'm really asking is: can I create custom tags that generate Spring custom tags that then get bound? Or do all custom tags (mine and Spring's) get handled simultaneously?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2024-07-18 08:59:07

通常,唯一的解决方案就是尝试。

我尝试了三种不同的方法——JSP 自定义标记库、参数化 JSP 包含和 JSP2 标记文件。

前两个不起作用(尽管我怀疑标签库可以起作用),但是标签文件起作用了! 该解决方案大致基于 Expert Spring MVC 和 Web Flow 中给出的示例。

这是我在 WEB-INF/tags/renderConditionalControl.tag 中的代码:

<%@ tag body-content="tagdependent" isELIgnored="false" %>
<%@ attribute name="readOnly" required="true" %>
<%@ attribute name="path" required="true" %>
<%@ attribute name="type" required="false" %>
<%@ attribute name="className" required="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="/WEB-INF/spring-form.tld" %>
<%@ taglib prefix="spring" uri="/WEB-INF/spring.tld" %>

<c:if test="${empty type}">
<c:set var="type" value="text" scope="page" />
</c:if>

<spring:bind path="${path}">
    <c:choose>
        <c:when test="${readOnly}">
            <span class="readOnly">${status.value}</span>
        </c:when>
        <c:otherwise>
           <input type="${type}" id="${status.expression}" name="${status.expression}"
                    value="${status.value}" class="${className}" />
        </c:otherwise>
    </c:choose>
</spring:bind>

这是 jsp 中的代码:

首先,使用其他 taglibs 指令:

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

并在表单中:

<tag:renderConditionalControl path="someObject.someField" type="text" readOnly="${someBoolean}" className="someClass" />

Frequently the only solution is to try it.

I tried it three different ways -- a JSP custom tag library, a parameterized JSP include, and a JSP2 tag file.

The first two didn't work (although I suspect the tag library can be made to work), but the tag file did! The solution was based loosely on an example given in Expert Spring MVC and Web Flow.

Here's my code in WEB-INF/tags/renderConditionalControl.tag :

<%@ tag body-content="tagdependent" isELIgnored="false" %>
<%@ attribute name="readOnly" required="true" %>
<%@ attribute name="path" required="true" %>
<%@ attribute name="type" required="false" %>
<%@ attribute name="className" required="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="/WEB-INF/spring-form.tld" %>
<%@ taglib prefix="spring" uri="/WEB-INF/spring.tld" %>

<c:if test="${empty type}">
<c:set var="type" value="text" scope="page" />
</c:if>

<spring:bind path="${path}">
    <c:choose>
        <c:when test="${readOnly}">
            <span class="readOnly">${status.value}</span>
        </c:when>
        <c:otherwise>
           <input type="${type}" id="${status.expression}" name="${status.expression}"
                    value="${status.value}" class="${className}" />
        </c:otherwise>
    </c:choose>
</spring:bind>

And here's the code in the jsp:

First,with the other taglibs directives:

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

and within the form:

<tag:renderConditionalControl path="someObject.someField" type="text" readOnly="${someBoolean}" className="someClass" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文