struts validation problem in IE - java

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.

Related

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"

Struts2 -ognl Exception

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.

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 do I link to a servlet within javascript code

I got an XLS pic inside of an HTML link, and i need to verify some information first before calling to the servlet, that's why i'm not including the servlet inside of the href="". So i've created a javascript function that verifies the input information in order to be used by the servlet.
(The Servlet returns a XLS in order to be saved by the user).
Tried this:
document.location.href = 'saveExcelServlet.do?' + <<GET method attributes>>;
But it didn't work.
It says:
Problem accessing /wscall-metrics-web/saveExcelServlet.do. Reason:
null
Caused by:
java.lang.NumberFormatException: null
If i write it works...
Can anyone help me?
Thanks.
M.
There's a good chance the URL isn't quite built the way you expect. A great poorman's technique for debugging this kind of thing is to assign a variable and pop it up in an alert:
var newLoc = 'saveExcelServlet.do?' + <<GET method attributes>>;
alert(newLoc);
You can see exactly what URL is getting fetched.

Syntax error results in blank page

I am new to wicket and trying to get some things working.
One thing that annoys me a lot is that I get a blank (0 chars of text) page whenever there is a syntax error on a page.
Striped down example:
Test.html
header stuff: doctype ... html ... head ... body ...
<span wicket:id="msgTest" id="message">MSG</span>
footer stuff: /body ... /html
Test.java
public class Test extends WebPage {
public Test() {
add(new Label("msgTest", "Hello, World!"));
}
}
This will output the page as expected.
Now, lets introduce an error:
header stuff: doctype ... html ... head ... body ...
<span wicket:id="msgTest2" id="message">MSG</span>
footer stuff: /body ... /html
I changed the label-id to something different then what the source-file expects.
If I run this code I get the already mentioned blank page.
However, for every request to a page with such a syntax error I get an error report in the log-file of around 1000+ lines. This error-report is basically just wicket-generated html of a page which describes the error.
This makes me wonder why wicket isn't displaying the error-stuff instead of the blank page. I'm not very experienced with wicket but to me it somehow looks like wicket is having trouble rendering its own error-page code.
It would be nice to know how one goes about finding syntax-errors with wicket.
Reading through a 1000+ line error-report for a small error like a misplaced character seems a bit tedious.
Thanks in advance for guiding me into the right direction :)
PS:
wicket-version: 1.4.9
stage: development
I can not confirm that behavior. I went to http://wicket.apache.org/quickstart.html and created a quickstart. Changed the wicket id from 'message' to 'message1' and got a nice descriptive page in jetty:
WicketMessage: Unable to find component with id 'message' in [Page class = com.mycompany.HomePage, id = 0, version = 0]. This means that you declared wicket:id=message in your markup, but that you either did not add the component to your page at all, or that the hierarchy does not match.
How did you create your project?
What I like to do is write unit tests with WicketTester to at least verify that things render, and usually also write assertions to check the components. Something along the lines of
#Test
public void testMessageLabel(
WicketTester tester = new WicketTester();
tester.startPage(Test.class);
tester.assertLastRenderedPage(Test.class);
tester.assertComponent("msgTest", Label.class);
tester.assertLabel("msgTest", "Hello, World!");
)
Then if as in your example the code contains "msgTest" and the html contains "msgTest2" you at least get a test failure instead of seeing it as part of a failing app after deploy.
It's certainly not a complete solution, since this error will make any rendering test for the page fail and the particular failure will just give a long error message in the test result, but at least you don't have to search log files.

Categories