JSTL标签学习

1、需要引两个jar包:

 2、创建一个servlet的步骤:

完事。

这样做的目的,就是,不需要再在web.xml里面进行映射关系的书写了,它会在

类似于这种地方,把映射关系建立起来了。

3、开始讲JSTL标签了。

<!DOCTYPE HTML>
<html>
	<head>
		<base href="<%=basePath%>">
		<meta charset="UTF-8">
		<title>H5模版:</title>
	</head>
	<body>
		<h2>
		<%
		out.print(request.getAttribute("book").toString()) 
		%>
		</h2>
		<h2><%=request.getAttribute("book") %></h2>
		<h2>${requestScope.book }</h2>
		<hr>
		<!-- c:out必须配合EL表达式一起使用 -->
		<h2><c:out value="${requestScope.book }"></c:out></h2>
		<hr>
		<c:out value="${requestScope.shxt }" escapeXml="false">
		  <font color="red">没有数据</font>
		</c:out>
		<hr>
		${empty requestScope.shxt ? "<font color='red'>没有数据</font>" : request.shxt}
	</body>
</html>

 前面两种:

<%
        out.print(request.getAttribute("book").toString()) 

%>

<%=request.getAttribute("book") %>

too old to use.

可以用EL表达式。

或者:<!-- c:out必须配合EL表达式一起使用 -->

<c:out value="${requestScope.book }"></c:out>

下面可以进行校验:

<c:out value="${requestScope.shxt }" escapeXml="false">
          <font color="red">没有数据</font>
</c:out>

当我们用<c:out>输出一段带有html标签的文本时,由于escapeXml属性默认为true,内部的html标签会被转义。设置为false,则正常显示。

上面的代码就可以代替掉:

${empty requestScope.shxt ? "<font color='red'>没有数据</font>" : request.shxt}

这个容易出错,但是那个jstl的就很清晰。

4、

<!DOCTYPE HTML>
<html>
	<head>
		<base href="<%=basePath%>">
		<meta charset="UTF-8">
		<title>H5模版:</title>
	</head>
	<body>
	   <h2>
        <c:if test="${requestScope.book eq '西游记'}">
                                    吴承恩
        </c:if>
        <c:if test="${!(requestScope.book eq '西游记')}">
                                    孙悟空
        </c:if>
        </h2>
		<h3>
		<c:if test="${requestScope.book eq '西游记'}" var="flag">
		<!-- var的范围是pageScope -->
		  123
		</c:if>
		<c:if test="${!flag}">
          456
        </c:if>
		</h3>
	</body>
</html>

if语句的判断。

        <c:if test="${requestScope.book eq '西游记'}" var="flag">
        <!-- var的范围是pageScope -->
          123
        </c:if>
        <c:if test="${!flag}">
          456
        </c:if>

5、如果servlet代码如下写:

@WebServlet("/demo03")
public class Demo03Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("int", 88);
		request.getRequestDispatcher("/result03.jsp").forward(request, response);
	}
}

我们可以看到,传递的参数名字叫做“int”,显然这是一个关键字,如何进行获取值,而不会产生错误呢。

如果    ${requestScope.int }会报500的错误。

<!DOCTYPE HTML>
<html>
	<head>
		<base href="<%=basePath%>">
		<meta charset="UTF-8">
		<title>H5模版:</title>
	</head>
	<body>
	   <h2>
	   <c:choose>
        <c:when test="${requestScope['int'] > 0 and requestScope['int'] < 60}">
          <h3>不及格</h3>
        </c:when>
        <c:when test="${requestScope['int'] >= 60 and requestScope['int'] <= 75}">
          <h3>及格</h3>
        </c:when>
        <c:otherwise>
          <h3>优秀</h3>
        </c:otherwise>
       </c:choose>
        </h2>
	</body>
</html>

${requestScope['int']}这样取值,是不会出现500的错误的。

并且,这样写,就替换掉了c:if

6、循环输出

<!DOCTYPE HTML>
<html>
	<head>
		<base href="<%=basePath%>">
		<meta charset="UTF-8">
		<title>H5模版:</title>
	</head>
	<body>
	   <h2>
	   <c:forEach items="${requestScope.bookList}" varStatus="vs">
	       ${vs.index}--${vs.count }--${vs.first }--${vs.last }
	       <!-- index是索引 -->
	       <!-- count是计数器 -->
	       <!-- 相当于list的第一条数据 -->
	       <!-- 相当于list的最后一条数据 -->
	       --${requestScope.bookList[vs.index]["book"] }
	       --${requestScope.bookList[vs.index].author}<br>
       </c:forEach>
       <hr>
       <%--Map temp = list.get(i) --%>
       <c:forEach items="${requestScope.bookList}" var="temp" varStatus="vs">
           ${vs.index}--${vs.count }--${vs.first }--${vs.last }
           --${temp["book"] }
           --${temp.author}<br>
       </c:forEach>
        </h2>
	</body>
</html>

var指向当前值
varStatus可以获得与当前行数相关的一些信息
如varStatus='vs' 则可以用${vs.index}获得当前行号

var 是你要循环集合的别名
varStatus 是循环索引

https://www.cnblogs.com/zjdxr-up/p/7528307.html 

7、 路径是fmt_rt

        request.setAttribute("myDate", new Date());
        request.setAttribute("money", 123456.789);

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
<%
	//项目的发布路径,例如:  /rabc
	String path = request.getContextPath();
	/*
	全路径,形式如下: http://127.0.0.1:8001/rbac/
	request.getScheme()      ——> http 获取协议
	request.getServerName()  --> 127.0.0.1 获取服务名
	request.getServerPort()  --> 8001 获取端口号
	path                     --> /rbac 获取访问的路径 路
	*/
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html>
	<head>
		<base href="<%=basePath%>">
		<meta charset="UTF-8">
		<title>H5模版:</title>
	</head>
	<body>
	   <h2>${requestScope.myDate }</h2>
	   <h2>${requestScope.money }</h2>
	   <hr>
	   <h3>
	   <fmt:formatDate value="${requestScope.myDate }" pattern="yyyy.MM.dd HH:mm:ss"/>
	   </h3>
	   <h3>
       <fmt:formatNumber type="currency">${requestScope.money }</fmt:formatNumber>
       </h3>
	</body>
</html>

这个可以理解为:格式化。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值