How to set the JSTL variable value in javascript?

后端 未结 8 2100
我在风中等你
我在风中等你 2020-11-27 16:56

How to set the JSTL variable value in java script?



        
相关标签:
8条回答
  • 2020-11-27 17:48

    one more approach to use.

    first, define the following somewhere on the page:

    <div id="valueHolderId">${someValue}</div>
    

    then in JS, just do something similar to

    var someValue = $('#valueHolderId').html();
    

    it works great for the cases when all scripts are inside .js files and obviously there is no jstl available

    0 讨论(0)
  • 2020-11-27 17:49

    As an answer I say No. You can only get values from jstl to javascript. But u can display the user name using javascript itself. Best ways are here. To display user name, if u have html like

    <div id="uName"></div>
    

    You can display user name as follows.

    var val1 = document.getElementById('userName').value;
    document.getElementById('uName').innerHTML = val1;
    

    To get data from jstl to your javascript :

    var userName = '<c:out value="${user}"/>';
    

    here ${user} is the data you get as response(from backend).

    Asigning number/array length

    var arrayLength = <c:out value="${details.size()}"/>;
    

    Advanced

    function advanced(){
        var values = new Array();
        <c:if test="${empty details.users}">
           values.push("No user found"); 
        </c:if>         
    
        <c:if test="${!empty details.users}">
            <c:forEach var="user" items="${details.users}" varStatus="stat">
                values.push("${user.name}");   
            </c:forEach>
        </:c:if>
        alert("values[0] "+values[0]);
    
    }); 
    
    0 讨论(0)
提交回复
热议问题