) 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!
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
有趣的。我相信你走在正确的道路上。
只是我会尝试扩展/修改属性标记,以便在 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/