jsp:javaBean

15891 ワード

<!-- useBean  -->
<jsp:useBean id="person" class="com.wzh.test.Person" scope="session">
kk
</jsp:useBean>

<%=person.getName() %>

生成されたservletファイル:
 com.wzh.test.Person person = null;
      synchronized (session) {
        person = (com.wzh.test.Person) _jspx_page_context.getAttribute("person", javax.servlet.jsp.PageContext.SESSION_SCOPE);
        if (person == null){
          person = new com.wzh.test.Person();
          _jspx_page_context.setAttribute("person", person, javax.servlet.jsp.PageContext.SESSION_SCOPE);
          out.write("\r
"); out.write(" kk\r
"); out.write(" "); } }

 
 
 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title></title>

  </head>
  
  <body>
  
    <!-- useBean  -->
    <jsp:useBean id="person" class="com.wzh.test.Person" scope="session">
    kk
    </jsp:useBean>
  
    <!--  Bean  -->
      <jsp:setProperty property="name" name="person" value="xx"/>  
      <%=person.getName() %>
      
      <!--  Bean  -->
      <jsp:setProperty property="name" name="person" param="name"/>  
      <%=person.getName() %>
      <!-- http://127.0.0.1:8080/day09/1.jsp?name=123   123 -->
      
      <jsp:setProperty property="age" name="person" param="age"/>
      <!--  8  ( )-->  
      <%=person.getAge() %>
      
      <jsp:setProperty property="birthday" name="person" param="birthday"/>        
      <%=person.getBirthday() %>
      
      --------------------------
      <!--   -->
      <jsp:setProperty property="*" name="person"/>
      <%=person.getName() %>
      <%=person.getAge() %>
      
      </br>
      <jsp:getProperty property="name" name="person"/>
  </body>
</html>

 
package com.wzh.test;

import java.util.Date;

public class Person {

    private String name="aaa";
    private int age;
    private Date birthday;
    public String getName() {
        return name;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}