Scriptlet in js

2019-03-06 12:20发布

问题:

i have a jsp page... i am adding some content to page dynamically depending upon request parameters (an array will be returned by request) based on this i have to create a drop down. i want to do this on change of another drop down.. so can be done using javascript only but i am unable to use scriptlet in js, is this really possible??

EDIT : i wanna perform some actions on the values retrieved from scriptlet as well

it will be of this sort

function changeMethod(){
    var templateselected = document.getElementById("templateDropDown");
    var versionDropDown = document.getElementById("versionDropDown");
    if ( templateselected.options.selectedIndex != -1)
    {
        var selected=templateselected[templateselected.options.selectedIndex].value;
        removeChildNodes(versionDropDown);
        <% 
        RetrieveTempSecVersions[] lsListOfFiles = (RetrieveTempSecVersions []) request.getAttribute("templateNames") ;
        for (int i=0 ; i < lsListOfFiles[1].getVersionNumber().length ; i++ ) {
            System.out.println("helllooooo");%>
        versionDropDown.innerHTML+='<OPTION VALUE="'+<%=lsListOfFiles[1].getVersionNumber()[i]%>+'">'+<%=lsListOfFiles[1].getVersionNumber()[i]%>+'</OPTION>';
        <%}%>
    }
}

回答1:

Yes you can have something like this

function addCombo() {
    var textb = document.getElementById("txtCombo");
    var combo = document.getElementById("combo");

    var option = document.createElement("option");
    <c:forEach var="state" items="${stateList}" varStatus="status">  
    option.text = "${state}";
    option.value = "${state}";
    try {
        combo.add(option, null); //Standard
    }catch(error) {
        combo.add(option); // IE only
    }
    </c:forEach>
    textb.value = "";
} 
  • Also See

Note: I haven't tested this code , this is just a demonstration



回答2:

If the javascript is inline or declared in the same jsp page, there is no problem. Something like:

<script type="text/javascript">
var foo = '${foo}'; // or <%= foo => if you like
</script>

If it is in a separate .js file, then you should serve the .js file through a special servlet.