Extending struts 2 "property" data tag - java

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!

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/

Related

wicket form changes are not visible when setDefaultFormProcessing(true)

I have a form in wicket which has two buttons. I would like one to have validation over the fields (if they are left null or not) which I already did. Now I would like the second button NOT to have this validation. I have seen few examples where people use the method setDefaultFormProcessing() which is a method of class Button in wicket.
However when I use this method my form seems to also ignore changes done in the fields of the form.
Any idea how I actually can achive bypassing the validation but still be able to see changes in my form??
Thanks!!
This is the defined behavoir. If data is not valid it is not possible update model anyway. Consider an example when you have a date field and you type there '99/9/YYYYY' that is not a valid date, thus wicket has no chance just to bypass validation and update model. The only chance is to keep input data as String and give you an option, how to convert or update model by your own implementation.
If you have a reference to your form components, you could invoke updateModel().
See http://apache-wicket.1842946.n4.nabble.com/Turn-off-form-validation-td1877661.html
And check API doc for FormComponent that is a base class of all fields and other form components. https://ci.apache.org/projects/wicket/apidocs/7.x/org/apache/wicket/markup/html/form/FormComponent.html

How are variables scoped when calling the same Play template again?

I'm writing a tag that keeps track of how many time it was called, in order to generate unique ids for its elements:
%{
try {
coolTagId++;
} catch (Exception) {
coolTagId = 0;
}
}%
<div id='cool-tag-${coolTagId}'></div>
...
$('#cool-tag-${coolTagId}').click(function(){alert("Cool Tag ${coolTagId} clicked")});
When I include this tag in a page multiple times, to my surprise, I see that coolTagId is 0 every time it's evaluated. Why is this happening?
(I'll be using some sort of UID in the meantime, I just want to understand why the above snippet doesn't work)
I don't know why that is but it doesn't surprise me. Play is a stateless framework, so it seems natural that it doesn't share state between two instances of the same tags.
It's interesting, even setting the value beforehand using #{set coolTagId:0 /} does not work.
What does work however is setting the value in your tag. Put this at the end of your tag: #{set coolTagId:coolTagId /}. This way you manually push the value to the base template.
It is a simple matter of scope.
Imagine writing this in pure Java, a tag is effectively calling a method. Everything defined in your tag (i.e. method) is locally scoped to that tag, so it would not exist once the tag has been executed, as it would have left the scope.
The reason for this, is to make sure that anything outside of your tag is not broken or modified by the execution of your tag. Everything is self contained, except for the parameters that you pass in.

Where to keep the list of values for the select tag?

I have a page with a select (drop-down). It will have values like:
"SUN" - "Sunday"
"MON" - "Monday"
"TUE" - "Tuesday"
etc.
I can:
store the list of values inside of the page (JSP-page) with
'option' tag
keep it in a Map in MyForm.class and pass it to the page from
Controller using 'options' tag.
The current example is about days of the week(there are only 7 of them), but is also applicable to days of the month (the amount of data becomes bigger), etc.
What will be the better way?
P.S. I'm using Java, JSP and Spring Framework
This type of data should be exposed through a controller or encapsulated in a custom tag (or both).
You don't want to hard-code strings into a JSP (in general) because of I18N issues, although you could do the I18N for that in the JSP, it's just more verbose.
I would suggest keeping in your class as that would give you the ability to change it whenever you want without changing the UI.
If you keep it on the UI, any change would require you to change the UI and change the business classes too.

Can we call html ids from Wicket Java class

Is there a way to call ids that are created in html page without specifically giving wicket:id in the component. If so, how can I do that?
What I am trying to accomplish is to get access of the component to assign a default value from CMS. I could assign the default value using JS, however I need that value to be populated to Java so that I could use that in my class to update my business logic depending on the defaulted value.
As I understand it, Wicket is component oriented which means that single component (piece of HTML code) can't see all the HTML. And my guess is that at some late stage, you want to look at the whole page HTML and do something with a DOM node with a specific ID.
I'm not sure what the correct solution should be. One way is to put the value into the user's session, so it's available for each component and your business logic.

Seam: programatically set s:decorate to invalid state

If you've used JBoss SEAM you've probably used the s:decorate tag. Quite a handy tag.
Anyway I need to a way to set its invalid state through java.
I've bound the xml tags to UIDecorate instance but I'll be damned if I can figure out how to set the 'invalid' state so that the decorator will apply the appropriate error CSS classes when the page is rendered.
Is it possible to do this? Am I going about it the wrong way? Note that in this case just writing a custom validator isn't an option (usually that would be the right way of doing it obviously).
Thanks!
Turns out the right way of doing this isn't by binding but simply by setting a message on the control using either StatusMessages.instance() or the injected FacesMessages.

Categories