how can i pass value from javascript to a java class in struts2?
This is the link similar to what I want.However it does not work for me.
I have tried something like this.
Struts tag for hidden in jsp is
function redirect(id)
{
window.alert("Hello"+id);
document.getElementById('param_ID').value=id;
document.forms["./ListAlgorithmAction"].submit();
}
In my action class I have declared getter and setter for the param_ID.But it returns null.
Kindly help.
Thank you.
You should use like this
<s:hidden id = "param_ID" name="xyz" />
Now you should have a getter and setter for xyz property in your struts action.
You should always have name attribute in tags mapped to attributes in Struts action.
Hope this works.
If the page is not submitting your hidden field, maybe the hidden field is not inside your form element. You can try pass it as a parameter to your url. Try this
function redirect(id)
{
var form = document.forms[0];
var url = './ListAlgorithmAction?param_ID=' + id;
form.action = url;
form.submit();
}
Let me know if this works.
If you want to call an action and return data you should use struts2-json plugin JQuery script for calling action
var abc = $("#abc").val();
$.getJSON('youraction.action', {
variable -name action class : abc,
value to be passed:value,
//and so on
}, function(data) {
//your processing code
}
});
return false;
});
If you want to just call action you may want to use $.ajax in place of $.getJson
If you want to use struts 2-json then you would also need to change your struts.xml i.e
<result name="success" type="json"></result>
It's worth a go
I had a similar issue and resolved it by appending the Action class from JavaScript function and appending the URL parameters as shown below:
<a href="(<%actionClassName%>.action?<%URLParms%>" />
Related
I was working on Co-drops Minimal Form Interface. I couldn't understand this code snippet in stepsForm.js. (Line 50)
stepsForm.prototype.options = {
onSubmit : function() { return true; }
};
I am new to JS, and wouldn't mind an explanation of the entire code in stepsForm if anyone has the time to do so. But, for the time being, an explanation for the above can do wonders for me. I know what a prototype is, but the onSubmit part is going over my head. I read on another question that this is to prevent refresh, but I feel that is wrong.
The library exposes options property that you may/can use to pass your own overriding values.This one in particular, exposes onSubmit.
For any html form an onSubmit is called when the submit action is invoked by another function or by click.
In the library the default onSubmit is returning true, meaning just execute the action. This can be overriden with you custom function like this...
<script>
var FORM_ELEMENT = document.getElementById( 'myForm' )
new stepsForm(FORM_ELEMENT, {
onSubmit :
function (FORM_ELEMENT) {
alert('You are about to submit the form ');
//manipulate your form or do any preprocess work...
return true;
});
</script>
Within the library the _submit (line 196 stepForm.js) is called which inturn calls the onSubmit. This time, instead of the default, it will execute the one we added above.
stepsForm.prototype._submit = function() {
this.options.onSubmit(this.el);
}
Hope that helps.
I have a problem with a a4j:jsFunction (below the snippet) :
<a4j:jsFunction
name="updateVal"
render="panelViewStation"
execute="#all"
onbegin="alert('begin');"
onbeforedomupdate="alert('onbeforedomupdate')"
oncomplete="alert('complete');"
action="#{controller.aMethod}"
actionListener="#{controller.anotherMethod}"
onerror="alert('error');">
<a4j:param name="val" assignTo="#{controller.val}"/>
</a4j:jsFunction>
In my managed bean, I have these methods :
/** action method */
public Object aMethod() {
return "something";
}
/** action listener method */
public void anotherMethod(ActionEvent actionEvent) {
// do something
}
// get/setter for val
This method is called by another js function below :
function processSelectEvent(target) {
alert(target.previousElementSibling.textContent);
updateVal(target.previousElementSibling.textContent);
}
and finally this method is called by a autocomplete element
<rich:autocomplete id="stationId" minChars="3" var="station" inputClass="autoCompleteWidth300" onselectitem="processSelectEvent(event.target);"
fetchValue="#{station.name}"
autocompleteMethod="#{controller.searchStations}"
mode="cachedAjax" required="true" autofill="false" layout="table">
Unfortunately, the feature is not as expected.
When I select an element on the autocomplete list, the function processSelectEvent is called.
The js function "updateVal" is called, but only the "onbegin" javascript.
Other attributes are not called (onbeforedomupdate, oncomplete, onerror) and the managed bean is not called too (action, actionListener).
Any idea to solve my problem ? Thx
Check, please, that a4j:jsFunction is inside a form.
The component sends a request using the standard JSF mechanisms. Note
that this means a JSF form is required.
See also http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=jsFunction&skin=classic
On a button click, I'm calling a function in client side JavaScript.
doIt("TEST");
"TEST" is just the ID of label on my XPage.
In the function, I want to use the variable I passed as an ID. Something like:
function doIt(item){
alert(dojo.query("[id$=':item']").innerHTML);
}
OR
function doIt(item){
val = XSP.getElementById("#{id:item}").innerHTML;
alert(val);
}
I have also tried using this, which gives undefined:
val = dojo.query("[id$=':" + item + "']").innerHTML;
alert(val);
If I hard code the ID name like so, then I get the correct innerHTML of the element with the ID "TEST":
val = XSP.getElementById("#{id:TEST}").innerHTML;
alert(val);
Where is my syntax wrong when trying to write this very simple line of code used the passed variable?
The easiest way is to call your function with the complete id:
doIt("#{id:Test}")
and to use it in your function
function doIt(item){
alert(dojo.byId(item).innerHTML);
}
In the "onclick" client-event in the button, (inside XPage or CC), the client Ids can be computed, so you should put there something like this:
doIt("#{id:Test}"); // << "#{id:Test}" is computed in the server-side and the final client ID is sent to the browser
Then, in your cjs library (cjs libraries are not "evaluated" before sending to the client, so here you cannot use "#{id:Test}" expressions) you should have something like:
function doIt(idElement) {
var domElem = dojo.byId(idElement); // << here you get the element
}
I have the following function:
<script>
function assign()
{
var val = "";
val = document.form1.text1.value;
alert(val);
}
I want to access the variable val's value inside jsp tag so that i can pass it to the googlePlus method as a String. I tried making var val as a global variable, but it doesnt work. How can I access the variable val inside the following code?
<%
String output = "";
if ( Boolean.valueOf(request.getParameter("submitted")) == true ) {
Scraping h = new Scraping();
output = h.googlePlus();
}
%>
You can assign the value of the variable using a assignment operator to some hidden JSP field and then use it in the JS using document.getElementById(). the code would be somewhat like:
<input type="hidden" value="<%=output%>">
Or alternatively if your js is residing inside the JSP only
var s = "<%=output%>"; should work!
Cheers!
You can't access javascript variables via JSP Java Code.
Your JSP & Java codes are compiled at server side.
And your javascript runs in a browser.
So send the 'val' variable to your servlet by form submit, ajax or whatever.
am calling __dopostback function in javascript while closing event of browser but its not working in Chrome.
the same function is working in IE
can any one give me the solution.
<script type="text/javascript" language="javascript">
function doUnload()
{
var btnlogout = document.getElementById("ctl00_lbtn_Logout"); alert(btnlogout);
__doPostBack(btnlogout, '');
}
</script>
When doing a __doPostBack, you need to pass the control's UniqueID via javascript. ClientID will not work.
I think you should be passing the btnlogout id as string (not sure if you have to remove the ctl00 thing since it's a child control) as the function expects text and it is probably resolved during the request on the server..
Take a look at this article:
http://wiki.asp.net/page.aspx/1082/dopostback-function/
Just do:
btnlogout.click();
since you already have a reference to the button and, by the way, don't get the element the way you are doing it; do this instead:
var btnlogout = document.getElementById("<%=btn_Logout.ClientID%>");
dopostback(clientID, args)'s first parameter must be a control's clientid, it is a string , not object (of course string is object ) ..
in your case , i assume that is 'ctl00_lbtn_Logout', pass the right params like :
__doPostBack('<%= downloadUp.ClientID %>', current_index);
if your control is server side control, change 'downloadUp' to your control's id , else you just need pass the id
Please try to getElementById this way
var btnlogout = document.getElementById('<%= lbtn_Logout.ClientID %>');
alert(btnlogout)
and test it again , it will work..