简体   繁体   中英

Pass javascript variable to jstl tags

I need to change (parse) one of the object property. Is it any way I can save the parse value in the js variable and then pass it back. This is what I tried but it does not work:

    <c:forEach var="user" items="${requestScope.users}">
       var parsedName= parseUserName("${user.name}");
       <c:set target = "${user}" property = "parsedName" value ="${parsedName}"/>  
    </c:forEach>

Your idea is:

<c:forEach var="user" items="${requestScope.users}">
    <script type="javascript">
        var parsedName= parseUserName("${user.name}");
    </script>
    <c:set target = "${user}" property = "parsedName" value ="${parsedName}"/>
</c:forEach>

unfortunately it is not possible.

The jstl code happen on the server side and javascript on the client side.

No it is not possible to do what you are trying to do. The only possible solution is to send the variable to the server using a partial refresh and set the required property once the page has been refreshed

If possible try to implement the parseUserName() logic in JSP itself using JSTL.

If you need help with that post the JS method logic here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM