zoukankan      html  css  js  c++  java
  • JSP入门实战下

    第一部分简单解说:jsp语法的规范,以及三大编译指令,七个动作指令和九大内置对象,生命周期解说等。

    这章主要解说el表达式,核心标签库。


    所有代码下载:链接

    1.核心标签库(JSTL:c)解说:

    1.1简要介绍:

    JSTL全名JspServer Pages Standdard Tag Library(Jsp标准标签库),它是sun公司公布的一个针对JSP开发的新组件,它同意使用标签开发Jsp页面.JSTL支持通用的、结构化的任务,比方迭代,条件推断,XML文档操作。国际化标签,SQL标签。 除了这些,它还提供了一个框架来使用集成JSTL的自己定义标签。
    JSTL所提供的标签库主要分为五大类:
    09

    1.2JSTL库安装:

    1. 从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip)。下载地址:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
    2. 将下载的压缩包解压。将lib下的两个jar文件:standard.jar和jstl.jar文件复制到Tomcat下lib/文件夹下。
    3. 如今就能够通过在头部包括标签使用JSTL了

    1.3核心标签库的使用:

    核心标签是最经常使用的JSTL标签。如今基本上我们也之使用功能核心标签库。此去仅仅介绍核心标签,对于其它的标签使用方法相似。
    1. 引用核心标签库的语法例如以下:

    <%--导入核心标签库 --%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" 
    1. 核心标签库的介绍:
      10
    2. 演演示样例如以下:具体见凝视
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"
        import="java.util.*,com.rlovep.entity.Student"
        %> 
    <%--导入核心标签库 --%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>核心标签jiangjie</title>
    </head>
    <body>
    <%--使用标签库 --%>
    <%--set标签:保存数据到域中,默认保存到pag域中
        var:属性名
        value:属性价值,能够是对象
        scope:范围
     --%>
    <c:set var="name" value="rose" scope="page"></c:set>
    <%--out标签: 相似输出表达式:<%= %>
        value:显示的内容;
        default:value为空时显示的内容
        escapexml:是否对<等实体符号转义。
    --%>
    <%--el表达式输出,调用属性 --%>
    <c:out value="${name }" default="<h3>标题3</h3>" escapeXml="true"></c:out>
    <%--默认值測试,以及输出特殊字符 --%>
    <c:out value="${peace }" default="<h3>标题</h3>" escapeXml="true"></c:out>
    <hr/>
    <%--remove标签:删除数据,默认删除到pag域中
        var:属性名
        scope:范围
     --%>
    <c:remove var="name" scope="page"/>
    <c:out value="${name }" default="删除name之后" escapeXml="true"></c:out>
    <hr/>
    <%--catch标签:能够用来取得错误发生时的信息,同一时候能够进行适当处理.相当于try catch
        var:保存错误信息的exception
     --%>
     <c:catch var="e">
     <%
       int a=0,b=10;
        b=b/a;
     %>
     </c:catch>
     <%--输出错误信息 --%>
     <c:out value="${e }"/>
    <%-- <%
       int a=0,b=10;
        b=b/a;
     %> --%>
     <hr/>
      <%--
        <c:url>标签将URL格式化为一个字符串,然后存储在一个变量中
        var:变量名。
        value:url
        context:本地的还有一个project库
      --%>
      <%--c:param 在重定向时当參数用 --%>
      <c:url var="url" value="el.jsp">
        <c:param name="pass" value="peace"/>
      </c:url>
      <a href="${url }">url重定向</a>
       <c:url var="baidu" value="http://wwww.baidu.com"/>
       <a href="${baidu }">百度</a>
    <hr/>
    <%--<c:import>标签:功能相似于<jsp:import>,可是功能更加强大。能够导入外部jsp文件。和保存到输入流中
        var:输出保存到string
        varReader:输出保存到输入字符流
        url:包括的页面
     --%>
     <c:import url="/common/header1.jsp" >
      <c:param name="name" value="sisi"/>
      </c:import>
      <hr/>
     <%--c:redirect 标签 能够是绝对地址 
        url:地址
        context:另外一个jsp容器
      --%>
    <%--  <c:redirect url="el.jsp">
       <c:param name="pass" value="wang"></c:param>
     </c:redirect> --%>
     <%
       Integer score=new Integer(60);
       pageContext.setAttribute("score", score);
     %>
       <%--if标签 :单条件推断
         test:推断是否为true运行
       --%>
        <c:if test="${!empty score}">
            条件成立
        </c:if>
        <hr/>
        <%--choose标签+when标签+otherwirse标签: 多条件推断 --%>
        <c:set var="score" value="56"></c:set>
            <c:choose>
            <c:when test="${score>=90 && score<=100}">
                优秀
            </c:when>
            <c:when test="${score>=80 && score<90}">
                良好
            </c:when>
            <c:when test="${score>=70 && score<80}">
                一般
            </c:when>
            <c:when test="${score>=60 && score<70}">
                及格
            </c:when>
            <c:otherwise>
                不及格
            </c:otherwise>
        </c:choose>
        <%-- forEach标签:循环 --%>
        <%
            //List
            List<Student>  list = new ArrayList<Student>();
            list.add(new Student("rose",18));
            list.add(new Student("jack",28));
            list.add(new Student("lucy",38));
            //放入域中
            pageContext.setAttribute("list",list);
                    //Map
            Map<String,Student> map = new HashMap<String,Student>();
            map.put("100",new Student("mark",20));
            map.put("101",new Student("maxwell",30));
            map.put("102",new Student("narci",40));
            //放入域中
            pageContext.setAttribute("map",map);
         %>
         <hr/>
         <%--
          begin="" : 从哪个元素開始遍历,从0開始.默认从0開始
          end="":     到哪个元素结束。

    默认到最后一个元素 step="" : 步长 (每次加几) ,默认1 items="": 须要遍历的数据(集合) var="": 每一个元素的名称 varStatus="": 当前正在遍历元素的状态对象。(count属性:当前位置,从1開始,last属性:最后一个) --%> <c:forEach items="${list}" var="student" varStatus="varSta"> 序号: {student.name } - 年龄:${student.age}<br/> </c:forEach> <hr/> <c:forEach items="${map}" var="entry"> {entry.value.name } - 年龄:${entry.value.age }<br/> </c:forEach> <hr/> <%-- forToken标签: 循环特殊字符串 --%> <% String str = "java-php-net-平面"; pageContext.setAttribute("str",str); %> <c:forTokens items="${str}" delims="-" var="s" varStatus="status"> ${s }<br/> <c:if test="${status.last }"> <c:out value="输出:${status.count}"/>个元素 </c:if> </c:forTokens> </body> </html>

    2.EL表达式语言:

    E L(Expression Language) 目的:为了使JSP写起来更加简单。

    EL 提供了在 JSP 脚本编制元素范围外使用运行时表达式的功能。
    EL既能够用来创建算术表达式也能够用来创建逻辑表达式。在JSP EL表达式内能够使用整型数。浮点数。字符串,常量true、false,还有null。
    EL使得訪问存储在JavaBean中的数据变得很easy,EL能够訪问内置对象。以及放置在对象中的属性。
    EL表达式作用: 向浏览器输出域对象中的变量值或表达式计算的结果!!

    2.1EL语法:${exper}

    1. 输出基本数据类型变量:
      不注明域的范围时,从四个域中获取:顺序为pageScoep / requestScope / sessionScope / applicationScope
      name<{pageScope.name} <%–等价于getAttribute()方法。–%>
    2. 输出对象的属性值
      ${student.name} 等价于 (点相对于调用getXX()方法)
    3. 使用EL获取集合对象

    {list[0].age } <%– list[0]等价于 (中括号相对于调用get(參数)方法) ((List)pageContext.findAttribute(“list”)).get(0)–%>

    1. el还能够运行算法表达式
      EL表达式支持大部分Java所提供的算术和逻辑操作符:
      11
      演演示样例如以下:
    比較运算
         ${10>5 }<br/>
         ${10<5 }<br/>
         ${10!=10 }
         <hr/>
    逻辑运算
         ${true && false }<br/>
         ${true || false }<br/>
         ${!false }<br/>
    判空
            null 或 空字符串:  empty
         <%
            //String name = "eric";
            //String name = null;
            String name = "";
            pageContext.setAttribute("name",name);
          %>
          推断null: ${name==null }<br/>
          推断空字符: ${name=="" }<br/>
         判空:  ${name==null || name=="" }
    还有一种判空写法: ${empty name }

    2.2EL高级使用方法自己定义函数:

    el表达语言的自己定义函数 本质是为了调用提供一种方法同意el中调用某类的静态方法:
    1. 自己定义函数使用语法:
    ${rlovep:reverse(student.name)}<%–调用reverse方法使传入的student.name反转–%>
    2. 开发步骤:
    1.在src建立开发处理类。这个类包括若干个静态方法。

    当然这个步骤能够省掉使用jdk库的类也是能够的
    2.使用标签库定义函数:定义函数的方式与定义标签的方式相似。添加function标签即可。
    3.使用:添加taglib指令
    3. 演演示样例如以下
    建立开发处理类: MyFuns.java

    public static String reverse(String str)
        {
            return new StringBuffer(str).reverse().toString();
        }
        public static int count(String str)
        {
            return str.length();
        }

    在webcontent文件夹下建立:mytaglib.tld标签库文件。添加function标签

    <?

    xml version="1.0" encoding="UTF-8" ?> <taglib 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-jsptaglibrary_2_1.xsd" version="2.1"> <description>A tag library exercising SimpleTag handlers.</description> <!-- 标签库的版本号 --> <tlib-version>1.0</tlib-version> <!-- 标签库前缀 --> <short-name>rlovep</short-name> <!-- tld文件的唯一标记 --> <uri>http://rlovep.com</uri> <!-- 定义第一个方法 --> <function> <!-- 定义方法名 --> <name>reverse</name> <!-- 定义方法的处理类 --> <function-class>com.rlovep.elmethod.MyFuns</function-class> <!-- 定义函数的实现方法:包括返回值和函数名以及參数 --> <function-signature>java.lang.String reverse(java.lang.String)</function-signature> </function> <!-- 定义第二个方法 --> <function> <!-- 定义方法名 --> <name>count</name> <!-- 定义方法的处理类 --> <function-class>com.rlovep.elmethod.MyFuns</function-class> <!-- 定义函数的实现方法:包括返回值和函数名以及參数 --> <function-signature>int count(java.lang.String)</function-signature> </function> </taglib>

    添加taglib指令
    <%@taglib prefix=”rlovep” uri=”http://rlovep.com” %>

    2.3总体演演示样例如以下:

    <%@page import="java.util.HashMap,java.util.Map"%>
    <%@page import="java.util.ArrayList"%>
    <%@page import="java.util.List"%>
    <%@page import="com.rlovep.entity.Student"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <!-- 定义标签 -->
    <%@taglib prefix="rlovep" uri="http://rlovep.com" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>el表达式学习:</title>
    </head>
    <body>
    <%--el的内置对象 --%>
     <%
        pageContext.setAttribute("name", "peace");
        pageContext.setAttribute("age", "22", pageContext.APPLICATION_SCOPE);
     %>
     <%--直接从域中搜索获得属性 --%>
     El表达式:${name }
     <hr/>
     <%--等价于 --%>
     表达式:<%=pageContext.findAttribute("name") %>
     <hr/>
     <%--从指定域中获取属性 --%>
     EL表达式:${applicationScope.age}
     <hr/>
     <%--等价于 --%>
     <%=pageContext.getAttribute("age", pageContext.APPLICATION_SCOPE) %>
     <hr/>
     <%--获取请求參数 --%>
     请求參数${param.pass}
     <hr/>
    <%--请求头获取 --%>
    请求头${header.Host}
    <%--还能够获得初始參数:initparam 以及cookie --%>
    <hr/>
    <%--el输出对象的属性 ,必须将对象放入域中--%>
    <%
        Student student=new Student("peace",22);
        String a="123";
        //放入域中
       pageContext.setAttribute("student", student);
        //放入list中
        List<Student> list=new ArrayList<Student>();
        list.add(new Student("sisi",22));
        list.add(new Student("nick",20));
        list.add(new Student("lucy",38));
        pageContext.setAttribute("list", list);
        //放入map中
        Map<String,Student> map=new HashMap<String,Student>();
        map.put("100",new Student("mark",20));
        map.put("101",new Student("maxwell",30));
        map.put("102",new Student("narci",40));
        //放入域中
        pageContext.setAttribute("map",map);
    %>
    <%--使用el获取对象值 --%>
    {student.age }
    
     <%--
           ${student.name} 等价于     (点相对于调用getXX()方法)
              <%=((Student)pageContext.findAttribute("student")).getName()%>
           --%>
           <hr/>
           <%--使用EL获取List对象 --%>
    {list[0].age }<br/>
    
    {list[1].age }<br/>
    {list[2].age }
    
           <%--
           list[0]等价于       (中括号相对于调用get(參数)方法)
                ((List)pageContext.findAttribute("list")).get(0)
            --%>
            <hr/>
            <%--使用EL获取Map对象 --%>       
    {map['100'].age }<br/>
    {map['101'].age }<br/>
    {map['102'].age }<br/>
    
    <%--el还能够运行算法表达式 --%>
    <%--el表达语言的自己定义函数 
    本质是为了调用提供一种方法同意el中调用某类的静态方法:
    1.在src建立开发处理类,这个类包括若干个静态方法。

    当然这个步骤能够省掉使用jdk库的类也是能够的 2.使用标签库定义函数:定义函数的方式与定义标签的方式相似。

    添加function标签即可。 3.添加taglib指令 --%> 此去表达式调用函数:<br/> peace倒转:${rlovep:reverse(student.name)}<%--调用reverse方法使传入的student.name反转--%> <br/> peace字符个数:${rlovep:count(student.name)} </body> </html>

    来自一条小鲨鱼wpeace(rlovep.com)

  • 相关阅读:
    RabbitMQ笔记-死信队列与延时队列
    设计模式-迭代器模式
    RabbitMQ笔记-Demo(C#)
    RabbitMQ笔记-消息追踪【未完成】
    RabbitMQ笔记-安装&命令
    RabbitMQ笔记-Exchange、Queue、Message详细说明
    MySQL笔记-MVCC【没写】
    MySQL笔记-基础知识
    多线程笔记-基础知识
    在Redis中进行分页排序查询【转】
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7388981.html
Copyright © 2011-2022 走看看