<c:if> jstl 条件不工作
<c:if> jstl condition does not work
我是 初学者 java ee programing 我正在写 JSTL 在会话 bean 中验证登录状态是真还是假的条件。
<c:choose>
<c:if test="${Boolean.valueOf(Session.getLogStat())}">
${Session.getLogStat()}
</c:if>
<c:if test="${Boolean.valueOf(!Session.getLogStat())}">
<a href="login">login</a>
</c:if>
</c:choose>
当我将 if 替换为 when:
<c:choose>
<c:when test="${Boolean.valueOf(Session.getLogStat())}">
${Session.getLogStat()}
</c:when>
<c:when test="${Boolean.valueOf(!Session.getLogStat())}">
<a href="login">login</a>
</c:when>
</c:choose>
对不起我的英语不好。
第一个肯定是错的。
<c:choose>
必须有一个或多个 <c:when>
标签和可选的 <c:otherwise>
标签嵌套在里面。
<c:if>
是一个独立的结构。
Session.getLogStat()
的 return 值是多少?如果它是字符串 "true"
或 "false"
,那么 Boolean.valueOf()
是正确的,但这似乎不太可能。或许您的意思是 null
测试?
无论如何,你应该使用 <c:otherwise>
和 <c:out>
:
<c:choose>
<c:when test="${Session.getLogStat() != null}">
<c:out value="${Session.getLogStat()}"/>
</c:when>
<c:otherwise>
<a href="login">login</a>
</c:otherwise>
</c:choose>
我通常会将其折叠成多路 if
语句,在本例中是一个简单的 if-else
语句:
<c:choose><c:when test="${Session.getLogStat() != null}">
<c:out value="${Session.getLogStat()}"/>
</c:when><c:otherwise>
<a href="login">login</a>
</c:otherwise></c:choose>
如果您的问题仍然存在(您在回答中说:但代码不适用于...)
我建议您在 servlet 中实例化您的 bean。 servlet 将在请求中设置您的参数。然后你在 JSP 中测试你想要的东西。
public class ExampleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Session MyObject = new Session();
boolean myParam=MyObject.getLogStat();
String s1 = Boolean.toString(myParam);
try {
request.setAttribute("I_WANT_TO_TEST", s1);
RequestDispatcher view = request.getRequestDispatcher("My.jsp");
view.forward(request, response);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}}
然后在你的 JSP
<% String msg2=(String)request.getAttribute("I_WANT_TO_TEST");%>
<% if ( (msg2==null || msg2.isEmpty()) ) { %>
// WHAT YOU ARE NOT LOGED IN
<% } else if(msg2!=null) { %>
<% } %>
我是 初学者 java ee programing 我正在写 JSTL 在会话 bean 中验证登录状态是真还是假的条件。
<c:choose>
<c:if test="${Boolean.valueOf(Session.getLogStat())}">
${Session.getLogStat()}
</c:if>
<c:if test="${Boolean.valueOf(!Session.getLogStat())}">
<a href="login">login</a>
</c:if>
</c:choose>
当我将 if 替换为 when:
<c:choose>
<c:when test="${Boolean.valueOf(Session.getLogStat())}">
${Session.getLogStat()}
</c:when>
<c:when test="${Boolean.valueOf(!Session.getLogStat())}">
<a href="login">login</a>
</c:when>
</c:choose>
对不起我的英语不好。
第一个肯定是错的。
<c:choose>
必须有一个或多个 <c:when>
标签和可选的 <c:otherwise>
标签嵌套在里面。
<c:if>
是一个独立的结构。
Session.getLogStat()
的 return 值是多少?如果它是字符串 "true"
或 "false"
,那么 Boolean.valueOf()
是正确的,但这似乎不太可能。或许您的意思是 null
测试?
无论如何,你应该使用 <c:otherwise>
和 <c:out>
:
<c:choose>
<c:when test="${Session.getLogStat() != null}">
<c:out value="${Session.getLogStat()}"/>
</c:when>
<c:otherwise>
<a href="login">login</a>
</c:otherwise>
</c:choose>
我通常会将其折叠成多路 if
语句,在本例中是一个简单的 if-else
语句:
<c:choose><c:when test="${Session.getLogStat() != null}">
<c:out value="${Session.getLogStat()}"/>
</c:when><c:otherwise>
<a href="login">login</a>
</c:otherwise></c:choose>
如果您的问题仍然存在(您在回答中说:但代码不适用于...)
我建议您在 servlet 中实例化您的 bean。 servlet 将在请求中设置您的参数。然后你在 JSP 中测试你想要的东西。
public class ExampleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Session MyObject = new Session();
boolean myParam=MyObject.getLogStat();
String s1 = Boolean.toString(myParam);
try {
request.setAttribute("I_WANT_TO_TEST", s1);
RequestDispatcher view = request.getRequestDispatcher("My.jsp");
view.forward(request, response);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}}
然后在你的 JSP
<% String msg2=(String)request.getAttribute("I_WANT_TO_TEST");%>
<% if ( (msg2==null || msg2.isEmpty()) ) { %>
// WHAT YOU ARE NOT LOGED IN
<% } else if(msg2!=null) { %>
<% } %>