zoukankan      html  css  js  c++  java
  • javabean组件

    javaBean组件引入:

    javaBean是使用java语言开发的一个可重用的组件,在Jsp开发中可以使用javaBean减少重复代码,使整个JSP代码的开发更简洁。

    我们首先创建一个类叫做Student 她有两个属性,age(年龄)    name(姓名)

    代码如下:

     1 package com.java1234.model;
     2 public class Student {
     3    private int age;
     4    private String name;
     5 public int getAge() {
     6     return age;
     7 }
     8 public void setAge(int age) {
     9     this.age = age;
    10 }
    11 public String getName() {
    12     return name;
    13 }
    14 public void setName(String name) {
    15     this.name = name;
    16 } 
    17 }
    Student.java

    若使用简单的取变量使用变量是这样的:

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <%@ page import="com.java1234.model.*" %>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     8 <title>javaBean</title>
     9 </head>
    10 <body>
    11     <%
    12         Student student =new Student();
    13         student.setName("王二小");
    14         student.setAge(12);
    15     %>
    16     <h1>姓名:<%=student.getName() %></h1>
    17     <h1>年龄:<%=student.getAge() %></h1>
    18 </body>
    19 </html>
    Javabean01.jsp

    如果我们使用jsp:useBean创建javabean(我们会发现大大简化了代码的数量)

    不需要<%@page import="com.java1234.model.Student" >

    jsp:useBean创建javabean:

    <jsp;useBean id=实例化对象的名称scope=“保存范围”class=类完整名称/>

    Scope,一共有page(页面),request(请求),session(会话)和application4个属性范围,默认是page;

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>javaBean</title>
     8 </head>
     9 <body>
    10     <jsp:useBean id="student" scope="page" class="com.java1234.model.Student"/>
    11     <% 
    12     student.setAge(18);
    13     student.setName("王二小");
    14     %>
    15     <h1>姓名:<%=student.getName() %></h1>
    16     <h1>年龄<%=student.getAge() %></h1>
    17 </body>
    18 </html>
    javabean02.jsp

    jsp:setProperty设置javabean属性值:

    <jsp:setProperty property=”属性名称”name=”实例化对象的名称”value=”属性值”param=”参数名称”/>

    Property=”*”自动匹配所有。

    如果我们不使用setProperty(创建一个student.jsp form表单提交界面):

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>javaBean</title>
     8 </head>
     9 <body>
    10     <form action="javaBean03.jsp" method="post">
    11         <table>
    12             <tr>
    13                 <td>姓名</td>
    14                 <td><input type="text" id="name" name="name"/></td>
    15             </tr>
    16             <tr>
    17                 <td>年龄:</td>
    18                 <td><input type="text" id="age" name="age"/></td>
    19             </tr>
    20             <tr>
    21                 <td colspan="2"><input type="submit" value="提交"></td>
    22             </tr>
    23         </table>
    24     
    25     </form>
    26 </body>
    27 </html>
    student.jsp

    此时javabean03.jsp文件request.getParameter()方法获取name 和 age :

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <%@ page import="com.java1234.model.Student"  %>
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     8 <title>Setproperty</title>
     9 </head>
    10 <body>
    11     <%
    12     request.setCharacterEncoding("utf-8");
    13     String name=request.getParameter("name");
    14     String age=request.getParameter("age");
    15     Student student=new Student();
    16     
    17     student.setAge(Integer.parseInt(age));
    18     student.setName(name);
    19     %>
    20     <h1>姓名:<%=student.getName() %></h1>
    21     <h1>年龄:<%=student.getAge() %></h1>
    22 </body>
    23 </html> 
    javaBean03.jsp

    采用jsp:setProperty进行设置:(此时的Property="*")

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>javabean03-1</title>
     8 </head>
     9 <body>
    10     <jsp:useBean id="student" scope="page" class="com.java1234.model.Student" />
    11     <jsp:setProperty property="*" name="student" />
    12     <h2>姓名:<%=student.getName() %></h2>
    13     <h2>年龄:<%=student.getAge() %></h2>
    14 </body>
    15 </html> 
    javaBean03-1

     现在使用Property="name" Property="age"重写为javaBean03-2.jsp。

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>javaBean03-1</title>
     8 </head>
     9 <body>
    10      <%
    11        request.setCharacterEncoding("utf-8");
    12      %>
    13     <jsp:useBean id="student" scope="page" class="com.java1234.model.Student" />
    14     <jsp:setProperty property="name" name="student" param="username"/>
    15     <jsp:setProperty property="age" name="student" value="100"/>
    16     <h2>姓&nbsp;名:<%=student.getName() %></h2>
    17     <h2>年&nbsp;龄:<%=student.getAge() %></h2>
    18 </body>
    19 </html> 
    javaBean03-2.jsp

    jsp:getProperty获取javabean属性值:

    <jsp:getProperty property=”属性名称” name=”实例化对象的名称”/>

    Student.java里面有age name属性。

    javaBean04.jsp代码:

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>javaBean</title>
     8 </head>
     9 <body>
    10     <jsp:useBean id="student" scope="page" class="com.java1234.model.Student"/>
    11     <% 
    12     student.setAge(18);
    13     student.setName("王二小2");
    14     %>
    15     <h1>姓名:<jsp:getProperty property="name" name="student"/></h1>
    16     <h1>年龄:<jsp:getProperty property="age" name="student" /></h1>
    17 </body>
    18 </html>
    javaBean04.jsp

    Javabean的保存范围:

    Javabean的保存范围有page,request,session,application.默认是page;

    page:

    在page中我们的上一个例子就是最好的说明,首先page是页面,也就是说在同一个页面存取。

    request:

    下面写一下在request中的存取,客户端内部跳转<jsp:forward  page="目标文件">

    我们把目标文件设置为target01.jsp

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>javaBean</title>
     8 </head>
     9 <body>
    10     <%
    11        request.setCharacterEncoding("utf-8");
    12     %>
    13     <jsp:useBean id="student" scope="request" class="com.java1234.model.Student"/>
    14     <jsp:setProperty property="age"  name="student" value="12" />
    15     <jsp:setProperty property="name" name="student" value="张狗蛋" />
    16     <jsp:forward page="target01.jsp"/>
    17 </body>
    18 </html>
    javaBean04-1.jsp

    target01.jsp

    目标文件:

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!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>javaBean03-1</title>
    </head>
    <body>
        <jsp:useBean id="student" scope="request" class="com.java1234.model.Student" />
        <h1>姓名:<jsp:getProperty property="name" name="student"/></h1>
        <h1>年龄:<jsp:getProperty property="age" name="student"/></h1>
    </body>
    </html>
    target01.jsp

    Session:(会话—只要浏览器不关闭就会一直存在)目标文件  target02.jsp

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>javaBean</title>
     8 </head>
     9 <body>
    10     <jsp:useBean id="student" scope="session" class="com.java1234.model.Student"/>
    11     <jsp:setProperty property="age"  name="student" value="12" />
    12     <jsp:setProperty property="name" name="student" value="张狗蛋" />
    13     <h1>session设置完毕</h1>
    14 </body>
    15 </html>
    javaBean04-2.jsp

    目标文件:

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>javaBean03-1</title>
     8 </head>
     9 <body>
    10 <h1>取到Session值</h1>
    11     <jsp:useBean id="student" scope="session" class="com.java1234.model.Student" />
    12     <h1>姓名:<jsp:getProperty property="name" name="student"/></h1>
    13     <h1>年龄:<jsp:getProperty property="age" name="student"/></h1>
    14 </body>
    15 </html>
    target02.jsp

    Application(很直观的换个浏览器都可以取到值)

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>javaBean</title>
     8 </head>
     9 <body>
    10     <jsp:useBean id="student" scope="application" class="com.java1234.model.Student"/>
    11     <jsp:setProperty property="age"  name="student" value="12" />
    12     <jsp:setProperty property="name" name="student" value="张狗蛋" />
    13     <h1>application设置完毕</h1>
    14 </body>
    15 </html>
    javaBean04-3.jsp

    目标文件:

    traget03,jsp(换个浏览器一样ok!)

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>javaBean03-1</title>
     8 </head>
     9 <body>
    10 <h1>取到application值</h1>
    11     <jsp:useBean id="student" scope="application" class="com.java1234.model.Student" />
    12     <h1>姓名:<jsp:getProperty property="name" name="student"/></h1>
    13     <h1>年龄:<jsp:getProperty property="age" name="student"/></h1>
    14 </body>
    15 </html>
    target03.jsp

    javabean删除:

    Page范围:pageConext.removeAttribute(“javaBean Name”);

    Request范围:request.removeAttribute(“javaBean Name”);

    Session范围:session.removeAttribue(“javaBean Name”);

    Application范围:application.removeAttribue(“javaBean Name”);

     举个例子:

    删除session中的值

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>javaBean</title>
     8 </head>
     9 <body>
    10     <%
    11     session.removeAttribute("student");
    12     %>
    13     <h1>session删除成功!</h1>
    14 </body>
    15 </html>
    javaBeanDelete.jsp

    ok了!

  • 相关阅读:
    [BZOJ1492] [NOI2007]货币兑换Cash 斜率优化+cdq/平衡树维护凸包
    [BZOJ2638] 黑白染色
    [BZOJ2006] [NOI2010]超级钢琴 主席树+贪心+优先队列
    [BZOJ3698] XWW的难题 网络流
    [BZOJ2151] 种树 贪心
    js中的闭包理解一
    HTML5 input placeholder 颜色修改示例
    26 个 jQuery使用技巧
    JS原型与原型链(好文看三遍)
    文字和图片垂直居中
  • 原文地址:https://www.cnblogs.com/zyxsblogs/p/9656829.html
Copyright © 2011-2022 走看看