将 href 链接放入 Struts2 set 标记中

发布于 2024-12-11 09:19:54 字数 723 浏览 2 评论 0原文

我想将锚标记放入 Struts2 变量中,然后可以在 JSP 中的各个位置显示该变量。这样我就可以避免重复用于构建各种形式的 href 标记的复杂 if/then 语句。

我使用 Struts 's:set' 标签来执行此操作。

问题在于 Struts 转换了 '<'字符到 htmlentities,当页面显示时,我看到实际显示的“a href=someURL”标签,而不是工作链接。

如果我将“escape='true'”参数添加到 s:set ,它会执行相同的操作,只是它显示 '<' 的 htmlentities和“>”标签。

如何将有效的锚标记放入 Struts2 变量中,然后将其显示为工作链接?

这就是我正在做的:

<s:set name="composerName">

<s:property value="'a href=%{viewRecordURL}>'" escape="false"/>

<s:property value="#composer.title" />

<s:property value="#composer.firstName" />

<s:property value="#composer.lastName" />

<s:property value="'</a>'" escape="false" />

</s:set>

I want to put an anchor tag into a Struts2 variable that I can then display at various points in a JSP. That way I can avoid duplicating the complicated if/then statements that are used to build various forms of the href tag.

I'm using the Struts 's:set' tag to do this.

The problem is that Struts converts the '<' characters to htmlentities and when the page displays I see the actual "a href=someURL" tag displayed, not a working link.

If I add the "escape='true'" argument to s:set it does the same thing, only it displays the htmlentities for the '<' and '>' tags.

How do I put a valid anchor tag into a Struts2 variable and then display it as a working link?

Here's what I'm doing:

<s:set name="composerName">

<s:property value="'a href=%{viewRecordURL}>'" escape="false"/>

<s:property value="#composer.title" />

<s:property value="#composer.firstName" />

<s:property value="#composer.lastName" />

<s:property value="'</a>'" escape="false" />

</s:set>

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

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

发布评论

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

评论(6

浮云落日 2024-12-18 09:19:54

使用 s:url 创建有效的 url,然后在 s:a 中使用它来生成链接。

您可以参考下面的链接了解更多详细信息:

http://struts.apache.org /2.1.8/docs/a.html

Use s:url to create a valid url and then use it in s:a to generate the link.

You can refer the link below for more details:

http://struts.apache.org/2.1.8/docs/a.html

囍笑 2024-12-18 09:19:54

我们可以使用 url 标签来创建 URL,如下所示

<s:url id="hLink" action="yourStrutsActionName">
   <s:param name="propertyName" value="%{propertyName}" />
</s:url>
<td><s:a href="%{hLink}"><s:property value="%{propertyName}"/></s:a></td> 

We can use url tag to create URL like mention below

<s:url id="hLink" action="yourStrutsActionName">
   <s:param name="propertyName" value="%{propertyName}" />
</s:url>
<td><s:a href="%{hLink}"><s:property value="%{propertyName}"/></s:a></td> 
我做我的改变 2024-12-18 09:19:54

在 Struts2 中设置链接的简单方法
// 在href中添加Struts2动作

<s:a href="forgetPasswordPage.action" >Forget Password</s:a>

do this easy way to set the link in Struts2
// add the Struts2 action in href

<s:a href="forgetPasswordPage.action" >Forget Password</s:a>
梦里°也失望 2024-12-18 09:19:54

这不是您想要的构建方式——您拥有的是两个不同的东西,一个 URL 和该 URL 的文本字符串。您已有的网址。仅使用 作为链接文本。

如果您确实需要,您可以将其包装到基于 JSP 的自定义标记中,但我不会打扰。

This isn't how you want to build this--what you have is two different things, a URL, and the text string of that url. The URL you already have. Only use <s:set> for the link text.

If you really need to, you can wrap this up into a JSP-based custom tag, but I wouldn't bother.

梦幻之岛 2024-12-18 09:19:54

我会回答我自己的问题。也许其他人有更优雅的解决方案。我们再次尝试将锚标记和 url 放入 as:set 变量中。问题有两个:1)让 s:set 将标签字符串视为文字字符串而不是对象(它想从 ActionContext 或其他地方获取),2) 关闭转义,以便锚点的字符串文本标签不会转换为 html 实体,例如 '& lt;'。

对于 1,我将 var 设置为 null,但提供默认参数,s:property 始终将其视为文字字符串。

对于#2,只需在正确的位置使用 escape="false" 即可。

不幸的是,该字符串必须使用 s:property 的各个位来构建,但是您能做什么呢?

<s:set name="composerName" >
    <s:property value="" default="<a href=" escape="false"/> 
    <s:property value="" default="'" />
    <s:property value="%{viewRecordURL}" escape="true"/>
    <s:property value="" default="'>" escape="false" />
    <s:property value="#composer.title" />
    <s:property value="#composer.firstName" />
    <s:property value="#composer.lastName" />
    <s:property value="" default="</a>" escape="false"/>
</s:set>

我尚未使用 UTF-8 字符或 #composer 部分中的“&”、撇号或单引号等可能有问题的字符进行测试。

I'll answer my own question. Maybe someone else has a more elegant solution. Once again, we're trying to get both an anchor tag and a url into a s:set variable. The issues were two: 1) getting s:set to treat the tag string as a literal string and not an Object (which it wants to fetch from ActionContext or somewhere), and 2) turning off escaping so that the string text of the anchor tag isn't converted into htmlentities like '& lt;'.

For 1, I set the var to null, but provide a default argument, which s:property always treats as a literal string.

For #2, it's just a matter of using escape="false" in the right places.

Unfortunately, the string has to be built up with separate bits of s:property, but what can you do?

<s:set name="composerName" >
    <s:property value="" default="<a href=" escape="false"/> 
    <s:property value="" default="'" />
    <s:property value="%{viewRecordURL}" escape="true"/>
    <s:property value="" default="'>" escape="false" />
    <s:property value="#composer.title" />
    <s:property value="#composer.firstName" />
    <s:property value="#composer.lastName" />
    <s:property value="" default="</a>" escape="false"/>
</s:set>

I haven't tested it yet with UTF-8 characters or potentially problematic characters like '&', apostrophe, or single quote in the #composer part.

茶花眉 2024-12-18 09:19:54

我遇到了同样的问题,我只是按以下方式解决了它:

在主 bean 中,我有以下具有 Setters 和 Getters 的变量:

private int ReqNo;
private String hyperLinkToPage;

在 JSP 页面中,我有如下 TD:

    <td class="displayValue" style="width: 10%;"><s:a href="%{hyperLinkToPage}"><s:property value="%{ReqNo}"/></s:a></td> 

我希望它会有所帮助。

I had the same issue, and I just solved it as following:

In the main bean, I have the following variables with Setters and Getters:

private int ReqNo;
private String hyperLinkToPage;

In the JSP page I have the TD as following:

    <td class="displayValue" style="width: 10%;"><s:a href="%{hyperLinkToPage}"><s:property value="%{ReqNo}"/></s:a></td> 

I hope that it will help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文