• JSP EL表达式使用


    JSP EL表达式使用:

    Servlet:

    package com.stono.servlet;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import com.stono.servlet.bean.Dog;
    import com.stono.servlet.bean.Person;
    import com.stono.servlet.bean.Toy;
    public class TestServlet6 extends HttpServlet {
        private static final long serialVersionUID = 1L;
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Person person = new Person();
            Dog dog = new Dog();
            Toy toy = new Toy();
            toy.setName("toyName");
            dog.setName("dogName");
            dog.setToy(toy);
            person.setDog(dog);
            person.setName("personName");
            req.setAttribute("person", person);
            String[] musics = new String[] { "m1", "m2", "m3" };
            req.setAttribute("musics", musics);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("a", "ma");
            map.put("b", "mb");
            map.put("c", "mc");
            req.setAttribute("map", map);
            Map<String, Object> map2 = new HashMap<String, Object>();
            map2.put("a", "m2a");
            req.setAttribute("map2", map2);
            req.setAttribute("aref", "a");
            RequestDispatcher dispatcher = req.getRequestDispatcher("index016.jsp");
            dispatcher.forward(req, resp);
        }
    }

    JSP:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
        pageContext.setAttribute("pca", "pageContextAttribute");
        //session.setAttribute("s", "session");
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>016</title>
    </head>
    <body>
        <p>JSP生成的java文件中,EL表达式使用统一的方式进行数据读取;</p>
        <p>EL result is : ${pageScope.pca }</p>
        <p>person.name is : ${person.name }</p>
        <p>person.dog.name is : ${person.dog.name }</p>
        <p>musics[0] is : ${musics[0]}</p>
        <p>musics["1"] is : ${musics["1"]}</p>
        <p>musics["2"] is : ${musics[1+1]}</p>
        <p>map[a] is : ${map[a] }</p>
        <p>map["a"] is : ${map["a"] }</p>
        <p>map.a is : ${map.a }</p>
        <p>[]中没有引号,就会计算括号中的内容:</p>
        <p>map2[aref] is : ${map2[aref] }</p>
        <p>map2["aref"] is : ${map2["aref"] }</p>
        <p>person.dog.toy.name is : ${person.dog.toy.name}</p>
        <p>XXX is : ${XXX }</p>
        <p>
            ${pageScope }
            ${requestScope }
            ${sessionScope }
            ${applicationScope }
            ${param }
            ${paramValues }
            ${header }
            ${headerValues }
            ${cookie }
            ${initParam }
            ${pageContext }
        </p>
    </body>
    </html>

    JSP转换生成的Java:

    /*
     * Generated by the Jasper component of Apache Tomcat
     * Version: Apache Tomcat/7.0.35
     * Generated at: 2015-10-30 00:31:49 UTC
     * Note: The last modified time of this file was set to
     *       the last modified time of the source file after
     *       generation to assist with modification tracking.
     */
    package org.apache.jsp;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;
    import java.util.*;
    
    public final class index016_jsp extends org.apache.jasper.runtime.HttpJspBase
        implements org.apache.jasper.runtime.JspSourceDependent {
    
      private static final javax.servlet.jsp.JspFactory _jspxFactory =
              javax.servlet.jsp.JspFactory.getDefaultFactory();
    
      private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
    
      private javax.el.ExpressionFactory _el_expressionfactory;
      private org.apache.tomcat.InstanceManager _jsp_instancemanager;
    
      public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
        return _jspx_dependants;
      }
    
      public void _jspInit() {
        _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
        _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
      }
    
      public void _jspDestroy() {
      }
    
      public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
            throws java.io.IOException, javax.servlet.ServletException {
    
        final javax.servlet.jsp.PageContext pageContext;
        javax.servlet.http.HttpSession session = null;
        final javax.servlet.ServletContext application;
        final javax.servlet.ServletConfig config;
        javax.servlet.jsp.JspWriter out = null;
        final java.lang.Object page = this;
        javax.servlet.jsp.JspWriter _jspx_out = null;
        javax.servlet.jsp.PageContext _jspx_page_context = null;
    
    
        try {
          response.setContentType("text/html;charset=UTF-8");
          pageContext = _jspxFactory.getPageContext(this, request, response,
                      null, true, 8192, true);
          _jspx_page_context = pageContext;
          application = pageContext.getServletContext();
          config = pageContext.getServletConfig();
          session = pageContext.getSession();
          out = pageContext.getOut();
          _jspx_out = out;
    
          out.write('
    ');
          out.write('
    ');
    
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
        pageContext.setAttribute("pca", "pageContextAttribute");
        //session.setAttribute("s", "session");
    
          out.write("
    ");
          out.write("<!DOCTYPE html>
    ");
          out.write("<html>
    ");
          out.write("<head>
    ");
          out.write("<meta charset="UTF-8">
    ");
          out.write("<title>016</title>
    ");
          out.write("</head>
    ");
          out.write("<body>
    ");
          out.write("	<p>EL result is : ");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${pageScope.pca }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("</p>
    ");
          out.write("	<p>person.name is : ");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${person.name }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("</p>
    ");
          out.write("	<p>person.dog.name is : ");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${person.dog.name }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("</p>
    ");
          out.write("	<p>musics[0] is : ");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${musics[0]}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("</p>
    ");
          out.write("	<p>musics["1"] is : ");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${musics["1"]}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("</p>
    ");
          out.write("	<p>musics["2"] is : ");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${musics[1+1]}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("</p>
    ");
          out.write("	<p>map[a] is : ");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${map[a] }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("</p>
    ");
          out.write("	<p>map["a"] is : ");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${map["a"] }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("</p>
    ");
          out.write("	<p>map.a is : ");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${map.a }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("</p>
    ");
          out.write("	<p>[]中没有引号,就会计算括号中的内容:</p>
    ");
          out.write("	<p>map2[aref] is : ");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${map2[aref] }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("</p>
    ");
          out.write("	<p>map2["aref"] is : ");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${map2["aref"] }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("</p>
    ");
          out.write("	<p>person.dog.toy.name is : ");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${person.dog.toy.name}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("</p>
    ");
          out.write("	<p>
    ");
          out.write("		");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${pageScope }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("
    ");
          out.write("		");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${requestScope }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("
    ");
          out.write("		");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${sessionScope }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("
    ");
          out.write("		");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${applicationScope }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("
    ");
          out.write("		");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${param }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("
    ");
          out.write("		");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${paramValues }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("
    ");
          out.write("		");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${header }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("
    ");
          out.write("		");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${headerValues }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("
    ");
          out.write("		");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${cookie }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("
    ");
          out.write("		");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${initParam }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("
    ");
          out.write("		");
          out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${pageContext }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
          out.write("
    ");
          out.write("	</p>
    ");
          out.write("</body>
    ");
          out.write("</html>
    ");
        } catch (java.lang.Throwable t) {
          if (!(t instanceof javax.servlet.jsp.SkipPageException)){
            out = _jspx_out;
            if (out != null && out.getBufferSize() != 0)
              try { out.clearBuffer(); } catch (java.io.IOException e) {}
            if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
            else throw new ServletException(t);
          }
        } finally {
          _jspxFactory.releasePageContext(_jspx_page_context);
        }
      }
    }
  • 相关阅读:
    css之布局
    css之浮动
    白扯之聊聊我们的情怀
    Vue之指令
    Vue之vue.js声明式渲染
    AJAX经常遇到的那些问题
    HTTP之cookie技术
    正则表达式资料
    require.js资料
    AMD 和 CMD 的区别
  • 原文地址:https://www.cnblogs.com/stono/p/4922333.html
Copyright © 2020-2023  润新知