Struts2锚标记不包含contextPath [英] Struts2 anchor tag doesn't include contextPath

查看:98
本文介绍了Struts2锚标记不包含contextPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

%{#request.contextPath}在Struts2的s:a标记内不起作用. (具体来说,Struts 2.2.1.)是否有使其工作的方法?它可以在其他Struts2标签中使用.

%{#request.contextPath} doesn't work inside an s:a tag in Struts2. (Struts 2.2.1 to be specific.) Is there a way to make it work? It works in other Struts2 tags.

在Struts 2项目的JSP文件中,以下两行的上下文路径为"/websites":

Here are two lines in a JSP file in a Struts 2 project whose context path is "/websites":

<s:a href="%{#request.contextPath}/clickme" theme="simple">Click here.</s:a>
<s:form method="post" action="%{#request.contextPath}/submitme" theme="simple"></s:form>

这是输出:

<a href="/clickme">Click here.</a>
<form id="submitme" name="submitme" action="/websites/submitme" method="post"></form>

请注意,上下文路径已脱离锚点,但已包含在表单中.

Notice that the context path is left off the anchor but is included in the form.

P.S.我在这里不能使用${#pageContext.request.contextPath},因为Struts2标记中不允许使用${}.此外,我正在努力保持一致.而且我也通常尝试避免使用${},因为它不会自动转义输出.

P.S. I can't use ${#pageContext.request.contextPath} here because ${} isn't allowed in Struts2 tags. Besides, I'm trying to be consistent. And I also try generally to avoid ${} since it does not auto-escape the output.

谢谢!

推荐答案

这应该有效:

<s:set id="contextPath"  value="#request.get('javax.servlet.forward.context_path')" />
<s:a href="%{contextPath}/clickme" theme="simple">Click here.</s:a>

但是,您不应该这样做.需要网址时,请使用<s:url>标记:

However, you're not supposed to do this. When you need an url, use the <s:url> tag:

<%-- Without specifying an action --%>
<s:url id="myUrl" value="clickme" />
<s:a href="%{myUrl}" theme="simple">Click here.</s:a>

<%-- With an action --%>
<s:url id="myUrl" action="clickme" />
<s:a href="%{myUrl}" theme="simple">Click here.</s:a>

顺便说一句,您不需要表单的action属性的上下文路径:

By the way, you don't need a context path for the action attribute of a form:

<s:form method="post" action="submitme" theme="simple"></s:form>

这篇关于Struts2锚标记不包含contextPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆