如何添加或更改 struts url 标签的参数?

发布于 2024-08-21 01:27:52 字数 1489 浏览 1 评论 0原文

在struts中,我希望有一个基本URL(

<s:url action="getSomeData" id="baseDataUrl">
    <s:param name="someID" value="%{currentID}"/>
</s:url>

然后我可以执行 ,它会输出类似 /getSomeData.do?someID=5 的内容。 (在这个例子中,我保持参数简单,但你可以想象有更多的默认参数。例如,我正在开发的应用程序有 3 个参数,每个 URL 都相同,还有两个参数会改变。

)希望能够在定义基本 URL 后添加或更改参数。我正在设想两种方法:

<s:url base="baseDataUrl" id="breadUrl">
    <s:param name="bread" value="%{'toast'}"/>
</s:url>

然后 将导致 /getSomeData.do?someID=5&bread=toast。另一种方法是使用占位符定义基本 URL:

<s:url action="getSomeData" id="baseDataUrl">
    <s:param name="someID" value="%{currentID}"/>
    <s:param name="bread" />
    <s:param name="jelly" />
</s:url>

然后在我请求属性时填写它们:

<s:param url="baseDataUrl" name="bread" value="%{'toast'}"/>
<s:param url="baseDataUrl" name="jelly" value="%{selectedJam}"/>

因此 将导致 /getSomeData。做?someID=5&面包=吐司&果冻=7。

s:url 的文档 (http://struts.apache.org/2.0 .14/docs/url.html)说“允许动态属性:false”,所以我想我正在冒险进入自定义标签领域。这感觉就像一个常见的用例(使用一些参数定义基本 URL,然后添加或更改其中一些参数),我不想重新发明轮子。搜索没有任何结果,但也许我没有在寻找正确的东西。我愿意接受各种各样的建议!

In struts, I'd like to have a base URL (an <s:url...) set up with a some parameters (<s:param...), and then either add parameters to that URL or change the values of some parameters. For example:

<s:url action="getSomeData" id="baseDataUrl">
    <s:param name="someID" value="%{currentID}"/>
</s:url>

I can then do <s:property value="baseDataUrl" /> and it'll spit out something like /getSomeData.do?someID=5. (I'm keeping the params simple for this example, but you can imagine having many more default params. For instance, the app I'm working on has 3 params that are the same for every URL, and two that change.)

I'd like to be able to add or change parameters after defining that base URL. I'm dreaming up two approaches:

<s:url base="baseDataUrl" id="breadUrl">
    <s:param name="bread" value="%{'toast'}"/>
</s:url>

And then <s:property value="breadUrl" /> would result in /getSomeData.do?someID=5&bread=toast. Another approach is to define the base URL with placeholders:

<s:url action="getSomeData" id="baseDataUrl">
    <s:param name="someID" value="%{currentID}"/>
    <s:param name="bread" />
    <s:param name="jelly" />
</s:url>

And then fill them in when I ask for the property:

<s:param url="baseDataUrl" name="bread" value="%{'toast'}"/>
<s:param url="baseDataUrl" name="jelly" value="%{selectedJam}"/>

So <s:property value="baseDataUrl" /> would result in /getSomeData.do?someID=5&bread=toast&jelly=7.

The docs for s:url (http://struts.apache.org/2.0.14/docs/url.html) say "Dynamic Attributes Allowed: false" so I think I'm venturing into custom-tag territory. This feels like such a common use case (define a base URL with some params, and then add or change some of them) that I don't want to reinvent the wheel. Searching has yielded nothing, but maybe I'm not looking for the right thing. I'm open to a wide variety of suggestions!

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

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

发布评论

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

评论(1

扛起拖把扫天下 2024-08-28 01:27:52

以下是我能想到的几个选项

  1. 使用名为“delegatorAction”的操作并仅写入所需的额外参数。您可以编写一个操作/处理程序,然后“推断”其他参数(在本例中为“someID”)。这可能很复杂,每次 url 处理逻辑发生更改时,您可能都必须更改此代码
  2. 您可以编写自己的 url 标记,并按照您建议的方式拥有一个类似“base”的属性,该属性仅评估基数属性并向其添加任何参数(您可以为此对 Url 标签进行子类化)
  3. 最简单的方法可能是首先将 3 个参数声明为 url,然后使用标准 s:property 标签添加额外的参数(哎呀,我无法访问输出 xml 代码,即使格式正确也不知道为什么,一定是因为 chrome)

在这种方法中,您只是在末尾附加字符串。

Here are few options that I can think of

  1. Use an action called "delegatorAction" and write only the extra params that is needed. You could write an Action/Handler which then "infers" the other params (in this case "someID"). This could be complicated and you may have to alter this code everytime there is a change to your url handling logic
  2. You could write your own url tag and say and have a property like "base" the way you have suggested, which just evaluates the base attribute and adds any param to it (You could subclass the Url Tag for that)
  3. The most simplistic approach could be to first declare the 3 params as a url and then add the extra parameters using standard s:property tags (Heck I cant get to output xml code even with proper formatting dont know why, must be because of chrome)

In this approach you are merely appending strings at the end.

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