码迷,mamicode.com
首页 > 编程语言 > 详细

javabean组件

时间:2018-09-20 11:04:09      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:实例化   nal   rop   pen   integer   type   mod   技术   char   

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了!

javabean组件

标签:实例化   nal   rop   pen   integer   type   mod   技术   char   

原文地址:https://www.cnblogs.com/zyxsblogs/p/9656829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!