JSP&EL&JSTL

1. JSP的三大指令

  语法:<%@ 指令的名称 属性名=”属性值” 属性名=”属性值”%> 

* page指令:
     * language        :JSP的语言的属性.现在只能是java.
     * contentType     :设置浏览器默认的打开的时候的编码.
     * pageEncoding    :设置JSP文件保存到硬盘,以及翻译成Servlet保存到硬盘的编码格式.
     * import          :设置JSP中引入其他的类.import属性可以出现多次的.
     * extends         :设置JSP被翻译成Servlet后继承的类:默认值:org.apache.jasper.runtime.HttpJspBase. 修改这个值得话一定要使这个类是一个Servlet才可以.
     * autoFlush       :设置自动刷出缓存
     * buffer          :JSP的缓冲区的大小。默认8kb
     * session         :默认值是true.设置JSP的页面中是否可以直接使用session对象.
     * isELIgnored     :设置JSP是否忽略EL表达式.默认值false 不忽略.可以在JSP中写EL表达式.
     * isErrorPage     :设置JSP页面中出现错误信息应该如何处理的.
     * errorPage       :设置JSP页面中出现错误信息应该如何处理的.
* 设置全局错误页面:
  <error-page>
      <error-code>404</error-code>
      <location>/404.jsp</location>
  </error-page>
  <error-page>
      <error-code>500</error-code>
      <location>/500.jsp</location>
  </error-page>
* include指令:用于引入其他的页面
    * file属性:属性中不能使用变量,不能传递参数
      <%@ include file="logo.jsp" %>
      <%@ include file="menu.jsp" %>
      <h1>Body</h1>
      <%@ include file="footer.jsp" %>
* taglib指令:用于在JSP中引入标签库
  <%@ taglib uri="" prefix="" %>

 2. JSP的9个内置对象

对象                           类型                                      方法
* request HttpServletRequest getParameter(),setAttribute(),getAttribute(); * response HttpServletResponse setStatus(),sendRedirect(),getOutputStream(); * session HttpSession setAttribute(),getAttribute(),removeAttribute(); * application ServletContext setAttribute(),getAttribute(),removeAttribute(); * page Object toString(),wait(),* pageContext PageContext setAttribute(),getAttribute(),removeAttribute(); * out JspWriter write(),print(), * config ServletConfig getInitParamter(),getInitParameterNames(), * exception Throwable getMessage(),getCause()
* out对象:先将自身缓存区中的内容输出到response的缓冲区中,由response向页面作出响应.
     * out                    :JspWriter
     * response.getWriter()   :PrintWriter
* pageContext:
     * 一类:用pageContext对象获得其他的8个内置对象. 用来编写一个通用性高的或者框架的时候.
     * 二类:用来存取数据.向四个域中存取数据.

3. JSP的四个域对象

* pageContext            pageScope                页面范围            
* request                requestScope             请求范围
* session                sessionScope             会话范围
* application            applicationScope         应用范围

4. JSP动作标签

  语法:<jsp:动作标签  属性=" "/>

<jsp:forward page="/demo1/demo11.jsp"></jsp:forward>

  静态包含和动态包含的区别:  

静态包含相对于代码的copy,最终被翻译成一个Servlet解释执行的.动态包含,包含其他页面的运行的结果.最终翻译成多个Servlet解释执行的.
<jsp:param />
    * 传递参数.
<jsp:useBean />
<jsp:setProperty />
<jsp:getProperty />

5. EL表达式

  语法:${EL 表达式}

  用途:(1)获取数据  

* 获得普通的值:
* 获得数组的数据:
* 获得List集合的数据:
* 获得Map集合的数据:

  []和.有什么区别?

  []获得带有下标(list、数组);.获得一个对象中的属性,如果该对象中的属性名包含特殊字符.必须使用[],而不使用.

  案列演示:

<h1>EL获取普通值</h1>
<%
    pageContext.setAttribute("pname", "赵");
    request.setAttribute("rname", "钱");
    session.setAttribute("sname", "孙");
    application.setAttribute("aname", "李");
%>
<h3>传统方式</h3>
<%= pageContext.getAttribute("pname")%>
<%= request.getAttribute("rname")%>
<%= session.getAttribute("sname")%>
<%= application.getAttribute("aname")%>
<h3>EL的方式</h3>
${ pageScope.pname }   
${ requestScope.rname }
${ sessionScope.sname }
${ applicationScope.aname }
<hr/>
<h3>EL获得数组的数据</h3>
<%
    String[] arrs = {"赵","钱","孙","李"};
    pageContext.setAttribute("arrs", arrs);
%>
${ arrs[1] }
<h3>EL获得List集合的数据</h3>
<%
    List<String> list = new ArrayList<String>();
    list.add("aaa");
    list.add("bbb");
    list.add("ccc");
    pageContext.setAttribute("list", list);
%>
${ list[1] }
<h3>获得Map集合的数据</h3>
<%
    Map<String,String> map = new HashMap<String,String>();
    map.put("aaa", "111");
    map.put("bbb", "222");
    map.put("ccc", "333");
    pageContext.setAttribute("map", map);
%>
${ map["ccc"] }
<h3>EL获得JavaBean中的数据</h3>
<%
    Person person = new Person();
    person.setId(1);
    person.setName("王美丽");
    pageContext.setAttribute("person", person);
%>
${ person.name } 

  (2)EL执行运算 

  (3)获取常用web中开发对象  

EL常用的对象:11个
四个域对象 ${pageScope} ${requestScope} ${sessionScope} ${applicationScope} ${ param } :相当于request.getParameter(); ${ paramValues } :相当于request.getParameterValues(); ${ header } :获得请求头 一个key对应一个value ${ headerValues } :获得请求头 一个key对应多个value ${ initParam } :获得初始化参数 ${ cookie } :获得Cookie的信息 ${pageContext} :相当于pageContext对象.

   (4)调用java中的函数

6. JSTL的标签库

  五大标签:core、fmt、xml、sql、fn

  案列: 

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
常用的标签:
c:set
c:if
c:forEach
c:choose
    * c:when
    * c:otherwise
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
${ fn:length("HelloWorld") }
${ fn:toLowerCase("ABCDEFG") }
${ fn:toUpperCase("abcdefg") }
${ fn:contains("www.baidu.com","baidu") }

 

  

 

 

 

 

 

 

 

 

 

posted on 2018-10-30 10:02  岁月磨平青春的棱角  阅读(146)  评论(0)    收藏  举报

导航