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>    

    
4

1 に答える 1

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

JSP の途中で OGNL エスケープを使用しています。JSP は OGNL について何も知りません。また、2 つの文字列を a なしで連結しようとしている+ため、構文は失敗します。

S2 のリクエスト ラッパーにより、通常の JSP EL を使用できます。

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

OGNL 式は、S2 タグなど、OGNL を認識している内部でのみ有効です。

それは問題ではありませんが、JS 文字列と JSTL パラメータの両方に同じ引用符を使用する IMO は、ソースの読み取りを非常に困難にします。IMO では、2 つを区別する方が明確です。

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

さらに良いことに、2 つの操作を混同しないで、<c:param>value属性を使用してください。

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

var url = '${feedUrl}';

警告ソース ID がユーザーが渡すことができるものである場合は、JS エスケープする必要があります。

于 2012-06-26T12:20:14.763 に答える