暑假学习计划:Day_3.JavaBean

  1.JavaBean的出现是为了减少代码的重复性。

  2.jsp:useBean创建JavaBean

    <% jsp:useBean id=”实例化对象名” scope = “设置范围” class=”类完整名称”%>

    scope的范围:page,request,sessionapplication。默认是:page

      a.写一个model:在com.rookie.model包下建立Student类。

 

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

 

      b.创建JavaBean并且使用(JSP):

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 
 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>Insert title here</title>
 9 </head>
10 <body>
11 <jsp:useBean id="student" scope="page" class="com.rookie.model"></jsp:useBean>
12 <%
13     student.setName("Salin");
14     student.setAge(20);
15 %>
16 
17 <h2>Name:<%=student.getName() %></h2>
18 <h2>Age:<%=student.getAge() %></h2>
19 </body>
20 </html>

      加入Tomcat中运行观察。

  3.jsp:setProperty设置javabean属性值:

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

    参数对应即可。

 

 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>Insert title here</title>
 8 </head>
 9 <body>
10 <% 
11     response.setCharacterEncoding("utf-8");
12 %>
13 <jsp:useBean id="stu" scope="page" class="salin.Student"/>
14 <jsp:setProperty property="name" name="stu" value="撒灵"/>
15 <h1> Name: <%= stu.getName() %></h1>
16 <h1> Age : <%= stu.getAge() %></h1>
17 </body>
18 </html>

 

 

  4.jsp:getProperty获取JavaBean属性:

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

JavaBean01.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>Insert title here</title>
 8 </head>
 9 <body>
10 <jsp:useBean id="student" scope="page" class="salin.Student"></jsp:useBean>
11 <% 
12     response.setCharacterEncoding("utf-8");
13 %>
14 
15 <jsp:useBean id="stu" scope="request" class="salin.Student"/>
16 <jsp:setProperty property="name" name="stu" value="中国"/>
17 <jsp:setProperty property="age" name="stu" value="100"/>
18 
19 <jsp:forward page="target01.jsp"></jsp:forward>
20 </body>
21 </html>

target01.jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="salin.Student" %>
<!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>Insert title here</title>
</head>
<body>
<%response.setCharacterEncoding("utf-8");%>
<jsp:useBean id="stu" scope="request" class="salin.Student"></jsp:useBean>
<h1><jsp:getProperty property="name" name="stu"/></h1>
<h1><jsp:getProperty property="age" name="stu"/></h1>
</body>
</html>

 

  5.保存范围的设置:  

  6.删除bean

    范围.removeAttribute(实例化对象名)

posted @ 2017-08-15 11:40  TulLing  阅读(164)  评论(0编辑  收藏  举报