1

On the onchange event on the dropdown list I called a JavaScript method. In this JavaScript method, I called an action class using URL tag and this URL also carries value through a parameter. But when I am doing this, I always get a null value for the setter/getter
method of the action class.

My code is: calling setbusiness() method inside javascript from onchange event of dropdown list and also passing value to it. Alert messages come with id value. But when xmlhttp.open("GET",url,true) called then action class is called with setter method with null value. I don't understand why the value is not coming. Please help me how can I assign dynamic value to the parameter of URL.

<script>  
function setbusiness(sourceid) {  
alert(" sourceid "+sourceid);
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv1").innerHTML=xmlhttp.responseText;
}
}
var url = "<c:url action="feedajax.action">
<c:param name="sourceid">"%{sid}"</c:param></c:url>";                
xmlhttp.open("GET",url,true);
xmlhttp.send();
} 
</script>    

    
3
  • That's really hard to read. Also, any reason you're not using any of a zillion JS libraries that make Ajax stuff waaaay easier?! Please include the rendered HTML, seeing what the source looks like isn't strictly relevant to reality. Commented Jun 26, 2012 at 12:16
  • When reporting problems it's best to be explicit about what doesn't work; I'm surprised you don't get a JavaScript error in the console with this. Commented Jun 26, 2012 at 12:26
  • thnx 4 ur reply. Actually i am very new on this,If u guide some different way to achieve this task then i will try. I am not getting any javaScript error in the console.Also after applying this xmlhttp.open("GET",url,true) action class is invoked with setter method setSourceid(String sourceid) and i try to print out this value on console but no output is coming. Commented Jun 27, 2012 at 4:17

1 Answer 1

1
var url = "<c:url action="feedajax.action"><c:param name="sourceid">"%{sid}"</c:param></c:url>";                

You're using an OGNL escape in the middle of a JSP. JSP knows nothing about OGNL. You're also trying to concatenate two strings without a + so the syntax would fail anyway.

You may use normal JSP EL because of S2's request wrapper:

var url = "<c:url action="feedajax.action"><c:param name="sourceid">${sid}</c:param></c:url>";                

OGNL expressions are only valid inside something that knows about OGNL, like S2 tags.

While it's not the problem, IMO using the same quotes for both your JS string and JSTL parameters makes reading the source very difficult–IMO it's cleaner to distinquish between the two:

var url = '<c:url action="feedajax.action"><c:param name="sourceid">${sid}</c:param></c:url>';

Even better, don't conflate the two operations, and use <c:param>'s value attribute:

<c:url var="feedUrl" action="feedajax.action">
  <c:param name="sourceid" value="${sid}"/>
</c:url>

var url = '${feedUrl}';

Caveat If the source ID is something that can be passed in by a user, it should be JS-escaped.

Sign up to request clarification or add additional context in comments.

1 Comment

Now I uderstood that can not use OGNL expression here. Will u plz help me how can i send id value dynamically to action class.I am stuck in this problem .....plz help with some code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.