Can we change the value of the inputHidden field "javax.faces.viewState" before the page render.
For the field
The value should be changed to a different length value. Can this be done by using a custom viewHandler?
Can we achieve this by extending the class to ResponseWriter.
The field is rendered by the ResponseStateManager that you obtain from the current render kit.
If you look up its API you'll see that you can't just override the field's value. You have to replace the entire thing! Since state saving is quite complex (think about both server and client algos), I would think twice about attempting this.
An alternative is using a Servlet filter to capture the entire response. The hidden field's name is standardized and you could search and replace on it. For a postback you could use the same filter to restore the param.
Related
How can we pass a list from JSP to the action in Struts 2?
The list is a list of strings set from the same action when the JSP page gets loaded (there is a hidden field in the JSP which is being set).
All that I need is when again the form is submitted and the control goes to the action, I need that list again.
Make an array of strings and submit it using json or make a # seperated string of all strings and associate it with some hidden variable and submit it to action class. then in action class you needs to parse it back to original form.
you can use the hidden tag name attribute to refer to the list name that is being used in the class. Now when the form is submitted it will automatically map to the list in your action.
You need to have list as your instance variable in the action class
If you are setting a single hidden field with the contents of a list then you'll need to parse it back into a list again. Whether or not this is a good idea depends on the contents of the list, and how good you are at parsing.
Another option is to use Struts 2's default list-building mechanism and use multiple hidden fields and OGNL's array notation, e.g., name="foo[0]", name="foo[1]", and so on.
I'd start, however, by examining the need to reconstruct the list from the JSP like this.
If you're just serializing/deserializing the same list, why bother? Either keep it in session, or reconstruct it on the Java side. If it's backed by a DB then your caching mechanism should reduce any overhead.
I'm trying to add a button to an existing form (BankAccountTable). I want to add a button to run an outside process with the value of one of the form fields as a parameter.
The value is being read using this code:
str value = element.design().controlName("FieldName").valueStr();
However when I click the button Dynamics displays what fields must be filled in. This doesn't happen if the click method doesn't reference the form fields (i.e. info("click");).
How can I:
Read the value of a field without triggering form validation?
and/or
Have a button (or command buttton) that doesn't trigger form validation?
The second question, how to avoid validation, is easy: set the button attribute SaveRecord to No.
You should rarely need to access the control value directly. A better option is usually to access the bound field directly: table.FieldName.
If the control is not bound to a field, then change the AutoDeclation attribute to Yes and access the control directly: fieldName.text(). The methods text, realValue or selection is a better choice than valueStr.
I am looking for a way to dynamically specify the format of my model DateTime fields in the view. I need these to be editable (meaning I want changes to them to be properly bound to model on postback). I am not worried about validation as I will be using a jquery datepicker control on the resulting textboxes.
So far all I have found is a way of setting the format in data annotations in the model - which is way too static and restricting (unless there is also a way to modify data annotations at runtime?). Have also found guides for making templates for DateTime, but that is also static. Found some ways to format the fields in the view for "display only" requirements that won't post back changes.
So far the only thing I have come up is having a separate string field for each DateTime field in all my models, do conversion in controllers manually before displaying only the string fields, and then convert them back. Before I embark on his messy approach, does anyone have any suggestions for an easier/cleaner way?
You could technically write your own Annotation that will give you the dynamic format depending on your wants.
These are evaluated at runtime so depending on what you want to return you can do it then.
I am currently using attributes to return very specific data for specific uses.
I have queried DB and set that object in Action class and set that back to jsp to display results. Now I want the same object into another or same action class. How do I do that.
I haev also faced same issue and implemeted in the following manner. find if it is useful.
You have use conversion technique to speficy what type of Object are you going to store in the list. Then i have set it in hidden feild in jsp. then i am able to retrive it in action class.
http://struts.apache.org/2.0.14/docs/type-conversion.html
I have an action that populates the result from the DB. Right now, I see a way of doing it and that is to make the Action ServletRequestAware, set the populated list as a request attribute and show it in jsp.
Since a lot of improvements have been introduced into struts2, is there any other way of doing that? The first thing that comes to my mind is to use displayTag and change the return type of Action to a List, but that does not seem to work.
Thanks for any replies in advance.
You question is unclear, you should read some book about Struts2 to get the general idea.
No need to make the Action ServletRequestAware. The mapping from http parameters to actions fields is automatically done via the Param interceptor (already set in the default configuration). And one of the points of Struts2 is decoupling the action from the http protocol, you should not (typically) do anything related to http in your action.
Tipically, in your action execute() method (or whatever) you'll get the data to display from the DB and set it as one property of your action, so that is accesable from some getter.
Then, in your view page (JSP or whatever) you'll display it. You can use displayTag, but first you'll prefer to display it "manually", to understand what's involved. See for example here http://www.roseindia.net/struts/struts2/struts2controltags/iterator-tag.shtml
For manually displaying a table, also see this example http://www.vaannila.com/struts-2/struts-2-example/struts-2-crud-example-1.html , search for the <table class="userTable> tag.