目录

jstl中的choose语句,类似我们常接触的if/else语句。很早以前不知道,希望能帮到大家。

首先在我们需要在jsp中引入jstl的标签库和函数库

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

1. jsp中传递参数,经过action,存储到request中

<a href="${pageContext.request.contextPath}/news/queryNews.do?artType=1"></a>

2. java获取传入的值,然后传到其它jsp页面

String artType=request.getParameter("artType");
  if(artType!=null && !artType.equals("")){
   request.setAttribute("artType",artType);
  }

3. jsp中使用c:choose来判断,再输出

<c:choose>
   <c:when test="${artType == '1'}">  
         作品名称: ${other.other}<br />
         作品编号: ${other.other}<br />       
   </c:when>
   <c:when test="${artType == '2'}">  
         小组名称: ${other2.other}<br />
         小组编号: ${other2.other}<br />       
   </c:when>
   <c:otherwise>
     班级: ${other3.other}<br />
     参赛编号: ${other3.other}<br /> 
   </c:otherwise>
</c:choose>

1. 如果在jsp中传递参数,没有经过action ,直接跳转到jsp页面

<a href="${pageContext.request.contextPath}/reg.jsp?newFlag=${requestScope.newFlag}">我要注册</a>

2. 在jsp中取得jsp传过来的参数,此时要使用${param.newFlag}

<c:choose>
   <c:when test="${param.newFlag== '1' || param.newFlag== '2' ||param.newFlag== '3'}">
       <th>作品名称<font color="Red">*</font>:</th>
   </c:when>
   <c:otherwise> 
       <th>班级<font color="Red">*</font>:</th>
   </c:otherwise>
</c:choose>