延伸支柱2“特性”数据标签

发布于 2024-08-28 18:03:18 字数 1210 浏览 4 评论 0原文

我们目前正在开发一个使用Struts2 的项目。我们有一个模块,可以通过“property”Struts 2 数据标记(即)在一组 bean 的只读字段上显示大量数据。 .jsp 文件。在某些情况下,大多数字段可能为空,因此屏幕上会显示空白。

我们的客户现在要求我们在属性为空时显示默认字符串(即“N/A”),以便显示它来代替当前显示的空格。

我们正在寻找一种以干净且可维护的方式实现这一目标的方法。 “property”标签带有一个“default”属性,当访问的属性为空时,可以在该属性上定义默认值。然而,我们的大多数属性都是空字符串,因此它在我们的例子中不起作用。

我们正在考虑的另一个解决方案是为所有 bean 定义一个基类,并定义一个 util 方法,该方法将验证字符串是否为 null 或空,然后返回默认值。然后我们会从每个 bean getter 调用这个方法。是的,这会很烦人,而且有点丑陋:),因此我们会坚持这一点,以防有更好的解决方案。

现在,我们想到了一个我们认为最好的解决方案,但在如何实施它方面还没有运气。我们计划以某种方式扩展“property”标签,定义一个新的“default”属性,以便除了处理空属性之外,它还可以处理空字符串(“”、“”等)。因此,我们只需要用新的自定义标签替换原来的 s:property 标签,就可以实现所需的结果,而无需接触 java 代码。

您知道如何做到这一点吗?另外,关于如何默认大量属性 bean 的值的任何其他聪明的解决方案(也许某种设计模式?)也是受欢迎的!

(或者也许,甚至可能有一些标签已经在 Struts2 中执行此操作??)

提前致谢。


如果您不想阅读以上所有内容,请使用较短的版本! :)

目前Struts2提供了一个属性标签(),它用于显示值的内容,例如Action类上的String变量。该标签包含一个名为“default”的属性,您可以在其中定义要在变量设置为 NULL 时显示的默认值,例如,您可以将其设置为显示这些值的“N/D”。

我们现在需要做同样的事情,但它也适用于空字符串(“”、“”等),而不仅仅是空值。我们计划扩展此标签,以便我们拥有自己的标签(可能类似于)并完成此行为。您能指导如何实现这一目标吗?

非常感谢您的帮助。谢谢!

We are currently developing a project with Struts2. We have a module on which we display a large amount of data on read-only fields from a group of beans via the "property" Struts 2 data tag (i.e. <s:property value="aBeanProperty" />) on a jsp file. In some cases most of the fields might come empty, so a blank is being displayed on screen.

Our customer is now requesting us to display default string (i.e. "N/A") whenever a property comes empty, so that it is displayed in place of the blank spaces currently shown.

We are looking for a way to achieve this in a clean and maintainable way. The 'property' tag comes with a 'default' attribute on which one can define a default value in cases when the accessed property comes as null. However, most of our properties are empty strings, therefore it does not work in our case.

Another solution we are thinking of is to define a base class for all of our beans, and define a util method which will validate if a string is null or empty and then return the default value. Then we would call this method from each bean getter. And yes this would be tiresome and kind of ugly :), therefore we are holding out on this one in case of a better solution.

Now, we have in mind a solution which we think would be the best but have not had luck on how implement it. We are planning on extending the 'property' tag some way, defining a new 'default' attribute so that besides working on null properties, it also do so on empty strings ("", " ", etc). Therefore we would only need to replace the original s:property tag with our new custom tag, and the desired result would be achieved without touching java code.

Do you have an idea on how to do this? Also, any other clever solution (maybe some sort of design pattern?) on how to default the values of a large amount of property beans are welcome too!

(Or maybe, even there might be some tag that does this already in Struts2??)

Thanks in advance.


Shorter version in case you don't want to read all of the above! :)

Currently Struts2 provides a property tag (<s:property value="someValue" />), it is used to display the content of a value, e.g. a String variable on an Action class. This tag contains an attribute called "default" where you can define a default value to be displayed IF the variable is set to NULL, e.g. you can set it to display "N/D" for these values.

We now need to do the same, but that it also works on empty Strings ("", " ", etc), not only on nulls. We plan on extending this tag so that we have our own (maybe something like <s:propertyEmpty value="someValue" />) and accomplish this behavior. Can you guide how to accomplish this?

Your help is greatly appreciated. Thanks!

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

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

发布评论

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

评论(1

依 靠 2024-09-04 18:03:18

有趣的。我相信你走在正确的道路上。

只是我会尝试扩展/修改属性标记,以便在 jsp 代码中显式设置新行为。我的意思是,我不希望 Web 应用程序中的每个自动获取新行为,因为也许有一些 bean 属性我想要原始的 struts2 行为。
我更喜欢有一个新的标签 nane (例如: ... 或更短的东西)或新属性(例如 )

也许这有帮助: http://bodez.wordpress.com/2009/03/13/customising-struts2-jsp-tags/

Interesting. I believe you are on the right track.

Only that I would try to extend/modify the property tag so that the new behaviour is explicitly set in the jsp code. I mean, I would not like that each in the web app automatically gets the new behaviour, because perhaps there are some bean properties for which I want the original struts2 behaviour.
I would prefer to have a new tag nane (eg: ... or something shorter) or a new attribute (eg )

Perhaps this helps: http://bodez.wordpress.com/2009/03/13/customising-struts2-jsp-tags/

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