javabean组件
Posted ZYXS
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
若使用简单的取变量使用变量是这样的:
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>
如果我们使用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>
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>
此时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>
采用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>
现在使用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>姓 名:<%=student.getName() %></h2> 17 <h2>年 龄:<%=student.getAge() %></h2> 18 </body> 19 </html>
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>
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>
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>
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>
目标文件:
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>
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>
目标文件:
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>
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>
ok了!
以上是关于javabean组件的主要内容,如果未能解决你的问题,请参考以下文章