zoukankan      html  css  js  c++  java
  • JSP userBean setProperty getProperty指令使用

    JSP userBean setProperty getProperty指令使用

    javaBean的属性取决于get/set方法,而不是真实的属性名称。

    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");
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>009index</title>
    </head>
    <body>
        <jsp:useBean id="person" class="com.stono.servlet.bean.Person">
            <jsp:setProperty property="name" name="person" value="stono" />
        </jsp:useBean>
        <jsp:getProperty property="name" name="person" />
    </body>
    </html>

    转换的java文件:

    /*
     * Generated by the Jasper component of Apache Tomcat
     * Version: Apache Tomcat/7.0.35
     * Generated at: 2015-10-20 04:32:26 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 index009_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");
    
          out.write("
    ");
          out.write("<!DOCTYPE html>
    ");
          out.write("<html>
    ");
          out.write("<head>
    ");
          out.write("<meta charset="UTF-8">
    ");
          out.write("<title>009index</title>
    ");
          out.write("</head>
    ");
          out.write("<body>
    ");
          out.write("	");
          com.stono.servlet.bean.Person person = null;
          person = (com.stono.servlet.bean.Person) _jspx_page_context.getAttribute("person", javax.servlet.jsp.PageContext.PAGE_SCOPE);
          if (person == null){
            person = new com.stono.servlet.bean.Person();
            _jspx_page_context.setAttribute("person", person, javax.servlet.jsp.PageContext.PAGE_SCOPE);
            out.write("
    ");
            out.write("		");
            org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("person"), "name", "stono", null, null, false);
            out.write('
    ');
            out.write('
    ');
            out.write('    ');
          }
          out.write('
    ');
          out.write('
    ');
          out.write('    ');
          out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((com.stono.servlet.bean.Person)_jspx_page_context.findAttribute("person")).getName())));
          out.write("
    ");
          out.write("	
    ");
          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);
        }
      }
    }

     JavaBean:

    package com.stono.servlet.bean;
    public class Person {
        // 对外的属性名称还是name,
        private String name2;
        public String getName() {
            return name2;
        }
        public void setName(String name) {
            this.name2 = name;
        }
    }

     jsp:userBean的使用:

    <%@ 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");
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>009index</title>
    </head>
    <body>
        <p>
            jsp:useBean id="person" class="com.stono.servlet.bean.Person",如果Person是抽象类,会有异常:
            <pre>    
                严重: Servlet.service() for servlet [jsp] in context with path [/TestBootWeb] threw exception [/index013.jsp (line: 14, column: 1) The value for the useBean class attribute com.stono.servlet.bean.Person is invalid.] with root cause
                org.apache.jasper.JasperException: /index013.jsp (line: 14, column: 1) The value for the useBean class attribute com.stono.servlet.bean.Person is invalid.
            </pre>
        </p>
        <p>
            对于抽象类需要使用jsp:useBean id="person" type="com.stono.servlet.bean.Person" class="com.stono.servlet.bean.Employee"进行使用。
        </p>
        <jsp:useBean id="person" type="com.stono.servlet.bean.Person" class="com.stono.servlet.bean.Employee">
            <jsp:setProperty property="name" name="person" value="stono" />
        </jsp:useBean>
        <jsp:getProperty property="name" name="person" />
        <p>
            直接输入jsp:useBean id="person2" type="com.stono.servlet.bean.Person"会报错:
            <pre>
                严重: Servlet.service() for servlet [jsp] in context with path [/TestBootWeb] threw exception [javax.servlet.ServletException: java.lang.InstantiationException: bean person2 not found within scope] with root cause
                java.lang.InstantiationException: bean person2 not found within scope        
            </pre>
            输入jsp:useBean id="person" type="com.stono.servlet.bean.Person"也是有问题的:
            <pre>
                严重: Servlet.service() for servlet [jsp] in context with path [/TestBootWeb] threw exception [/index013.jsp (line: 35, column: 1) useBean: Duplicate bean name: {0}] with root cause
                org.apache.jasper.JasperException: /index013.jsp (line: 35, column: 1) useBean: Duplicate bean name: {0}        
            </pre>
        </p>
        <p>
            在servlet中进行request.setAttribute("person3",person3),但是在jsp中直接写:jsp:useBean id="person3" type="com.stono.servlet.bean.Person"会报错:
            <pre>
                严重: Servlet.service() for servlet jsp threw exception
                java.lang.InstantiationException: bean person3 not found within scope        
            </pre>
            因为默认的scope是page;修改为scope="request",就可以读取到request传递过来的值;
        </p>
        <p>
            如果希望使用jsp:useBean id="person3" type="com.stono.servlet.bean.Person",还不能在scriplet中使用名称为person3的变量,需要如下定义使用:
            <pre>
                &lt;%@ page import="com.stono.servlet.bean.*" %&gt;
                &lt;%
                    Person person33 = new Employee();
                    person33.setName("stono3inJsp");
                    pageContext.setAttribute("person3", person33);
                %&gt
                &lt;jsp:useBean id="person3" type="com.stono.servlet.bean.Person"&gt&lt;/jsp:useBean&gt
                &lt;jsp:getProperty property="name" name="person3"/&gt
            </pre>
        </p>
    </body>
    </html>
  • 相关阅读:
    取消PHPCMS V9后台新版本升级提示信息
    phpcmsv9全站搜索,不限模型
    jq瀑布流代码
    phpcms v9模版调用代码
    angular.js添加自定义服务依赖项方法
    angular多页面切换传递参数
    angular路由最基本的实例---简单易懂
    作用域事件传播
    利用angular控制元素的显示和隐藏
    利用angular给节点添加样式
  • 原文地址:https://www.cnblogs.com/stono/p/4894445.html
Copyright © 2011-2022 走看看