Can we call html ids from Wicket Java class - java

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.

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

Can tiles have action classes?

In Stuts2 I am using Tiles plugin to create a layout for the website (menu, footer, header etc.) that is consistent on every page.
Now each tile is just a static HTML content.
Is it possible to make a Tile more dynamic by eg. calling a Footer action class every time the footer is to be rendered? For example: to fetch footer content from database.
If I was to do that inside every page's action class in my application this would make for a very unusable code...
So maybe it is possible from Tile perspective?
There is only one way to do what you ask with a tiles version less than 2.2.2 and that is with a "preparer".
This however is not an integration with struts2 but would mean the preparer it self will access the service layer to get the required content for the view and all that content would need to be set though tiles attributes.
With tiles versions 2.2.2 and higher:
You can use OGNL expressions within tiles attributes, this can allow access to some struts2 interaction as well as static method access. With static method access you can call a method to return a string how ever you want. Creating such a string would be on par with writing a scriptlet.
To upgrade you need to either manually override some jars to get tiles 2.2.2, or to get version three you will need to implement your own result type: How to integrate Struts 2 with Tiles 3.
I don't actually recommend either of the above methods at this time, tiles 3 is recommended but not as an excuse to do something as bad as writing a scriptlet. It would probably be better to use the s:action tag in a tile, as mentioned by David or use an Ajax method as mentioned by Jaiwo99. The reason being that both these methods keep with struts2 while the ones I presented would be unusual and be harder to maintain. Personally I would lean towards the ajax methods.
Struts2 along with the struts2-json-plugin makes creating json services very simple. Tiles is a nice system for reducing boiler plate. If ajax is used heavily the two really can compliment each other. You can make a lot of reusable ajax components, just be sure to not hard code the urls of actions. Always use the s:url tag and assign that to JS variables.
Try following code:
$('#footer').load('your/action/with/namespace');
i'm assuming your footer is with id footer, everytime you open a page, your footer action class will be called and the data can be fetched dynamically.

How to get the DOM of a JSF 2.0 component's generated HTML from the backing bean?

I have a JSF-page containing only a PrimeFaces Calendar component, backed by a view scoped bean.
<p:calendar value="#{calendarBean.today}" mode="inline" showOtherMonths="true" pages="1" binding="#{calendarBean.model}" />
The generated HTML for this component contains a table that I'd like to adjust. More specifically, I want to put icons dynamically into this calendar by adding some HTML-code under the td-tags of the targeted days.
From the CalendarBean I can fetch from some service a list of objects representing days "with icons". I need this list to decide which days to alter in the table. I tried to get the HTML DOM in my backing bean, but I don't understand how. The UIComponent tree (that I get via the binding property) only contains the JSF-components, not the generated HTML. I start to doubt if this is even the way to approach the problem.
My google-skills seem to fall short here. Can anybody help me?
The concrete functional requirement is not entirely clear, so it's hard to propose the right way for this, but there are in general varous ways to alter the look'n'feel of the JSF/PrimeFaces-generated HTML.
Use custom CSS. Icons could simply be set as CSS background images.
Use custom JS. JS has instant access to the entire HTML DOM tree.
Use custom JSF renderer. Extend the PrimeFaces CalendarRenderer class and override the encodeXxx() method(s) accordingly.
The backing bean is supposed to be the controller between the view (the XHTML file) and the model (the javabean entity class holding the data which is to be presented to the enduser or to capture the submitted data from enduser). It isn't supposed to manipulate the HTML.

JSTL/JSP dynamic form page state

Working on a large Web app, I have houndresds of JSPs.
Each JSP includes (ONLY) a set of internal tags, for instance:
<AAA:INPUT value="bbb" state="<%=getPageState()"/>
This tag is rendered into an HTML input field, with a readonly/enabled state, based on the return value from getPageState().
This basically allows me to set the complete page as enabled/disabled from a single entry point.
I don't like this (mainly because it drives me away from writing the HTML I want and I need to maintain attributes for each HTML attribute I want), I know I can something similar on client side with JavaScript.
Are there other approachs to control the state of a complete JSP form in a single point on the web-server side?
The way I would go about this would be to convert the getPageState() method into a tag, and then use this tag within your custom tags. That way, you don't need to explicitly pass in the page-state on every invocation of your custom tag.
Of course, this means that you would have to modify all your custom tags to use this new tag, but I think that's better than having your code littered with explicit calls to get the page state. Not to mention, an opening for inconsistencies if a developer forgot to check the page state.
Also, now the decision as to how the element needs to be rendered rests in the custom tag itself (where it belongs).
The only problem I see is that now you have to make multiple (redundant) calls per element to get the page state, which is not that efficient. To get around this problem, you can have your custom tag (that gets the page state) set a page attribute, and have your custom tags inspect this page attribute to decide if the form element should be disabled or not (another way would be to create a variable with the scope AT_END).

Extending struts 2 "property" data tag

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/

Categories