zoukankan      html  css  js  c++  java
  • JSTL的相关使用

    index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <!-- 先得定义一个taglib -->
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    	<!--
    	<link rel="stylesheet" type="text/css" href="styles.css">
    	-->
      </head>
      
      <body>
      <!-- 输出一个常量 -->
      <c:out value="this is a first demo"></c:out>
      <% 
      	String name="1q23";
      	session.setAttribute("name", name);
      	request.setAttribute("name", name);
      %>
      <!-- 输出一个变量 -->
      <!-- 这里通过el表达式输出了name的值 -->
      <!-- EL表达式中的几个对象 -->
      ${sessionScope.name }
      ${requestScope.name }
      ${responseScope.name }
      ${pageScope.name }
      <c:out value="${sessionScope.name}"></c:out><br>
      <!-- 输出转义字符  escapeXml设置为false-->
      <c:out value=" " escapeXml="false"></c:out><br>
      <c:out value=" " escapeXml="true"></c:out><br>
      <!-- 设置默认值如果这个变量不存在的话 -->
      <c:out value="${responseScope.name }" default="123"></c:out><br>
        This is my JSP page. <br>
      </body>
    </html>
    

    choose.jsp

    <%@page import="java.util.ArrayList"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<form action="chooose.jsp">
    		<input type="text" name="score" value="${param.score }">
    		<input type="submit" value="点击">
    	</form>
    	<c:choose>
    		<c:when test="${param.score>90 }">
    			<c:out value="太棒了"></c:out>
    		</c:when>
    		<c:otherwise>
    			<c:out value="ohhhh"></c:out>
    		</c:otherwise>
    	</c:choose>
    	<!-- foreach函数  -->
    	<%
    		ArrayList<String> list=new ArrayList<String>();
    		list.add("x1");
    		list.add("x2");
    		list.add("x3");
    		list.add("x4");
    		list.add("x5");
    		list.add("x6");
    		session.setAttribute("list", list);
    	 %>
    	 <!-- 这里的var是对象的遍历内容 begin 和end是结束标准 step 是步长 varStatus是获取这里面的属性值 -->
    	<c:forEach var="f1" items="${list}" begin="0" end="5" step="1" varStatus="f">
    		<c:out value="${f1}"></c:out><br>
    	</c:forEach>
    	<!-- forTaken的用法 -->
    	<c:forTokens items="这里是分割字符串-的地方-这里的内容是-文本" delims="-" var="str">
    		<c:out value="${str }<br>" escapeXml="false"></c:out>
    	</c:forTokens>
    	<!-- 这个是导入网站 scope保存的区域-->
    	<c:import url="http://www.imooc.com" charEncoding="utf-8" scope="session">
    		
    	</c:import> 
    	<!-- 可以导入其他的本地的项目的jsp -->
    	<c:catch var="e">
    		<c:import url="/index.jsp" context="LoginDemo"></c:import>
    	</c:catch>
    	<c:out value="${e }"></c:out>
    </body>
    </html>
    

    redirect.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c1" %>
    <!-- 导入核心类库 -->
    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <!-- redirect标签 
    <c1:redirect url="index.jsp">
    	<c1:param name="user">123</c1:param>
    	<c1:param name="pass">456</c1:param>
    </c1:redirect>-->
    <!-- 下面是fn的例子 -->
    <c1:out value="${fn:contains('21332','21') }"></c1:out>
    </body>
    </html>
    

    set.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <!-- javaBean 的初始化 -->
    <jsp:useBean id="person" class="cn.lonecloud.Person"></jsp:useBean>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<!-- 设置session中的值 -->
    	<c:set value="demo" var="name1" scope="session"></c:set>
    	<c:out value="获取session中的值:${sessionScope.name }<br>" escapeXml="false"></c:out>
    	<!-- 设置javaBean中的值 target是获取实例名称 proerty 是设置实例中的哪个属性 -->
    	<!-- 必须注意的是target="${person }"  必须用El表达式不然会出错-->
    	<c:set  target="${person }" property="name" value="张三" ></c:set>
    	<c:out value="${person.name}"></c:out>
    	<!-- remove标签--只能remove一个变量 -->
    	<br>
    	<c:remove var="name1"/>
    	<c:out value="remove后的${name1 }"></c:out>
    	<!-- catch 块进行抓取error到里面去 -->
    	<c:catch var="err">
    		<c:out value="${3/0}"></c:out>
    	</c:catch>
    	<c:out value="err"></c:out>
    	<form action="set.jsp" method="post">
    		<!-- 必须设置name属性 -->
    		<input type="text" name="score" value="${param.score }">
    		<input type="submit" value="点击">
    	</form>
    	<c:out value="${param.score}"></c:out>
    	<c:if test="${param.score>90 }" var="result">
    		<c:out value="恭喜你"></c:out>
    	</c:if>
    	<c:out value="${result }"></c:out>
    </body>
    </html>
    

      

  • 相关阅读:
    jQuery下拉框操作系列$("option:selected",this) &&(锋利的jQuery)
    Jquery全选系列操作(锋利的jQuery)
    ASP.NET MVC 简单事务添加
    LINQ语法类似于SQL的语法
    C#(简单递归)和实现IComparable接口
    Jquery使用Id获取焦点和失去焦点
    解决使用C#打开第三方应用后进程关联问题
    使用 NSIS 制作安装包
    C# 窗口程序闪退
    C++ std::string 不可初始化为NULL
  • 原文地址:https://www.cnblogs.com/lonecloud/p/5578005.html
Copyright © 2011-2022 走看看