`

JSTL中Core的使用

    博客分类:
  • J2EE
阅读更多

先下载JSTL,然后将jstl.jar, standard.jar两个jar包copy到lib下,   将要使用的Core标签中的c.tld文件copy到WEB-INF下,修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <jsp-config>
  	<taglib>
  		<taglib-uri>jstl-c</taglib-uri>
  		<taglib-location>/WEB-INF/c.tld</taglib-location>
  	</taglib>
  </jsp-config>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
 定义一个taglib.jsp用于引入标签
<%@ taglib prefix="c" uri="jstl-c"%>
 
先看out标签的使用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@include file="taglib.jsp" %>
<%
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>c:out的使用</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">

  </head>
  
  <body>
    <h2 align="center">c:out 的作用就相当于<%="param1" %></h2><br>
    <h3>输出value的值要是value为空就输出default的值,escapeXml属性的值是转义用的</h3>
    <br><br>
    <c:out value="学习JSTL"></c:out><br>
    <c:out value="${sessionScope.username}" default="libinxuan"></c:out><br>
    <c:out value="${username}" default="libinxuan"></c:out><br>
	<c:out value="<h1>学习JSTL</h1>" escapeXml="false"></c:out><br>
	<c:out value="<h1>学习JSTL</h1>" escapeXml="true"></c:out><br>
  </body>
</html>
 set的使用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="jstl-c" 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>c:set的用法</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">

  </head>
  
  <body>
    <h2 align="center">作用是将变量保存到JavaBeans属性中或JSP页面的特定取值范围</h2><br><br>
    <h2>各属性的含义:</h2><br>
    <h3>    value: 要保存的内容,可以是EL表达式或常量</h3><br>
    <h3>    target: 要修改属性的对象名,一般为JavaBeans对象名</h3><br>
    <h3>    property: 要修改的JavaBeans的属性</h3><br>
    <h3>    var: 要保存内容的变量的名称</h3><br>
    <h3>    scope: 保存内容的作用范围</h3><br><br><br>
    <h2>注意: 指定了target属性就要同时指定property属性</h2><br><br>
    <c:set value="libinxuan" var="username" scope="session"></c:set><br>
    <c:set value="libinxuan" var="user" scope="session"></c:set><br>
    <c:set var="username2" scope="session">libinxuan</c:set><br><br>
    <h2>将value的值保存到target对象的propertyName属性中</h2><br>
    <c:out value="${sessionScope.user}" default="sss"></c:out>
  </body>
</html>
 remove的使用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="jstl-c" 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>c:remove的使用</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">

  </head>
  
  <body>
    <h2 align="center">作用是删除所设定的变量</h2><br><br>
    <h3>删除session中名为username的属性的值</h3><br>
    <c:remove var="username" scope="session"/>
  </body>
</html>
 if的使用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="taglib.jsp" %>
<%
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>c:if的使用</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">

  </head>
  
  <body>
  	<c:set value="libinxuan" var="name" scope="session"></c:set>
  	<c:out value="${sessionScope.name}"></c:out><br>
    <c:if test="${sessionScope.name == 'libinxuan'}">
    	欢迎管理员登录
    </c:if><br>
    <c:if test="${1>0}">
    	你好
    </c:if>
  </body>
</html>
 choose的使用,这里要借助一个jsp的请求input.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>用于测试choose,when,otherwise</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>
  	<form action="choose.jsp">
    	请输入你的年龄:<input type="text" name="age" id="age" >
    	<input type="submit" value="提交" >
    </form>	
  </body>
</html>
 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="jstl-c" 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>c:choose,c:when,s:otherwise的使用</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">

  </head>
  
  <body>
    <c:choose>
    	<c:when test="${param.age>70}">
    		老人
    	</c:when>
    	<c:when test="${param.age<=70 and param.age>=35 }">
    		中年人
    	</c:when>
    	<c:when test="${param.age>=0 and param.age<=35 }">
    		年轻人
    	</c:when>
    	<c:otherwise>
    		你不是人
    	</c:otherwise>
    </c:choose>
  </body>
</html>
 forEach的使用
<%@ page language="java" import="java.util.*"  pageEncoding="UTF-8"%>
<%@include file="taglib.jsp" %>
<%
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>c:forEach标签的使用</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">
	<%
    	String names[] = new String[4];
    	names[0]="张三";
    	names[1]="李四";
    	names[2]="王五";
    	names[3]="张六";
    	pageContext.setAttribute("names",names);
    %>
  </head>
  
  <body>
  	<h2 align="center">用于迭代字符串,数组,集合</h2>
    <h2>各属性的介绍:</h2><br/>
    <h3>     begin: 开始的索引</h3><br>
    <h3>     end: 介绍的索引</h3><br>
    <h3>     step: 步长</h3><br>
    <h3>     var: 当前迭代变量的名称</h3><br>
    <h3>     items: 进行循环的集合</h3><br>
    <h3>     varStatus: 显示循环状态的变量</h3><br>
    <c:forEach items="${names}" var="name" varStatus="i">
    	${name}<br>
    	${i.index}<br>
    </c:forEach>
  </body>
</html>
 

分享到:
评论

相关推荐

    JSTL中if标签使用方法

    ### JSTL中if标签使用方法详解 JSTL(JavaServer Pages Standard Tag Library)是为简化JSP页面开发而设计的一组标准标签库。它提供了处理数据、条件逻辑等功能的强大工具,使得开发者无需编写复杂的Java代码即可...

    javax/servlet/jsp/jstl/core/ConditionalTagSupport

    在Java Web开发中,`javax.servlet.jsp.jspl.core.ConditionalTagSupport`是JSTL(JavaServer Pages Standard Tag Library)库中的一个核心类,用于支持条件标签的实现。当你遇到`java.lang.NoClassDefFoundError: ...

    JSTL中文帮助文档

    本文将对JSTL的基础概念、核心标签库、XML处理标签库、国际化格式化标签库以及数据库访问标签库进行详细介绍,并探讨如何在JSP页面中有效地利用这些标签来提高开发效率。 #### 二、JSTL简介 JSTL是随JSP 1.2版本...

    JSTL-Core标签库.pdf

    但在JSP 2.0中,JSTL已经成为标准的一部分,这意味着它可以在更广泛的环境中使用。 - **版本**:当前最新的版本为1.02,但最终发布版为1.0。JSTL分为两个主要部分:标签库和EL(Expression Language,表达式语言)。...

    jsp中使用jstl标签的驱动

    在Java Web开发中,JSP(JavaServer Pages)是一种用于创建动态网页的技术,而JSTL(JavaServer Pages Standard ...在实际开发中,应当尽可能地避免在JSP页面中直接写Java脚本,转而使用JSTL等标签库来实现业务逻辑。

    jsp中使用JSTL的jar包

    **JSP中使用JSTL的Jar包** JavaServer Pages(JSP)是Java平台上的一个标准技术,用于创建动态web内容。为了增强JSP页面的功能性和可维护性,开发人员常常会使用JSP Standard Tag Library(JSTL)。JSTL提供了一...

    jstl中文参考手册

    - **减少脚本元素**:通过使用JSTL,可以在很大程度上避免在JSP页面中使用复杂的脚本元素,从而降低了软件维护的难度。 - **增强可维护性**:将逻辑封装在JSTL标签中,使得表示层更清晰,易于维护。 - **提高可读性*...

    jstl标签在jsp中使用问题

    在web项目中使用JSTL标签,JSTL 1.0 的声明是 &lt;%@ taglib prefix="c" uri="http://java.sun.com/jstl/core " %&gt; 所使用的web应用服务器resin与tomcat有所区别 resin\lib目录下存在jstl-1.2.jar、jta-101.jar包 而...

    JSTL标签库-tomcat10-简化JSP中java代码

    在Tomcat 10中,要使用JSTL,首先需要将jstl.jar和standard.jar添加到项目的lib目录下,或者将其配置在Tomcat的lib目录中以供全局使用。接着,在JSP页面中引入JSTL的标签库,通常通过以下方式: ```jsp ...

    JSTL Core标签库资料

    从JSP 1.1规范开始,JSP就支持在JSP中使用自定义标签了,自定义标签的广泛使用造成了程序员重复定义,这样就促成了JSTL(JavaServer Pages Standard Tag Library)的诞生。

    jstl标签库与使用教程

    在项目中使用JSTL,需要将`jstl.jar`和`standard.jar`两个库添加到类路径中。这两个文件通常位于`WEB-INF/lib`目录下。`standard.jar`包含了JSTL所需的EL表达式解析器和其他支持库。 **3. JSTL配置:** 在`web.xml`...

    jstl1.2标签库jar包

    在运行时,Web应用服务器需要这个JAR来执行在JSP页面中使用的JSTL标签。`jstl-impl-1.2.jar`包含了处理表达式语言(EL, Expression Language)、核心标签库(Core)、函数库(Function)、国际化(fmt)以及SQL操作...

    JSTL官方使用手册

    **JSTL官方使用手册**,全称...在JSP页面中使用JSTL,首先需要引入对应的JAR文件,如`jstl.jar`和`standard.jar`,并将它们放入项目的类路径中。然后在JSP页面中通过`&lt;%@ taglib %&gt;`指令导入JSTL库。 例如: ```jsp ...

    JSTL中文帮助文档_java_JSTL_

    1. **Core库**:这是JSTL中最基本的部分,包含用于控制流程、处理URL、操作集合等常用功能的标签。例如: - `&lt;c:if&gt;` 和 `&lt;c:choose&gt;` 用于条件判断。 - `&lt;c:forEach&gt;` 用于循环遍历集合。 - `&lt;c:set&gt;` 用于设置...

    jsp中标签JSTL中英文对照的中文帮助文档(chm格式)

    JSTL的出现是为了减少在JSP页面中使用过多的Java脚本和表达式,使页面更易于阅读、维护和优化。 **JSTL的组成部分** JSTL主要由以下几个核心部分组成: 1. **Core标签库**:提供了基本的控制结构,如迭代、条件...

Global site tag (gtag.js) - Google Analytics