在jsp页面中使用javaBean:三个标签;

<jsp:useBean>标签

<jsp:setProperty>标签

<jsp:getProperty>标签

首先建立一个customer类:

package com.lanqiao.javatest;

public class Customer {
private Integer id;
private String name;
private int age;
public Customer() {
super();
// TODO Auto-generated constructor stub
}
public Customer(Integer id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", age=" + age + "]";
} }

建立一个jsp页面:直接调用customer的属性和方法,并且可以给属性赋值;

<%@page import="com.atguigu.javaweb.Customer"%>
<%@ 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>Insert title here</title>
</head>
<body> <jsp:useBean id="customer" class="com.lanqiao.javatest.Customer"
scope="request"></jsp:useBean> <jsp:useBean id="customer2" beanName="com.lanqiao.javatest.Customer"
type="java.lang.Object" scope="request"></jsp:useBean> <%--
Object customer2 = request.getAttribute("customer2");
if(customer2){
customer2 = Class.forName("com.lanqiao.javatest.Customer").newInstance();
request.setAttribute("customer2", customer2);
}
--%> <!-- 若 property 的值为 *, 省略 value 属性值, 则将自动为所有属性赋值为对应的请求参数的值. -->
<jsp:setProperty property="*" name="customer"/> <%--
<jsp:setProperty property="name" value="ATGUIGU2" name="customer"/>
--%> age: <jsp:getProperty property="age" name="customer"/>
<br>
name: <jsp:getProperty property="name" name="customer"/>
<br>
id: <jsp:getProperty property="id" name="customer"/> <%--
<%= customer.getAge() %>
--%> <%--
customer.setAge(10);
--%> <%--
//1. 从 scope(session) 中获取 id(customer) 属性值, 赋给 class(com.lanqiao.javatest.Customer)
//类型的 id(customer) 变量
Customer customer = (Customer)session.getAttribute("customer"); //2. 若属性值为 null, 则利用反射创建一个新的对象, 把该对象赋给 id(customer), 并以 id(customer)
//为属性名让如到 scope(session) 中.
if(customer == null){
customer = (Customer)Class.forName("com.lanqiao.javatest.Customer").newInstance();
session.setAttribute("customer", customer);
}
--%> </body>
</html>

javaBean的使用方法;的更多相关文章

  1. Eclipse快速生成一个JavaBean类的方法

    原文: https://jingyan.baidu.com/article/948f5924156866d80ff5f921.html Eclipse快速生成一个JavaBean类的方法 听语音 | ...

  2. 【JavaWeb】【JSP】【Bean】JavaBean基础使用方法与操作步骤

    JavaBean基础使用方法与操作步骤 JavaWeb jsp Bean 项目结构 JavaBean的概念 JavaBean是可复用的.平台独立的软件组件 JavaBean既可以是简单的GUI要素,如 ...

  3. (转)json格式转换成javaBean对象的方法

    把json格式转换成javaBean才可以.于是查了一下资料,网上最多的资料就是下面的这种方式: Java code? 1 2 3 4 5 6 7 8 9 String str = "[{\ ...

  4. JavaBean的toString方法工具类

    import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.r ...

  5. JavaBean转Map方法

    Map<String, Object> fieldMap =new HashMap<String, Object>(); BeanInfo beanInfo = Introsp ...

  6. IntelliJ:自动生成JavaBean的读写方法

    1.定义好一个class,其中写好private属性. 2.右键点击该class,在generate中选择自动根据模板生成的读写方法.

  7. 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性

      本文转载自:http://blog.sina.com.cn/s/blog_5d65a16901011kom.html 关键字:内省.JavaBean.PropertyDescriptor类.Int ...

  8. java-内省与javabean

    JavaBean 是一种JAVA语言写成的可重用组件.为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器.JavaBean 通过提供符合一致性设计模式的公共方法将内部域暴露成员属性 ...

  9. JavaBean 基础概念、使用实例及代码分析

    JavaBean 基础概念.使用实例及代码分析 JavaBean的概念 JavaBean是一种可重复使用的.且跨平台的软件组件. JavaBean可分为两种:一种是有用户界面的(有UI的):另一种是没 ...

随机推荐

  1. Java基础之处理事件——应用程序中的语义事件监听器(Sketcher 5 with element color listeners)

    控制台程序. 为了标识元素的类型,可以为菜单已提供的4中元素定义常量,用作ID.这有助于执行菜单项监听器的操作,还提供了一种标识颜色类型的方式.我们会累积许多应用程序范围的常量,所以把它们定义为可以静 ...

  2. java中HashSet详解(转)

    HashSet 的实现 对于 HashSet 而言,它是基于 HashMap 实现的,HashSet 底层采用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,查看 HashSe ...

  3. MD5和DES加密方法

        /// <summary>         /// MD5加密         /// </summary>         /// <param name=&q ...

  4. PostgreSQL simple select(group by and insert into ...select)

    warehouse_db=# create table student(number int primary key,name varchar(20),age int);CREATE TABLEwar ...

  5. Lintcode: Route Between Two Nodes in Graph

    Given a directed graph, design an algorithm to find out whether there is a route between two nodes. ...

  6. DIY小能手|别买电动滑板车了,咱做一台吧

    !! http://www.shoudian.org/thread-316111-1-1.html http://www.jiequer.com/html/news/xinpin/2014/1218/ ...

  7. 【Origin】时迁念昔

    -清明,未曾归,恰大门来沪,晨起准备,车道劳顿,隔五年而见一面,感时事变迁,物各两异,小道旁之争艳花木,觉道长且阻,叹而留记. 清明时节雨纷纷, 不辞跋涉见故人; 红花绿叶皆失色, 握手言欢语无伦. ...

  8. t4 multiple output sample

    <#@ output extension=".js" #> <#@ template debug="false" hostspecific=& ...

  9. sdutoj 2603 Rescue The Princess

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2603 Rescue The Princess ...

  10. 复习课程jdbc:使用配置文件properties进行连接数据库,数据库存取图片,批处理,时间戳,事物回滚等等

    使用配置文件properties进行连接数据库 首先创建一个file自定义文件名,但是后缀名必须改为.properties(不分大小写):如config.properties: 然后双击config. ...