JSTL 标签库
*
注:此处声明标签库引入易出现一些问题 看最下面第三大点对JSTL引入问题的介绍
复习 参考代码: ManagerSystem/WebContent/jstl.jsp
此外:
复习时注意查看利用EL和JSTL将ManagerSystem进行修改的代码
如:
在login.jsp中:
进行逻辑判断时
旧版利用jsp原生态代码判断请求逻辑login.jsp部分内容如下
<%
//声明java代码块进行错误提示语的逻辑校验
Object obj=request.getAttribute("flag");
if(obj!=null){
%>
<div style="text-align: center;">
<span style="font-size: 15px;color:darkred;font-weight: bold;">用户名或密码错误</span>
</div>
<%} %>
<%
//声明java代码块进行密码修改提示语
Object pwd=session.getAttribute("pwd");
if(pwd!=null){
%>
<div style="text-align: center;">
<span style="font-size: 15px;color:darkred;font-weight: bold;">密码修改成功</span>
</div>
<%}
session.removeAttribute("pwd");//删掉这个属性 //使得刷新页面没有提示‘密码修改成功’的提示
%>
<%
//声明java代码块进行注册提示语
Object reg=session.getAttribute("reg");
if(reg!=null){
%>
<div style="text-align: center;">
<span style="font-size: 15px;color:darkred;font-weight: bold;">注册成功</span>
</div>
<%}
session.removeAttribute("reg");
%>
利用EL和JSTL更改之后为:
<!-- 下面是使用JSTL和EL表达式完成提示语 -->
<c:choose>
<c:when test="${flag==0}">
<div style="text-align: center;">
<span style="font-size: 15px;color:darkred;font-weight: bold;">用户名或密码错误</span><!-- 不把提示语写在Servlet中是为了解耦 -->
</div>
</c:when>
<c:when test="${flag==1}">
<div style="text-align: center;">
<span style="font-size: 15px;color:darkred;font-weight: bold;">密码修改成功</span>
</div>
</c:when>
<c:when test="${flag==2}">
<div style="text-align: center;">
<span style="font-size: 15px;color:darkred;font-weight: bold;">注册成功</span>
</div>
</c:when>
</c:choose>
<!-- 下面这句是移除session中的flag标记-->
<c:remove var="flag" scope="session" /><!-- 用来删除session里每个提示语和用于信息之类的东西 -->
旧版利用jsp原生态代码判断请求逻辑showUser.jsp部分内容如下
<%
List<User> list=(ArrayList<User>)request.getAttribute("list");//对应的是UserServlet请求转发的方法
//List<User> list=(ArrayList<User>)request.getSession().getAttribute("list");//对应的是UserServlet重定向的方法
for(User u:list){
%>
<tr>
<td><%=u.getUid() %></td>
<td><%=u.getUname() %></td>
<td><%=u.getPwd() %></td>
<%if("1".equals(u.getSex())){ %>
<td>男</td>
<%}else{ %>
<td>女</td>
<%} %>
<td><%=u.getAge()%></td>
<td><%=u.getBirth() %></td>
</tr>
<%}%>
利用EL和JSTL更改之后为:
<c:forEach items="${list}" var="u"><!-- u是一个user对象 -->
<tr>
<td>${u.uid}</td>
<td>${u.uname}</td>
<td>${u.pwd}</td>
<td>${u.sex=="1"?'男':'女'}</td>
<td>${u.age}</td>
<td>${u.birth}</td>
</tr>
</c:forEach>
三。jstl引入问题
1.引入JSTL库的方法:
https://blog.csdn.net/leisure_life/article/details/73559183
引入时出现了很麻烦的错误 查找了半天#_#
2.解决org.apache.jasper.JasperException: Failed to load or instantiate TagLibraryVal
方法:https://blog.csdn.net/mayjay517/article/details/78566028
3.错误处理——According to TLD or attribute directive in tag file, attribute test does not accept any express
应用部署运行的时候出现JSP异常, 发生在使用JSTL库的时候: According to TLD or attribute directive in tag file, attribute value does not accept any expressions, 可能是因为使用了JSP2.0版本, 同时又没有使用JSTL core库的备用版本(RT库), 一种简单的解决方法是使用JSTL core RT库
即引入
<%@ taglib uri=http://java.sun.com/jstl/core_rt prefix=“c”%>
方法: https://blog.csdn.net/jasper_success/article/details/6693434