使用if-else和html时出现jsp错误

时间:2009-08-05 04:43:22

标签: java html jsp

我的jsp页面中有以下内容(假设客户端是对象)

<%
 if( client == null)
 %>
NO client
 <% 
 else
%>
<a href='page.jsp?aid=<%=client.getID()%>'> and his name is  <%=client.getName()%>

感谢

2 个答案:

答案 0 :(得分:7)

你错过了括号:

<% if( client == null) {  %>
NO client
<% } else { %>
<a href='page.jsp?aid=<%=client.getID()%>'> and his name is  <%=client.getName()%>
<% } %>

那就是说,这是一个糟糕的JSP代码的例子。考虑使用JSTL标记/表达式而不是scriptlet。

答案 1 :(得分:3)

在jstl中它会类似于

<c:choose>
  <c:when test="${client is null}">
NO client
  </c:when>
<c:otherwise>
  <A href="<c:url value="page.jsp" >
    <c:param name="aid" value="${client.ID}" />
           </c:url>" 
  > and his name is <c:out value="${client.name}"/>
</c:otherwise>