• JSP + JavaBean


    JavaBean是一种规范,可以理解。但实在想不出JSP中这样使用JavaBean有啥意义。算了,按照一个小例子做下,当熟悉JSP。

    Bean类:

    package com.my.beans;
    
    import java.lang.*;
    
    public class User {
        private String name;
        private String password;
        private int age;
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
    
        public User() {}
    }

    JSP:

    <%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
    <%@ page import="java.util.*"%>
    
    <html>
        <header>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        </header>
        <body>
            <jsp:useBean id="user" class="com.my.beans.User" scope="page" />
            <jsp:setProperty name="user" property="name" value="robin" />
            <jsp:setProperty name="user" property="password" value="123" />
            <jsp:setProperty name="user" property="age" value="18" />
            
            User name:<jsp:getProperty name="user" property="name" /><br/>
            Passoword:<jsp:getProperty name="user" property="password" /><br/>
            Age:<jsp:getProperty name="user" property="age" />
        </body>
    </html>

    生成的java servlet文件:

    /*
     * Generated by the Jasper component of Apache Tomcat
     * Version: Apache Tomcat/7.0.52
     * Generated at: 2014-03-28 09:16:40 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.pages;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;
    import java.util.*;
    
    public final class testbean_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("
    ");
          out.write("
    ");
          out.write("<html>
    ");
          out.write("	<header>
    ");
          out.write("		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    ");
          out.write("	</header>
    ");
          out.write("	<body>
    ");
          out.write("		");
          com.my.beans.User user = null;
          user = (com.my.beans.User) _jspx_page_context.getAttribute("user", javax.servlet.jsp.PageContext.PAGE_SCOPE);
          if (user == null){
            user = new com.my.beans.User();
            _jspx_page_context.setAttribute("user", user, javax.servlet.jsp.PageContext.PAGE_SCOPE);
          }
          out.write("
    ");
          out.write("		");
          org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("user"), "name", "robin", null, null, false);
          out.write("
    ");
          out.write("		");
          org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("user"), "password", "123", null, null, false);
          out.write("
    ");
          out.write("		");
          org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("user"), "age", "18", null, null, false);
          out.write("
    ");
          out.write("		
    ");
          out.write("		User name:");
          out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((com.my.beans.User)_jspx_page_context.findAttribute("user")).getName())));
          out.write("<br/>
    ");
          out.write("		Passoword:");
          out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((com.my.beans.User)_jspx_page_context.findAttribute("user")).getPassword())));
          out.write("<br/>
    ");
          out.write("		Age:");
          out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((com.my.beans.User)_jspx_page_context.findAttribute("user")).getAge())));
          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);
        }
      }
    }

    其实就是new一个object,然后调用set and get方法。

  • 相关阅读:
    进制转换问题
    奶牛野炊
    BFS
    深搜DFS
    Map容器之热血格斗场
    衣服尺码
    BZOJ2789 [Poi2012]Letters
    BZOJ1098 [POI2007]办公楼biu
    BZOJ1753 [Usaco2005 qua]Who's in the Middle
    BZOJ2442 [Usaco2011 Open]修剪草坪
  • 原文地址:https://www.cnblogs.com/HD/p/3631119.html
Copyright © 2020-2023  润新知