How to pass java variables from scriptlets to c:when expression in jstl?

后端 未结 2 1322
独厮守ぢ
独厮守ぢ 2020-12-19 08:58

what is a proper way to use variables from scriptlets in jstl? I don\'t know what is wrong in my code:

<%
boolean a = true;
boolean b = false;
%>

<         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-19 10:03

    Variables in scriptlets cannot be seen in JSTL because Expression Language, the stuff between ${} used in JSTL, will look for attributes in page, request, session or application. You have to at least store the variable from scriptlet in one of these scopes, then use it.

    This is an example:

    <%
        boolean a = true;
        request.setAttribute("a", a);
    %>
    
    
        
    
    

    More info:

    • Expression Language StackOverflow wiki
    • JSTL StackOverflow wiki

    As a recommendation, stop using scriptlets. Move the business logic in your JSP to controller and the view logic into EL, JSTL and other tags like . More info: How to avoid Java code in JSP files?

提交回复
热议问题