can not read some string from struts.properties - java

I have define variable error in struts.properties as follows:
error=this is an error
Now I can call this error as follows:
ErrorMsg = "<s:property value='getText(\"error\")'/>";
and it works, the result is: ErrorMsg=this is an error
How to get the text of variable instead of string?
I tried the following:
var m="error";
error1 = "<s:property value='getText(m)'/>";
error2 = "<s:property value='getText(\"m\")'/>";
I use firebug debugger and error1 and error2 are displyed as follows:
error1=""
error2=""
Any Idea?
thank you in advance

You appear to be mixing server side and client side code.
The s:property tags will be evaluated first on the server side, long before any value of m is valid, as that is client side JavaScript code.
If you post what you're trying to achieve then I or someone else may be able to help further.
HTH

Related

console message captured by onConsoleMessage is incomplete

I have WebView into which I'm loading a webpage, I have set the custom WebChromeClient to capture console logs but I've found it is incomplete comparing to the console in browser. The message returned by cm.message() doesnt contain all values passed to the console methods.
console.log("Test");//Test
will work but following will only display first param
console.log("test", 1);//test - expected test 1
is there any way to workaround it?
An interesting bug!
You can wrap console.log in your own function that concatenates all the arguments into a single string. The JavaScript code for doing this is below:
(function() {
var oldLog = console.log;
console.log = function() {
oldLog.call(console, Array.prototype.slice.call(arguments).join(" "));
}
})();
You can inject it into your page with WebView.loadUrl('javascript:...'), or WebView.evaluateJavascript.

How can I execute JS on server-side?

I'm on a Java EE project and I need to use the value of a JS var on my Java code but my java code is execute before my JS (logic)
But I need the value of a JS var to execute my java and I think it will work if I execute my js on server-side but I don't know how to do this ...
My code is something like this :
JS :
function(){
var url = "an url of 20.000 char that I can't pass in GET and who is automatically generated by google chart API"
}
Java :
<%
String urlChart = "value of my "url" var in js";
session.setAttribute("urlChart", urlChart);
%>
But I don't know how tu put the value of my JS var un my java code. Can you help me ?
Somebody said me that I have to use AJAX but I don't know how to use it.
That is not possible in my understanding. you must switch to some other alternate solutions. you can use CGI or PHP or AJAX.
http://www.w3schools.com/ajax/
You can check this similiar question: how to send a string to a servlet from javascript using xmlhttprequest
If this won't help, then you need to provide more details because i'm not sure if i understand your problem.

comparing strings in javascript (string origin is from xmlhttp.responseText)

My servlet responds a string which contains "false" or "true"
=> Servlet code: out.println(validusername); validusername is a string from my servlet that goes to the var validusername in my javascript.
so in my front-end, I have this javascript code:
The problem is that when my servlet returns false, the javascript always jumps into the "else".. I am wondering why.. searching this for hours.
I know that my servlet responds with a "false" because the alert(validusername) occurs a popup window with the text "false" in it... Do you guys have any idea why validusername =="false" always jumps in my else tag?
I think there is something wrong with the comparision, but I can't find it...
Thank you so much for helping
var validusername = xmlhttp.responseText;
if (validusername == "false") {
alert("username already exists");
}
else {
alert("username free");
alert(validusername); //this popup contains the value "false"
Stop using alert(...) as of today, and start using "console.log(...)" instead. It'll print the data to the web console, and you get much better insight in what's going on. It also doesn't block your page. Without information on what's going on, the first advice is to force-validate all your data. Even if you own the servlet.
force if (validusername == "false") to resolve correctly by instead using if (validusername.trim() === "false") -- now if it fails, at least the string can't "look" like false without actuallying being "false".
Probably you need to trim the String and compare.
validusername = validusername.replace(/^\s+|\s+$/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.

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