Struts2 -ognl Exception - java

I have a JSP page with an html form . i enter the value of the form fields and hit the submit button the control will go the Action class . My question here is for every field in the JSP page do i need to have a corresponding property in Action class with getters and setters .
I dont have any property defined in my Action class and am trying to fetch value's from the HTML field's . . . i get OGNL Exception
WARNING: Error setting expression 'Release Version Template' with value '[Ljava.lang.String;#4eb585'
ognl.ExpressionSyntaxException: Malformed OGNL expression: Release Version Template [ognl.ParseException: Encountered " "Version "" at line 1, column 9.
Is there some workaround for this or should i edit my JSP?

No, you don't have to provide a property for every parameter you're sending with your request. After all it's just a warning that you get and I suspect the reason should be that development mode is enabled in struts.xml.
The warning above, on the other hand, seems to indicate that you're passing the value as the parameter name and thus you get the OGNL warning, so please check that (and maybe post the relevant part of your jsp).
You can also blacklist or whitelist parameters per application or per action but you'd still get warnings if you send those parameters and have development mode enabled.

Related

Is there a way to have ActionError in Struts 2 with key and values to replace the replacement parameters in properties file with an exception message?

I've noticed that Action Error does not work the same way in Struts 2 as it works in Struts 1.
In Struts 1 the ActionError constructor takes a key and values as parameters to replace the replacement parameters in the properties file. I'm trying to achieve the same using the Struts 2 framework (Struts 2.5.26). But I'm only able to add ActionErrors using the addActionError() method that just takes the message string. I would like to know if there's a way of achieving the same functionality as Struts 1.
The application that I'm working on requires to have message keys with replacement parameters in the properties file and have values as messages caught from the exceptions in the Action Class replacing the replacement parameters.
Please have a look at the below example for a better understanding.
For example:
Let us say the global.properties file has this kind of a message key
name.error.param = Error caused due to: {0}
To replace the above replacement parameters in ActionErrors, In Struts 1 it would be done as:
new ActionError("name.error.param", e.getMessage());
Where “name.error.param” is the message key from the properties file. And e is an exception message caught in the Action class.
So the exception message will replace the replacement parameters in “name.error.param”.
How can I achieve this in Struts 2?

Thymeleaf add custom data attribute with message resource value

I have a requirement where I need to insert the value to custom data tag using thymeleaf. The code for doing it using
data-th-attr="${data-custom=#messages.msg('test')}"
as well as
th:attr="data-custom=${#messages.msg('test')}"
I am unable to get the value in both the cases.
ultimately the parsing should be like data-custom="test"
here test is key for the value test in a properties file
By using the
data-th-attr="data-custom=#{test}"
or By using
th:attr="data-custom=#{test}"
helped me out, here test is the key for the value in message resource the issue was with the intellij IDEA IDE, it was having a bug that was showing me an unnecessary error.
Use th:attr="data-custom=#{key.for.message}" , this should work.
then after parsing the Expression,
data-custom="value.for.message"

Servlet not retrieve parameters from AJAX call

I used AJAX to call an action and pass parameters, the AJAX call occurs from xsl page and its as follows:
xmlHttp.open("GET","examcont?action=AJAX_SectionsBySessionId&sessionId="+sessionId,true);
I decided to put the amp; after & as xsl raises this error when I removed it:
The reference to entity "sessionId" must end with the ';' delimiter
the problem is that the action is unable to read the parameter sessionId however I tried the same action URL but without the amp; and the action reads the parameter successfully
The problem seems to be that the & represents & in the style sheet but gets expanded/escaped to & again during output (because it is HTML/XML). You may try to use the following in XSL to avoid escaping:
xmlHttp.open("GET","examcont?action=AJAX_SectionsBySessionId<xsl:text disable-output-escaping="yes">&</xsl:text>sessionId="+sessionId,true);
However, note that - if you happen to let your XSL run in the browser - this does not work (although it is correct XSL and it should) on Firefox according to https://bugzilla.mozilla.org/show_bug.cgi?id=98168.
As portable alternative, you can use the following which avoids mentioning & by inserting it at runtime with what you might call "Javascript-escaping":
xmlHttp.open("GET","examcont?action=AJAX_SectionsBySessionId"+String.fromCharCode(38)+"sessionId="+sessionId,true);
Also have a look at similar question with deeper discussion and other options using a html entity in xslt (e.g. )

How can directly get the value of a specified cookie name from the header?

In my jsp page i have put
${header.cookie}
and I got this :
JSESSIONID=1bxvxsg61zphc; JSESSIONID=385a85a4ad8a3041ee047d586447; treeForm:tree-hi=treeForm:tree:applications; clientLanguage=fr
My question is, how can directly get the value of a specified cookie name without using loop, for example: if i want to get the value of the clientLanguage.
Cookies are available by ${cookie.name}. So:
${cookie.clientLanguage}
See also:
Unified Expression Language - Implicit objects
${pageContext.response.locale}

struts validation problem in IE

I am using Struts 2.1.8 and facing validation problem in IE. I am getting the following error
An exception occurred: Error. Error message: Invalid argument.
I tried out to figure out the cause and found the following. My generated javascript code is:
field = form.elements['district.name'];
var error = "Enter only alphabets for district";
if (continueValidation && field.value != null && !field.value.match("^[a-zA-Z ]*$")) {
addError(field, error);
errors = true;
}
I tried to mock up by putting the same code in a function and calling it in onclick event. The method addError() throws the exception and the reason is field variable. If I change it to field[0], it works fine. How to fix this error?
Check the generated HTML source. Open the page in webbrowser, rightclick and choose View Source. Is the input field's name really district.name? Isn't it prefixed/suffixed with some other autogenerated key (possibly the ID/name of the <form>) like as many other MVC frameworks do? If so, you'll need to change the JavaScript code accordingly that it uses the right element name as it appears in the HTML DOM tree. You know, JavaScript runs at the client machine and only sees the generated HTML DOM tree, not the "original" server-side code which is responsible for generating the HTML.

Categories