OGNL exceptions setting Struts2 checkbox value - struts2

After adding a s:checkbox to my form, I get OGNL errors in the ParamsInterceptor:
WARN [OgnlValueStack] Error setting expression '__checkbox_filter.findRejected' with value '[Ljava.lang.String;#dc926f'
ognl.OgnlException: target is null for setProperty(null, "findRejected", [Ljava.lang.String;#dc926f)
I am aware that the extra hidden field with underscores in its name (__checkbox_filter.findRejected) was correctly added by Struts2.
I don't understand, however, why the ParametersInterceptor is trying to set this property, that was added by Struts2, on my Action (which obviously doesn't contain a '__checkbox_filter' property).
It is normal to see this OGNL error coming from with Struts2 checkboxes? How can I avoid it?

I've just stumble across the very same problem.
You need to place the Checkbox Interceptor BEFORE the Parameters Interceptor in your interceptor stack.
This is the case by default, so I guess that you're using a custom stack...

Most of the time the mistake in such cases is that we forget to write getters and setters for the attributes. So check whether the getters and setters are at their place.

Related

Struts Jquery Grid load issue after upgrading Struts to 2.5.27 from Struts 2.5.22

Hi We have recently upgraded Struts2 from Struts 2.5.22 to 2.5.27 . In our application we are are using Struts Jquery Grid. We are using struts2-jquery-grid-plugin-4.0.3.jar library.
One of our application need is to assign grid id a dynamic value which is bean property.
My code snippet is below:
<sjg:grid id="%{#tabGrid.gridId}" caption="%{#tabGrid.gridCaption}" gridModel="%#tabGrid.gridData}" href="%{gridUrl}"-------->
Before upgrade above piece of code was working. But after the upgarde we are unable to assign grid id bean property.But we can assign grid caption bean property like shown in above code snippet.Only when we are assigning id a bean property my code is breaking.
I have tried to workaround by assigning grid class value of property bean than id. But this change will impact lot of code changes in my application.
Please suggest what could be the reason for it and possible solution.
Since Struts 2.5.26 you no longer can use %{} to force OGNL evaluation in the Struts tags using public attributes which leads to double evaluation of OGNL expression. This fixture is documented in S2-061.
Some of the tag's attributes could perform a double evaluation if a developer applied forced OGNL evaluation by using the %{...} syntax. Using forced OGNL evaluation on untrusted user input can lead to a Remote Code Execution and security degradation.

JSF and EL, I don't understand this code (an array?)

I was reading some code and I found the next EL expression inside a JSF file:
${text['somefield']}
How is it work?.
Since I don't have access to the whole code, I can check what it is. Is it "text" a managed bean?.
Because I could understand the next code:
${someBean.text['somefield']}
(accessing a field array inside a bean but it's not the case.
text can be a managed bean, a CDI dependency (these are the 2 most likely)
text['somefield'] is reading somefield field of text object. text is likely to be a map, but it could be a normal bean too. It's equivalent to text.somefield
In the documentation, you can also find similar exampes:
${customer.address["street"]}
Which is similar to:
${customer.address.street}

Struts 2 ModelDriven for different request parameter name and bean property name

Is it possible to assign the value to the bean property by implementing ModelDriven interface but having different name in request and bean
for eg Ajax request
DemoStruts.Action?param_a=649
the value of param_a parameter must set to the property paramAR in the bean. For doing this is there any xml configuration or annotation to specify this mapping
The normal mechanism is the alias interceptor, although I haven't used it for deep aliasing.
There are some pretty hideous games you can play with this technique. I've never been entirely sure if it's a good idea or not, though; another option is to just map parameters manually in the action itself. This is often easier to understand.

JSF el not resolving

I have the following line in a JSF page:
<h:commandLink action="#{myBean.test}" value="Wizard Step"></h:commandLink>
I expect that when the page is loaded, the Bean corresponding to myBean will be instantiated (and I'll be able to see this in the eclipse debugger).
The rest of the JSF is working correctly, but this bean is not resolving (without any error).
How do I get the errors from the el failing to resolve the bean?
If there are EL errors, you will surely get an exception of javax.el package:
ELException: Represents any of the exception conditions that can arise during expression evaluation.
MethodNotFoundException: Thrown when a method could not be found while evaluating a MethodExpression.
PropertyNotFoundException: Thrown when a property could not be found while evaluating a ValueExpression or MethodExpression.
PropertyNotWritableException: Thrown when a property could not be written to while setting the value on a ValueExpression.
In your case, the managed bean is not constructed on initial request. This can only mean that the managed bean is not referenced elsewhere in the view. The EL in action attribute is only evaluated when the form is submitted. So the bean will only be constructed when the action is invoked. As a test, just put #{myBean} somewhere in the view. You'll see that it get constructed on initial request.
Your real problem is that command button action is simply not invoked, so the EL in its action attribute is simply not evaluated at all. The problem cannot be debugged nor nailed down in the EL side. There are a lot of possible causes for the button action not being invoked. You can find them all here: commandButton/commandLink/ajax action/listener method not invoked or input value not updated. The most common cause among starters is that the button is inside an repeating component like <h:dataTable> whose value is not properly preserved and returns a completely different value during the form submit request, or that the component or one of its components has a rendered attribute which is not properly preserved and defaults to false.

Struts2 form to action fields mapping automatically

I would like to know if it is possible, in Struts2, to map an HTML form's fields to those of an action, automatically, without getters and setters.
It is clear that by getters and setters or the ParameterAware interface and the Map, fields can be set in the action, but I just wanted to know if otherwise there was a way.
First, instead of thinking in terms of "with fields with getters and setters" you are advised to think in terms of "bean properties" here. Struts2 (and most java frameworks) think in that way, they usually don't care (and rightly so) whether those "properties" are real fields or not.
The short answer to your question is: no.
But be aware that Struts2 is very flexible - when I say "no" I mean "using the default interceptors". You could always write your own interceptor instead of the default to do that - bad idea IMO.
The interceptor that does that mapping is (basically) the parameters interceptor. From its documentation:
This interceptor gets all parameters
from ActionContext#getParameters() and
sets them on the value stack by
calling ValueStack#setValue(String, Object)
typically resulting in the values
submitted in a form request being
applied to an action in the value
stack.
And looking into ValueStack.setValue(String,Object) we read:
Attempts to set a property on a
bean in the stack with the given
expression using the default search
order.
So there you have.
ModelDriven was the correct choice :)

Resources