首页 技术 正文
技术 2022年11月10日
0 收藏 347 点赞 3,335 浏览 16097 个字

在上一章中总结了Web开发中应用MVC架构模式,将Servlet 用做控制器,JSP作为视图,JavaBean作为模型,实现业务流程控制,页面逻辑和业务逻辑的分离。然而,使用前面的技术实现MVC,并不是最完美的。

在当代的一个最佳的JavaWeb开发实践就是在MVC架构模式中,中和使用JavaBean,EL(expression language),JSP自定义标记库以及JSP标准标记库JSTL,编写无java脚本的JSP页面。

一 JSP表达式语言EL

在使用基于MVC架构模式的Web应用开发中,我们使用Servlet解析用户的请求,调用业务逻辑或数据访问代码,将得到的结果放入Bean中,然后将Bean出处在请求,会话或Servlet上下文中。最后将请求转发给JSP页面。JSP页面通过标准动作jsp:getProperty,和jsp:useBean得到储存在作用域中的Bean属性数据,显示在页面上。我们使用EL就是在最后的步骤不一样。我们不在使用jsp:getProperty和jsp:useBean。不同之处看看下面的案例。

有两个示例的JavaBean,Person和Dog。 Person有一个字符串类型的userName属性,同时有一个Dog类型的dogName属性。Dog有一个字符串类型的name属性。

我们取出人的名字,和dog的名字。

Dog

 package com.cy.bean; import java.io.Serializable; public class Dog  implements Serializable{
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }

Person

 package com.cy.bean; import java.io.Serializable; public class Person implements Serializable {
private String userName;
private Dog dogName; public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Dog getDogName() {
return dogName;
}
public void setDogName(Dog dogName) {
this.dogName = dogName;
} }

编写一个Servlet作为控制器,在控制器中创建Person实例。将请求转发到.jsp页面。

 package com.cy.servlet; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.cy.bean.Dog;
import com.cy.bean.Person; public class MyDog extends HttpServlet{
private static final long serialVersionUID = 1L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException { resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8"); Person p= new Person();
p.setUserName("小明");
Dog d=new Dog();
d.setName("小狗狗");
p.setDogName(d);
req.setAttribute("person", p); req.getRequestDispatcher("/jsp/myDog.jsp").forward(req, resp); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
} }

在web.xml中配置servlet

   <servlet>
<servlet-name>mydog</servlet-name>
<servlet-class>com.cy.servlet.MyDog</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mydog</servlet-name>
<url-pattern>/mydog</url-pattern>
</servlet-mapping>

作为视图的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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'myDog.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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
--> </head> <body>
<jsp:useBean id="person" type="com.cy.bean.Person" scope="request"></jsp:useBean>
主人:<jsp:getProperty property="userName" name="person"/><br/>
宠物:<jsp:getProperty property="dogName" name="person"/><br/>
</body>
</html>

得出来的结果却是:

编写无Java脚本的JSP页面

因为:person的属性dogName是个对象,而<jsp:getProperty>的Property属性只能访问Bean的属性,而不能访问嵌套的属性。

解决方案就是JSP EL(即表达式语言 Expression Language)

我们使用EL

 <%@ 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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'myDog.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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
--> </head> <body> 主人: ${person.userName}
宠物: ${person.dogName.name }
</body>
</html>

结果:

编写无Java脚本的JSP页面

1.1 EL表达式和JSP脚本表达式

EL表达式形式:${ }

${person.userName}     ${userName}

JSP表达式形式:<%= userName%>

一个<div>元素的style属性的代码:

JSP表达式设置:  <div style=<%=mystyle%>>hello</div>

EL表达式设置:  <div style=${mystyle} >hello</div>

EL表达式不能在脚本中定义变量

JSP中定义一个变量,只需要用<%! int num=10%>

num的值:<%=num%>   值为10;

num的值:${num}     返回一个未定义的值;

1.2在EL表达式中使用隐式变量

类别

隐式变量名

描述

JSP

pageContext

用来访问JSP的隐式对象

作用域

pageScope

与page作用域属性的名称和值相关联的 Map 类

requestScope

与request作用域属性的名称和值相关联的 Map 类

sessionScope

与session作用域属性的名称和值相关联的 Map 类

applicationScope

与application作用域属性的名称和值相关联的 Map 类

请求参数

param

包含请求参数字符串的Map 类

paramValues

包含请求参数字符串数组( String[])的 Map 类

请求头

header

包含请求头字符串的 Map 类

headerValues

包含请求头字符串数组(String[])的 Map 类

Cookie

cookie

按名称存储请求附带的 cookie 的 Map 类

注意:pageScope,requestScope,sessionScope,applicationScope这些变量并没有直接赋予我们在EL中直接访问真正的page,ServletRequset,HttpSession以及ServeltContext的权利,他们返回的只是一个分别与page作用域,request作用域,session作用域,application作用域有关的属性名和值的Map对象。

当容器解析EL表达式中的变量时,例如${x}中的x,容器首先检查隐式变量。如果隐式变量中找不到x,他将依次查找page,request,session,application范围的属性,如果还找不到x,则返回null。

测试EL表达式中是个隐式变量

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="juxing" uri="http://function/getJuXing" %>
<%
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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>隐式变量</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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
</head> <body>
<br/>---------------- 1 pageContext对象 :获取JSP页面中的pageContext对象---------------------- <br/>
${pageContext}<br/> <br/>---------------- 2 pageScope对象 :从page域(pageScope)中查找数据--------------------------<br/>
<% pageContext.setAttribute("name","kitty");%> <!-- map -->
${pageScope.name}<br/> <br/>---------------3 requestScope对象:从request域(requestScope)中获取数据------------------------<br/>
<% request.setAttribute("name", "kitty1"); %> <!-- map -->
${requestScope.name}<br/> <br/>---------------4 sessionScope对象:从session域(sessionScope)中获取数据------------------------<br/>
<%session.setAttribute("name", "kitty2"); %> <!-- map -->
${sessionScope.name}<br/> <br/>---------------5 applicationScope对象:从application域(applicationScope)中获取数据------------------------<br/>
<%application.setAttribute("name", "kitty3"); %><!-- map -->
${applicationScope.name}<br/> <br/>--------------6 param对象:获得用于保存请求参数map,并从map中获取数据------------------------<br/>
<br/>--------------7 paramValues对象:paramValues获得请求参数 ------------------------<br/>
param或paramValues变量使我们可以获取ServletRequest中传递过来的参数值。<br/>
param相当于是使用参数名,调用getParameter(String name)后 得到的结果。<br/>
paramValues是使用getParameter(String[] name)方法所返回的指定参数名的数组值<br/> <br/>--------------8 header对象:header获得请求头------------------------<br/>
${header.Accept}<br/> <br/>--------------9 headerValues对象:headerValues获得请求头的值------------------------<br/>
<%--headerValues表示一个保存了所有http请求头字段的Map对象,它对于某个请求参数,返回的是一个string[]数组
例如:headerValues.Accept返回的是一个string[]数组 ,headerValues.Accept[0]取出数组中的第一个值
--%> ${headerValues.Accept[0]}<br/>
<br/>--------------10、cookie对象:cookie对象获取客户机提交的cookie------------------------<br/>
<!-- 从cookie隐式对象中根据名称获取到的是cookie对象,要想获取值,还需要.value -->
${cookie.JSESSIONID.value} //保存所有cookie的map
</body>
</html>

结果:

编写无Java脚本的JSP页面

1.3 EL运算符

运算符可以分为四类:属性和集合访问运算符,算术运算符,关系运算符,逻辑运算符。

1.3.1用于访问属性和集合的EL运算符

属性访问运算符是我们可以访问对象的成员,集合访问运算符可以返回Map,List 或Array中的元素。

1.3.2 EL算数,关系,逻辑运算符

1.3.2.1 算术运算符就是加减乘除取模;

1.3.2.2 关系运算符

编写无Java脚本的JSP页面

1.3.2.3 逻辑运算符

编写无Java脚本的JSP页面

1.3.2.4 其他

empty运算符:检查对象是否为null(空)

二元表达式:${user!=null?user.name :””}

 <%@ 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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>运算符</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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
--> </head> <body>
<br/>--------------------用于访问属性和集合的EL运算符----------------<br/>
<%
Map<String,String> map = new LinkedHashMap<String,String>();
map.put("a","aaaaxxx");
map.put("b","bbbb");
map.put("c","cccc");
map.put("1","aaaa1111");
request.setAttribute("map",map);
%>
${map.a}<br/>
${map.b}<br/>
${map["1"]}<br/> <br/>--------------------算术运算符----------------<br/>
5+5= ${5+5}<br/>
5-5= ${5-5}<br/>
5*5= ${5*5}<br/>
5/5= ${5/5}<br/><%-- ${5div5} --%>
5%3= ${5%3}<br/><%-- ${5mod3} --%>
<br/>--------------------关系运算符----------------<br/>
${5==5} <br/>
${5!=6} <br/>
${5<=5} <br/><%-- ${5 le 6} --%>
${5<} <br/><%-- ${5 lt 6} --%>
${5>=5} <br/><%-- ${5 ge 6} --%>
${5> 4} <br/> <%-- ${5 gt 6} --%>
<br/>--------------------是否为空----------------<br/>
<%
List<String> list = new ArrayList<String>();
list.add("1111");
list.add("222");
request.setAttribute("list",list);
%>
${empty(list)}
<br/>--------------------条件运算----------------<br/>
${3==5?3:5} </body>
</html>

结果显示:

编写无Java脚本的JSP页面

1.4 函数

EL函数使用起来比较复杂,但是他为JSP提供了完全的业务逻辑和表现逻辑分离。

使用EL函数的步骤–方法类(.java):包含需要在JSP中使用的Java方法;–标记库描述文件(*.tld):映射每个Java方法到XML函数名;–部署描述文件(web.xml):映射TLD到标记库URI

JSP页面(*.jsp):使用标记库URI和函数名调用方法

示例:打印个矩形方法。

Rectangle

1.4.1 创建静态方法

Rectangle.java
 package com.cy.functionn; public class Rectangle{
/*方法必须被声明为public和static,而方法所属的类必须声明为public。这样Servlet就可以直接访问该类及其方法,而不用创建一个新对象*/
/*方法的参数和返回值必须在EL中是有效的。*/
/*类文件一般保存在/WEB-INF/下*/
public static String getRectangle(int clos,int rows){
StringBuffer sb=new StringBuffer();
for(int i=0;i<clos;i++){
for(int j=0;j<rows;j++){
sb.append("*");
}
sb.append("<br/>");
}
return sb.toString();
} }

1.4.2 创建标记库描述文件(TLD)

function.tld

Rectangle
 <!--TLD的目标是讲静态方法映射到可以在JSP中使用的函数名,这是必须的,因为EL中不允许直接调用java方法 -->
<taglib xmlns="http://java.sun.com.xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com.xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
version="2.0"> <tlib-version>1.0 </tlib-version>
<function>
<name>aaa</name><!-- 定义要在JSP中使用的函数名 -->
<function-class>com.cy.functionn.Rectangle</function-class><!-- 定义方法所在的类的全名 -->
<function-signature>java.lang.String getRectangle(int,int)</function-signature><!--定义静态方法及其参数和返回值的全数据类型 -->
</function> </taglib>

1.4.3 更改部署描述文件

1

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>EL</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <jsp-config>
<taglib>
<taglib-uri>http://function/getRectangle</taglib-uri><!-- 定义用于 Servlet, JSP要访问的路径-->
<taglib-location>/WEB-INF/tld/function.tld</taglib-location><!-- 定义标记库描述文件的上下路径 -->
</taglib>
</jsp-config> </web-app>

1.4.4 在JSP内访问EL函数

1

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!-- 使用taglib指令访问TLD,并用一个前缀代表该标记库。使用TLD前缀和函数名创建一个EL表达式。
prefix:定义前缀
uri:定义路径与taglib-uri相同
-->
<%@ taglib prefix="myFun" uri="http://function/getRectangle"%>
<%
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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'MyJsp.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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
--> </head>
<body>
<pre>${myFun:aaa(6,10)}</pre>
</body>
</html>

编写无Java脚本的JSP页面

二 JSP自定义标记库与标准标记库JSTL

JSTL(JSP标准标准库,JavaServlet Page Standard Tag Liabary)

jstl规范是SUN JCP指定的一个官方java规范请求(JSR).

JSTL标准标记库

功能范围

作用

URI

前缀

核心(core)

一般用途处理的标记

http://java.sun.com/jsp/jstl/core

c

xml

解析、选择、转换XML数据的标记

http://java.sun.com/jsp/jstl/xml

x

数据库(sql)

访问关系型数据库的标记

http://java.sun.com/jsp/jstl/sql

sql

国际化(I18N)

为国际化应用格式化数据的标记

http://java.sun.com/jsp/jstl/fmt

fmt

函数(Functions)

处理字符串和集合的标记

http://java.sun.com/jsp/jstl/functions

fn

我们最常用的是核心标记库。

为了给JSP提供JSTL能力,我们需要两个JAR文件。第一个文件是jstl.jar,他为JSTL标记库提供API类。

第二个文件是standard.jar,他提供了标记库的实现类。我们需要把这两个文件复制到我们自己的应用程序的WEB-INF下的lib目录。

我们需要在JSP中通过taglib指令引用标记库。

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

2.1 通用标记

<c:catch > ,<c:out>

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'jstl.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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
</head>
<body>
<%-- 标记<c:catch>允许我们在jsp中不需要错误页面捕获程序错误。标记<c:out>与JSP脚本表达式类似,用于显示内容 --%>
<c:catch var="a">
会抛异常的代码 ,异常的值会存储在页面范围变量a中。<br/>
</c:catch>
<c:out value=""></c:out>
如----------------------------:<br/>
<c:catch var="e">
${"1aaa"+1} <!-- 会抛异常的代码 -->
</c:catch>
<c:out value="e"></c:out><br/>
<c:out value="${e}"></c:out> <br/>
</body>
</html>

显示:

编写无Java脚本的JSP页面

2.2 变量支持标记

1<c:set>:用于设置变量和对象的值;

2<c:remove>:用于讲一个变量从其作用域中删除;

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'jstl.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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
</head>
<body>
<c:set var ="num"> ${8*7} </c:set>
<c:set var ="num" value="${8*7}"></c:set>
<% Map<String,String> map = new LinkedHashMap<String,String>();
map.put("a","aaaaxxx");
map.put("b","bbbb");
request.setAttribute("map",map);
%>
<!-- 用于javabean和map对象 -->
<c:set target="${map}" property="a"> 狗狗</c:set>
${map.a} <br/>
<c:set target="${map}" property="b" value="小狗狗"></c:set>
${map.b} <br/>
${num}<br/>
remove标记不用于javaBean和Map对象<br/>
<c:remove var="num" scope="page"/>
${num}<br/>
</body>
</html>

编写无Java脚本的JSP页面

2.3 流程控制

2.3.1 JSTL条件处理

2.3.2 循环

1 <c:if> <c:choose>条件处理

2 <c:forEach><c:forTokens> 循环

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'jstl.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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
</head>
<body> test:设置布尔表达式,true,false.
<br />
<c:if test="${9==9}">
你好!<br />
</c:if>
<c:choose>
<c:when test="${5!=5}">
错误<br />
</c:when> <c:when test="${5==5}">
正确1<br />
</c:when> <c:when test="${5!=5}">
正确2<br />
</c:when> </c:choose> x:变量 begin:起始值 end:结束值 step:间隔值
<c:forEach var="x" begin="0" end="10" step="3">
${x}
</c:forEach>
<br /> <c:set var="str" value="hello,world,kitty"></c:set>
items:要处理的用分隔符分隔的字符串对象; delims:指定分隔符
<c:forTokens var="bl" items="${str}" delims=",">
${bl}
</c:forTokens> </body>
</html>

编写无Java脚本的JSP页面

2.4 用JSTL访问URL信息

2.4.1 <c:url> 重写URL并对其参数编码

2.4.2 <c:import> 访问Web应用程序之外的内容

2.4.3 <c:redirect> 重定向到不同的URL

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,893
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,421
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,240
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,052
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,683
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,718