一、MVC设计模式
MVC是一个设计模式,它强制性的使应用程序的输入、处理和输出分开。使用MVC设计模式被分成三个核心层:模型层、视图层、控制层。它们各自处理自己的任务,各层的任务如下:
显示层(View):此层主要是负责将内容显示给用户。 比如:JSP.
控制层(Controller):此层的主要负责所有的用户请求参数,判断请求参数是否合法,根据请求的类型调用模型层执行操作并将最终的处理结果交由显示层进行显示。 eg:Servlet
模型层(Model):操作数据库的独立的操作组件,或使用JavaBean(POJO)保存数据。
二、JSP内置对象
在JSP中为了简化用户的开发,提供了九个内置对象,这些内置对象将由容器为用户进行实例化,而用户直接使用即可,而不用像在java中那样,必须通过关键字new进行实例化对象之后才可以使用
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" errorPage="error.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
</head>
<body>
<%
int x=100/0;
%>
</body>
</html><%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>处理错误的页面</title>
</head>
<body>
<%
String errorMsg=exception.getMessage();
out.println("<h3>问题:"+errorMsg+"</h3>");
%>
</body>
</html>三、EL表达式语言
表达式语言(Expression Language)是JSP2.0中新增的功能。使用表达式语言(EL)可以在JSP页面进行方便地输出内容。EL语法为:${表达式}
EL也有自己的内置对象:
pageContext、pageScope、requestScope、sessionScope、applicationScope、param、paramValues、 header、headerValues、cookie、 initParam。
1.使用EL访问不同的属性范围:
${pageScope.属性名}
${requestScope.属性名}
${sessionScope.属性名}
${applicationScope.属性名}
注意:
如果使用 "${属性名}" 的方式访问属性范围,
则属性范围的访问顺序是pageContext、request、session、application。
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL访问不同属性范围的值</title>
</head>
<body>
<%
pageContext.setAttribute("info","page范围的属性值");
request.setAttribute("info","request范围的属性值");
session.setAttribute("info","session范围的属性值");
application.setAttribute("info","application范围的属性值");
%>
<h3>page===> ${pageScope.info}</h3>
<h3>request===> ${requestScope.info}</h3>
<h3>session===> ${sessionScope.info}</h3>
<h3>application===> ${applicationScope.info}</h3>
<h3>${info}=====> ${info}</h3>
</body>
</html>2.EL访问JSP内置对象使用EL的pageContext内置对象访问JSP的内置对 象。
${pageContext.对应的jsp内置对象}
例如:
EL获取上下文路径:
特殊:
${pageContext.servletContext.contextPath}
EL访问session的ID:
${pageContext.session.id}
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL访问jsp内置对象</title>
</head>
<body>
<%
String contextPath=application.getContextPath();
%>
<h3>直接通过JSP内置对象获取上下文路径:<%=contextPath%></h3>
<h3>通过EL获取上下文路径:${pageContext.servletContext.contextPath}</h3>
<h3>通过EL获取sessionID: ${pageContext.session.id}</h3>
<h3>通过EL判断当前的请求方式: ${pageContext.request.method}</h3>
</body>
</html>3.EL访问参数(访问客户端发送的参数、全局参数、 一组参数)用途一:
使用EL的param内置对象访问客户端发送的参数
${param.参数名}
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>param内置对象访问客户端发送的参数</title>
</head>
<body>
<h3>客户端发送的用户名为:${param.username}</h3>
<h3>客户端发送的用户密码为:${param.pwd}</h3>
<h3>${cookie["JSESSIONID"].name}</h3>
<h3>客户端的JSESSIONID:${cookie["JSESSIONID"].value}</h3>
<h3>服务器端sessionID:${pageContext.session.id}</h3>
</body>
</html> 用途二:使用EL的initParam内置对象访问上下文参数(全局参数)
在web.xml中配置上下文参数:
<context-param>
<param-name>admin</param-name>
<param-value>Lucy</param-value>
</context-param>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>initParam内置对象访问上下文参数</title>
</head>
<body>
<h3>上下文参数是:${initParam.contextValue}</h3>
</body>
</html>用途三:使用EL的paramValues访问一组参数
${paramValues.参数名[0]} 获取提交来的第一个参数值
${paramValues.参数名[n]} 获取提交来的第(n+1)个参数值
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>兴趣爱好</title>
</head>
<body>
<form action="paramValues.jsp" method="post">
<input type="checkbox" name="hobby" value="sport">运动 <br/>
<input type="checkbox" name="hobby" value="music">音乐<br/>
<input type="checkbox" name="hobby" value="movie">电影<br/>
<input type="submit" value="注册">
</form>
</body>
</html><%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>paramValues访问一组参数</title>
</head>
<body>
<h3>${paramValues.hobby[0]}</h3>
<h3>${paramValues.hobby[1]}</h3>
<h3>${paramValues.hobby[2]}</h3>
</body>
</html>4.访问cookie举个例子:
通过EL的cookie内置对象访问JSESSIONID名称的 cookie的语法:
${cookie["JSESSIONID"].name}
${cookie["JSESSIONID"].value}
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL的cookie内置对象访问Cookie</title>
</head>
<body>
<h3>Cookie名为JSESSIONID的值为: ${cookie["JSESSIONID"].value}</h3>
<h3>sessionID为:${pageContext.session.id}</h3>
</body>
</html>5.访问header使用EL的header内置对象访问请求header信息
比如:
${header["cookie"]}
EL运算符
1. 算数运算符
+ - * /(div) %(mod)
eg: ${param.num1+param.num2} 参数相加
2. 关系运算符
< (lt) > (gt) == (eq) !=(ne) >=(ge) <=(le)
3. 逻辑运算符
&&(and) ||(or) !(not)
4. empty运算符
${empty 表达式} 判断是否为null或空字符串””
5. 三目运算符
${返回true或false的表达式 ? "为true时输出的内容":"为false时输出的内容"}
6. 括号运算符
()用来改变运算顺序的
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>EL运算符</title>
</head>
<body>
<h3>num1+num2=${param.num1+param.num2}</h3>
<h3>num1/num2=${param.num1 div param.num2}</h3>
<h3>num1与num2是否相等:${param.num1==param.num2}</h3>
<h3>${empty param.abc}</h3>
<h3>num1与num2的关系:${param.num1>param.num2?"num1大于num2":"num1小于num2"}</h3>
</body>
</html>四、JSTL(JSP Standard Tag Libraries)引入JSTL的步骤:
第一步:将jstl-xx.jar导入/WEB-INF/lib文件夹下。
第二步:将jstl-xx.jar解压后的META-INF文件夹下的xxx.tld文件拷贝到/WEB-INF/的某目录下
第三步:在JSP页面使用taglib指令引入xxx.tld文件。
1、SP页面使用taglib指令引入xxx.tld文件
<%@ taglib uri="指向xxx.tld文件的路径" prefix="JSTL标签前缀"%>
注意:uri中指向xxx.tld文件的路径有两种写法:
第一种:直接指向xxx.tld文件的路径
第二种:在web.xml中配置taglib的uri
<jsp-config>
<taglib>
<taglib-uri>http://www.xxx.com</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
</jsp-config>
1.输出标签
<c:out value="输出的内容,支持EL" default=" 默认值,支持EL"/>
2.设置标签
设置属性范围的属性值
<c:set var="属性名" value="属性值,支持EL" scope="属性范围"/>
设置对象的属性值
<c:set target=“${perObj}” property=“对象的属性名” value=“支持EL"/>
先创建一个Person类
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="pojo.Person"%>
<%@ taglib uri="http://www.mycompany.com" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>set属性值</title>
</head>
<body>
<c:set var="info" value="${param.username}" scope="request"/>
<h3> <c:out value="${requestScope.info}" default="默认值"></c:out> </h3>
<%
Person person=new Person();
request.setAttribute("per",person);
%>
<c:set target="${requestScope.per}" property="name" value="${requestScope.info}"/>
取出设置给Person的name属性值:<c:out value="${requestScope.per.name}"/>
</body>
</html>3.捕获异常标签<c:catch var="保存异常信息的属性名">
// 有可能发生异常的代码
</c:catch>
4.判断标签
<c:if test="判断表达式,支持EL">
// 判断结果为true,执行此处
</c:if>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://www.mycompany.com" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>JSTL的if标签</title>
</head>
<body>
<c:if test="${param.score>=60}">
<h3>恭喜,及格了~~~</h3>
</c:if>
</body>
</html>5.forEach标签(遍历list集合或Map集合)<c:forEach var="当前正在遍历的对象,作为属性用(msg)" items="要遍历的集合,支持EL">
${msg}
</c:forEach>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.util.*,pojo.Person"%>
<%@ taglib uri="http://www.mycompany.com" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>JSTL的foreach标签</title>
</head>
<body>
<%
List<Person> list=new ArrayList<Person>();
list.add(new Person("小明",25));
list.add(new Person("小华",22));
list.add(new Person("小丽",20));
list.add(new Person("小娟",70));
request.setAttribute("personList",list);
%>
<c:forEach items="${requestScope.personList}" var="per">
${per.name} ===========> ${per.age} <br/>
</c:forEach>
</body>
</html>6.choose标签<c:choose>
<c:when test="${param.score>=90}">
<h3>优秀</h3>
</c:when>
<c:when test="${param.score>=80}">
<h3>良好</h3>
</c:when>
<c:otherwise>
<h3>不及格,要加油!</h3>
</c:otherwise>
</c:choose>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://www.mycompany.com" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>JSTL的choose标签</title>
</head>
<body>
<c:choose>
<c:when test="${param.score>=90}">
<h3>优秀</h3>
</c:when>
<c:when test="${param.score>=70}">
<h3>一般</h3>
</c:when>
<c:when test="${param.score>=60}">
<h3>及格</h3>
</c:when>
<c:otherwise>
<h3>要加油,这次不及格!</h3>
</c:otherwise>
</c:choose>
</body>
</html>
本文详细介绍了MVC设计模式在JSP中的应用,包括模型、视图和控制器的概念,并深入探讨了JSP内置对象、EL表达式及其运算符、JSTL标签库的使用方法。
3万+

被折叠的 条评论
为什么被折叠?



