zoukankan      html  css  js  c++  java
  • JSP(8)—EL案例和JSTL案例

    1.EL案例
    el.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import="com.test.bean.Customer" %>    
    <%@ page import="java.lang.reflect.Method;" %>    
    
    <!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>Insert title here</title>
    </head>
    <body>
    
    <!-- 一、使用JavaBean为属性赋值,属性的有效范围为session-->
        <jsp:useBean id="customer1" class="com.test.bean.Customer" scope="session"></jsp:useBean>
        <jsp:setProperty property="username" name="customer1" value="root"/>
    
        <!-- 获取设置的属性值的三种方式 -->
    
        <!-- 1.使用Java代码 -->
        <%
            Customer customer2 = (Customer)session.getAttribute("customer1");
            out.println("1.username:");
            out.println(customer2.getUsername());
        %>
        <br><br>
        <!-- 2.使用JavaBean的getProperty()方法 -->
        2.username:<jsp:getProperty property="username" name="customer1"/>
    
        <br><br>
        <!-- 3.使用EL表达式 -->
        3.username:${sessionScope.customer1.username }
        <br><br>
    
    
    <!-- 二、获取请求参数的值 ${param.username }-->
        <form action="el.jsp" method="post">
    
            ①回显——使用request对象    
            usernName:<input type="text" name="username" 
            value="<%=request.getParameter("username") == null ? "" : request.getParameter("username")%>">
    
            <br><br>②回显——使用EL表达式:
            usernName:<input type="text" name="username" 
            value="${param.username }">
    
            <input type="submit" value="Submit">
    
            <br><br>③使用request对象:
            userName:<%=request.getParameter("username") %>
        </form>
        <br><br>
    
    <!-- 三、跨页面传递参数 -->
        <a href="el2.jsp?age=26&name=a&name=b&name=c">To El2 Page</a>
        <br><br>
    
    
    <!-- 四、存储数据范围 -->
    
        <%
            Customer customer = new Customer();
            customer.setUsername("Ann");
            request.setAttribute("customer1", customer);
        %>
    
    
        获取默认范围内的username的值:${customer1.username }  
        <br><br>
        获取session范围内的username的值:${sessionScope.customer1.username }
    
    </body>
    </html>

    el2.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import="com.test.bean.Customer" %>
    <!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>Insert title here</title>
    </head>
    <body>
    
    
    
    <!-- 五、自动类型变换 -->
    
        age:${param.age }
        <br><br>
        age:<%=request.getParameter("age")%>
        <br><br>
        对数据进行加减操作之后:
        <br><br>
        age:${param.age + 10}
        <br><br>
        age:<%=request.getParameter("age") + 10%>
        <br><br>
    
    <!-- 六、跨页面获取属性值 -->
    
        <!-- 获取设置的属性值的三种方式 -->
    
        <!-- 1.使用Java代码 -->
        <%
            Customer customer3 = (Customer)session.getAttribute("customer1");
                out.println("1.username:");
                out.println(customer3.getUsername());
        %>
        <br><br>
        <!-- 2.使用JavaBean的getProperty()方法 -->
        <jsp:useBean id="customer1" class="com.test.bean.Customer" scope="session"></jsp:useBean>
        2.username:<jsp:getProperty property="username" name="customer1"/>
    
        <br><br>
        <!-- 3.使用EL表达式 -->
        3.username:${sessionScope.customer1.username }
        <br><br>
    
    <!-- 七、隐含对象 --> 
    
        <!-- 1、与范围相关的隐含对象pageScope、requestScope、sessionScope、applicationScope -->
        <%
            application.setAttribute("username", "appName");
            session.setAttribute("username", "sesName");
            request.setAttribute("username", "reqName");
            pageContext.setAttribute("username", "pageName");
        %>
        <br><br>
        pageScope——username:${pageScope.username }
        <br><br>
        requestScope——username:${requestScope.username }
        <br><br>
        sessionScope——username:${sessionScope.username }
        <br><br>
        applicationScope——username:${applicationScope.username }
        <br><br>
    
        <!-- 2.与输入有关的隐含对象Param、ParamValues,与五中的自动类型变换获取属性的方式一样 -->
        Age(param):${param.age }
        <br><br>
        Names(paramValues):${paramValues.name }
        <br><br>
        Names(paramValues):${paramValues.name[0] }
        <br><br>
    
        <!-- 3.其他隐含对象   cookie、header、headerValues、initParam、pageContext -->
        cookie:${cookie.JSESSIONID.name }——${cookie.JSESSIONID.value }
        <br><br>
        header:${header["Accept-Language"] }
        <br><br>
        initParam:${initParam.user }   
        <br><br>
        pageContext即为PageContext类型,但只能读取属性。
        pageContext读取contextPath:${pageContext.request.contextPath }
        <br><br>
        sessionid:${pageContext.session.id }
        <br><br>
    
    <!-- 八、运算符 -->
        年龄:${param.age },是${param.age > 18 ? "成年人" : "未成年人" }
        <br><br>
    
    
        <form action="ElFunction.jsp">
            <input type="text" name="name" value="test">
            <input type="submit" value="To ElFunction Page">
        </form> 
    
    </body>
    </html>

    ELFunction.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    <%@ taglib prefix="test" uri="http://www.Test.com/mytag/core" %>
    
    <!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>Insert title here</title>
    </head>
    <body>
    
        <h3>EL表达式函数</h3>
        <!-- 1.使用预定义函数 -->
        参数长度:${fn:length(param.name)}
        <br><br>
        大小写转换:${fn:toUpperCase(param.name) }
    
        <!-- 2.自定义函数 -->
        <br><br>
        两数相加,和为:${test:concat("10", "10") }
    </body>
    </html>

    2.JSTL案例

    jstl.jsp

    <%@page import="java.util.HashMap"%>
    <%@page import="java.util.Map"%>
    <%@page import="com.test.bean.Student"%>
    <%@page import="com.test.bean.Customer"%>
    <%@page import="java.util.List"%>
    <%@page import="java.util.ArrayList"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!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>Insert title here</title>
    </head>
    <body>
    
        <%
            request.setAttribute("book", "<<Java>>");
            Customer customer = new Customer();
            customer.setUsername("test");
            request.setAttribute("cus", customer);
        %>
        <form action="jstl.jsp">
            <input type="text" name="name" value="test">
            <input type="text" name="age" value="23">
            <input type="submit" value="Sunbmit">
        </form>
        <h3>jstl核心标签库,表达式操作</h3>
    
    <!-- 1.out -->
        book:${requestScope.book }
        <br><br>
        book:<c:out value="${requestScope.book}"></c:out>
        <br><br>
    
    <!-- 2.set -->
        <c:set var="name" value="test" scope="page"></c:set>
        <c:set var="name" value="test" scope="request"></c:set>
        <c:set var="name" value="${param.name }" scope="session"></c:set>
        name1:${pageScope.name }
        <br><br>
        name2:${requestScope.name }
        <br><br>
        name3:${sessionScope.name }
        <br><br>
        <!-- 把请求参数赋给javaBean的属性 -->
        name4:${requestScope.cus.username }
        <br><br>
        <!-- 方案一、使用java代码 -->
        <%
            String str = request.getParameter("name");
            request.setAttribute("username", str);
        %>
        <!-- 显示结果 -->
        name5:${requestScope.cus.username }
        <br><br>
        <!-- 方案二、使用jstl标签 -->
        <c:set target="${requestScope.cus }" property="username" value="${param.name }"></c:set> 
        <!-- 显示结果 -->
        name6:${requestScope.cus.username }
        <br><br>
    
    <!-- 3.remove -->   
        <c:remove var="name" scope="session"/>
        <!-- 显示结果 -->
        name3(移除属性name3的结果显示):${sessionScope.name }
        <br><br>
    
    
    
        <h3>jstl核心标签库,流程控制操作</h3>
    
    <!-- 4.if -->
        <c:set var="age" value="20" scope="page"></c:set>
        <c:if test="${pageScope.age > 18}">成年</c:if>
        <br><br>
        <c:if test="${pageScope.age > 18}" var="isAdult" scope="page"></c:if>
        isAudlt:<c:out value="${pageScope.isAdult}"></c:out>
        <br><br>
    
    <!-- 5.choose when otherwise -->
        <c:choose>
            <c:when test="${param.age > 60 }">老年</c:when>
            <c:when test="${param.age > 30 }">中年</c:when>
            <c:when test="${param.age > 18 }">青年</c:when>
            <c:when test="${param.age < 18 }">少年</c:when>
            <c:otherwise>其他...</c:otherwise>
        </c:choose>
        <br><br>
    
    
        <h3>jstl核心标签库,流程控制操作</h3>
    
    <!-- 6.c:forEach -->
        <!-- 1至10,每隔三个数输出 -->
        <c:forEach begin="1" end="10" step="3" var="i" >
                ${i }、
        </c:forEach>
        <br><br>
        <!-- 遍历集合数据 -->
        <%
            //List
            List<Student> students = new ArrayList<Student>();
            students.add(new Student(1,"AA"));
            students.add(new Student(2,"BB"));
            students.add(new Student(3,"CC"));
            students.add(new Student(4,"DD"));
            students.add(new Student(5,"EE"));
            request.setAttribute("stu", students);
    
            //Map
            Map<String, Student> studentMap = new HashMap<String, Student>();
            studentMap.put("a", new Student(1, "AAA"));
            studentMap.put("b", new Student(2, "BBB"));
            studentMap.put("c", new Student(3, "CCC"));
            studentMap.put("d", new Student(4, "DDD"));
            studentMap.put("e", new Student(5, "EEE"));
            request.setAttribute("stuMap", studentMap);
        %>
        <c:forEach items="${requestScope.stu }" var="student" varStatus="status">
            ${student.id }、${student.name }、${status.index }、${status.count }、${status.first }、${status.last }、<br>
        </c:forEach>
        <br><br>
        <c:forEach items="${requestScope.stu }" var="student" begin="1" step="2">
            ${student.id }、${student.name }<br>
        </c:forEach>
        <br><br>
        <c:forEach items="${requestScope.stuMap }" var="student">
            ${student.key }、${student.value.id }、${student.value.name }<br>
        </c:forEach>
        <br><br>
    
    <!-- 7.c:forToken -->
        <c:set value="a,b,c,d.e.f.g.h" var="test" scope="request"></c:set>
        <c:forTokens items="${requestScope.test }" delims="," var="str">
            ${str }<br>
        </c:forTokens>
        <br><br>
    
    
        <h3>jstl核心标签库,URL操作</h3>
    
    <!-- 8.c:import -->
    
        <br><br>
        <c:import url="http://www.baidu.com" charEncoding="UTF-8"></c:import>
        <br><br>
    
    <!-- 9.c:redirect,从当前页面重定向到指定页面 --> 
        <!-- 其中/代表的是web应用,所以该操作是服务器跳转,而Servlet中的重定向是客户端跳转 -->
        <%--
            <c:redirect url="test.jsp"></c:redirect> 
            <c:redirect url="/demo3_JSTL/test.jsp"></c:redirect>
        --%>
    
    <!-- 10.c:url,产生一个url地址 --> 
        <c:url value="/demo3_JSTL/test.jsp" var="varValue">
            <c:param name="name" value="Test你好!"></c:param>
        </c:url>
        <br><br>
        url:${varValue}
    
    
    
    
    
    </body>
    </html>

    test.jsp

    <body>
        <h3>重定向到该页面!!</h3>
    </body>

    MyElFunction.java

    package com.test.MyElFunction;
    /**
     * EL表达式自定义函数
     */
    public class MyElFunction {
    
        public static String concat(String str1, String str2){
            return str1 + str2;
        }
    }
  • 相关阅读:
    IDEA中设置自动build-改动代码,不用重启工程,刷新页面即可
    前端上传大文件并支持中途取消上传
    网页上传大文件并支持中途取消上传
    HTTP上传大文件并支持中途取消上传
    B/S上传大文件并支持中途取消上传
    如何将word公式粘贴到CKEditor里面
    如何将word公式粘贴到HTML编辑器里面
    如何将word图片粘贴到HTML编辑器里面
    如何将word图片粘贴到Web编辑器里面
    如何将word公式粘贴到Web编辑器里面
  • 原文地址:https://www.cnblogs.com/tengpengfei/p/10453976.html
Copyright © 2011-2022 走看看