loading请求处理中...

JSP内置对象/EL/JSTL

2021-12-02 05:48:27 阅读 9367次 标签: java开发基础c#开发基础 作者: yf8488240

一:MVC设计模式

MVC是一个设计模式,它强制的使应用层的输入,处理和输出分开。使用MVC设计模式被分为三个核心层:模型层,视图层,控制层。它们各自处理自己的任务。

显示层(View):此层主要是负责将内容显式给用户。比如:JSP

控制层(Controller):负责判断所有的用户请求参数是否合法,根据请求的类型调用模型层执行操作,再讲处理结果交给显示层显示。eg:servlet

模型层(Model):操作数据库的独立的操作组件,或使用lavaBean(POJO)保存数据。

二:JSP内置对象

JSP中提供了九个内置对象,这些内置对象由容器为用户进行实例化,用户直接使用即可。

JSP内置对象/EL/JSTL

 eg:

[html] view plain copy
  1. (1):<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
  2. >  
  3. <html>  
  4. <head>  
  5. <meta charset="utf-8"/>  
  6. <title>response内置对象title>  
  7. head>  
  8. <body>  
  9.     <h3>3秒之后跳转到index页面h3>  
  10.     <%  
  11.       //  response.setHeader("refresh","3"); 定时刷新  
  12.       response.setHeader("refresh","3;URL=index.jsp");   // 定时跳转  
  13.     %>  
  14. body>  
  15. html>  
[html] view plain copy
  1. (2):<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
  2. >  
  3. <html>  
  4. <head>  
  5. <meta charset="utf-8"/>  
  6. <title>index.jsptitle>  
  7. head>  
  8. <body>  
  9.    <h3>跳转过来了h3>  
  10. body>  
  11. html>  
[html] view plain copy
  1. (3):<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" isErrorPage="true"%>  
  2. >  
  3. <html>  
  4. <head>  
  5. <meta charset="utf-8"/>  
  6. <title>处理错误的页面title>  
  7. head>  
  8. <body>  
  9.    <%  
  10.       String errorMsg=exception.getMessage();  
  11.       out.println("<h3>有点问题:"+errorMsg+"h3>");  
  12.    %>  
  13. body>  
  14. html>  

[html] view plain copy
  1. (4):xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <display-name>JSPProjectdisplay-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.htmlwelcome-file>  
  6.     <welcome-file>index.htmwelcome-file>  
  7.     <welcome-file>index.jspwelcome-file>  
  8.     <welcome-file>default.htmlwelcome-file>  
  9.     <welcome-file>default.htmwelcome-file>  
  10.     <welcome-file>default.jspwelcome-file>  
  11.   welcome-file-list>  
  12.     
  13.     
  14.   <context-param>  
  15.       <param-name>ctxparam-name>  
  16.       <param-value>ContextParamValueparam-value>  
  17.   context-param>  
  18.     
  19.   <jsp-config>  
  20.       <taglib>  
  21.           <taglib-uri>http://www.mycompany.comtaglib-uri>  
  22.           <taglib-location>/WEB-INF/c.tldtaglib-location>  
  23.       taglib>  
  24.   jsp-config>  
  25. web-app>  

三:EL表达式语言

使用表达式语言(EL)可以在JSP页面进行方便的输出内容:

EL语法:${表达式}

EL有自己的内置对象:

PageContext ,pageScope,requestScope,sessionScope,applicationScope,param,paramValues,header,headerValues,cookie,initParam

1. 使用EL访问不同的属性范围:

${pageScope.属性名}${requestScope.属性名},${sessionScope.属性名},${applicationScope.属性名},这四种属性访问范围由小到大。

Eg:

[html] view plain copy
  1. <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
  2. >  
  3. <html>  
  4. <head>  
  5. <meta charset="utf-8"/>  
  6. <title>EL访问不同属性范围的值title>  
  7. head>  
  8. <body>  
  9.    <%  
  10.        pageContext.setAttribute("info","page范围的属性值");  
  11.        request.setAttribute("info","request范围的属性值");  
  12.        session.setAttribute("info","session范围的属性值");  
  13.        application.setAttribute("info","application范围的属性值");  
  14.    %>  
  15.      
  16.    <h3>page===>  ${pageScope.info}h3>  
  17.    <h3>request===>  ${requestScope.info}h3>  
  18.    <h3>session===>  ${sessionScope.info}h3>  
  19.    <h3>application===>  ${applicationScope.info}h3>  
  20.    <h3>${info}=====>  ${info}h3>  
  21. body>  
  22. html>  

2. EL访问JSP内置对象

使用ELpageContext内置对象访问JSP的内置对象:${pageContext.对应的jsp内置对象}

Eg:EL获取上下文路径:特殊:${pageContext.servletContext.contextPath}

EL访问sessionID:${pageContext.session.id}

Eg::

[html] view plain copy
  1. <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
  2. >  
  3. <html>  
  4. <head>  
  5. <meta charset="utf-8"/>  
  6. <title>EL访问jsp内置对象title>  
  7. head>  
  8. <body>  
  9.    <%  
  10.        String contextPath=application.getContextPath();  
  11.    %>  
  12.    <h3>直接通过JSP内置对象获取上下文路径:<%=contextPath%>h3>  
  13.    <h3>通过EL获取上下文路径:${pageContext.servletContext.contextPath}h3>  
  14.    <h3>通过EL获取sessionID: ${pageContext.session.id}h3>  
  15.    <h3>通过EL判断当前的请求方式:  ${pageContext.request.method}h3>  
  16. body>  
  17. html>  

3. EL访问参数(访问客户端发送的参数。全局参数,一组参数)

用途1:使用ELparam内置对象访问客户端发送的参数${param.参数名}

eg:

[html] view plain copy
  1. <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
  2. >  
  3. <html>  
  4. <head>  
  5. <meta charset="utf-8"/>  
  6. <title>EL访问客户端发送的请求参数title>  
  7. head>  
  8. <body>  
  9.    <h3>客户端发送来的username参数是:${param.username}h3>  
  10.    <h3>客户端发送来的pwd参数是:${param.pwd}h3>  
  11. body>  
  12. html>  

用途2:使用ELinitParam内置对象访问上下文参数(全局参数),在web.xml中配置上下文参数:

admin

Obama

eg:

[html] view plain copy
  1. <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
  2. >  
  3. <html>  
  4. <head>  
  5. <meta charset="utf-8"/>  
  6. <title>EL访问全局参数(上下文参数)title>  
  7. head>  
  8. <body>  
  9.    <h3>全局参数为ctx的值是:${initParam.ctx}h3>  
  10. body>  
  11. html>  

用途3:使用ELparamValues内置对象访问一组参数

           ${paramValues.参数名[0]}访问一组参数值

           ${paramValues.参数名[n]}访问n+1组参数值

eg:

[html] view plain copy
  1. <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
  2. >  
  3. <html>  
  4. <head>  
  5. <meta charset="utf-8"/>  
  6. <title>兴趣爱好页面title>  
  7. head>  
  8. <body>  
  9.    <form action="paramValues.jsp" method="post">  
  10.             运动:   <input type="checkbox" name="hobby" value="sport"/>  <br/>  
  11.             电影:  <input type="checkbox" name="hobby" value="movie"/>  <br/>  
  12.             读书:  <input type="checkbox" name="hobby" value="reading"/>  <br/>  
  13.             音乐:  <input type="checkbox" name="hobby" value="music"/>  <br/>  
  14.             <input type="submit" value="提交"/>  
  15.    form>  
  16. body>  
  17. html>  

[html] view plain copy
  1. <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
  2. >  
  3. <html>  
  4. <head>  
  5. <meta charset="utf-8"/>  
  6. <title>EL访问一组参数title>  
  7. head>  
  8. <body>  
  9.    <h3>爱好是:h3>  
  10.    <h3> ${paramValues.hobby[0]} h3>  
  11.    <h3> ${paramValues.hobby[1]} h3>  
  12.    <h3> ${paramValues.hobby[2]} h3>  
  13.    <h3> ${paramValues.hobby[3]} h3>  
  14. body>  
  15. html>  

4. 访问cookie

通过ELcookie内置对象访问JSESSIONID名称的cookie的语法:

${cookie[“JSESSIONID”].name},${cookie[“JSESSIONID”].value}

[html] view plain copy
  1. <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
  2. >  
  3. <html>  
  4. <head>  
  5. <meta charset="utf-8"/>  
  6. <title>EL的cookie内置对象访问Cookietitle>  
  7. head>  
  8. <body>  
  9.    <h3>Cookie名为JSESSIONID的值为: ${cookie["JSESSIONID"].value}h3>  
  10.    <h3>sessionID为:${pageContext.session.id}h3>  
  11. body>  
  12. html>  

5. 访问header

${header[“cookie”] }

 EL的运算符

1. 算数运算符
      + - * /(div)  %(mod)
      eg: ${param.num1+param.num2}   参数相加


 2. 关系运算符
       < (lt)  > (gt)  == (eq)  !=(ne)  >=(ge) <=(le)


 3. 逻辑运算符

       &&(and)   ||(o r )  !(not)

4. empty运算符
   ${empty 表达式}  判断是否为null或空字符串””


5. 三目运算符
       ${返回true或false的表达式 ? "为true时输出的内容":"为false时输出的内容"}


6. 括号运算符
       ()用来改变运算顺序的


eg:

[html] view plain copy
  1. <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>  
  2. >  
  3. <html>  
  4. <head>  
  5. <meta charset="utf-8"/>  
  6. <title>EL运算符title>  
  7. head>  
  8. <body>  
  9.    <h3>num1+num2= ${param.num1+param.num2}h3>  
  10.    <h3>num1/num2= ${param.num1 / param.num2} 也可以div ${param.num1 div param.num2}h3>  
  11.    <h3>num1是否等于num2: ${param.num1 == param.num2}h3>  
  12.    <h3>true && true: ${true  && true}h3>  
  13.    <h3>true || false: ${true  || false}h3>  
  14.    <h3>${empty param.abc}h3>  
  15.    <h3>三目运算符:${param.num1>param.num2 ? "num1真的大于num2":"num1不大于num2" }h3>  
  16. body>  
  17. 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文件。

  

 JSP页面使用taglib指令引入xxx.tld文件

  <%@ taglib uri="指向xxx.tld文件的路径"     prefix="JSTL标签前缀"%>
注意:uri中指向xxx.tld文件的路径有两种写法:
          第一种:直接指向xxx.tld文件的路径
第二种:在web.xml中配置taglib的uri
       
                 
                       http://www.xxx.com
                       /WEB-INF/c.tld
                   
         
1.输出标签
   
2.设置标签
      设置属性范围的属性值
     

设置对象的属性值
   
3.捕获异常标签
     
           // 有可能发生异常的代码

     

4.判断标签
     
            // 判断结果为true,执行此处
     
5.forEach标签(遍历list集合或Map集合)
     
            ${msg}

     

6.choose标签
     
          =90}">
               

优秀


         
          =80}">
               

良好


         
         
               

不及格,要加油!


         
     

开发公司推荐

成为一品威客服务商,百万订单等您来有奖注册中

留言( 展开评论

快速发任务

价格是多少?怎样找到合适的人才?

官方顾问免费为您解答

 
相关任务
DESIGN TASK 更多
桌面应用程序开发

¥50000 已有0人投标

招商引资APP开发

¥20000 已有0人投标

APP原生开发

¥20000 已有1人投标

开发ai拍照软件

¥100 已有2人投标