javaweb中javaBean的简单应用(动作表示读取,设置)

主要就是通过设置javaBean;
通过jsp里面的动作表示jsp:useBean 来读取或者设置里面的属性值;

package com.dmh;

public class Produce {
    private String name = "电吉他";
    private double price = 1880.5;
    private int count = 100;
    private String factoryAdd = "吉林省长春市XX琴行";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getFactoryAdd() {
        return factoryAdd;
    }

    public void setFactoryAdd(String factoryAdd) {
        this.factoryAdd = factoryAdd;
    }
}

下面的是jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>获取JavaBean中的相关数据title>
head>
<body>
<jsp:useBean id="produce" class="com.dmh.Produce">jsp:useBean>

<div>
    商品名称:<jsp:getProperty name="produce" property="name"/>
div>
<div>
    商品价格:<jsp:getProperty name="produce" property="price"/>
div>
<div>
    商品数量:<jsp:getProperty name="produce" property="count"/>
div>
<div>
    商品厂址:<jsp:getProperty name="produce" property="factoryAdd"/>
div>

<div > -------------- 分割线--------------div>
    <div > 用jsp动作标示设置javabean里面的相关属性值div>

   <jsp:setProperty name="produce" property="name" value="大雄"/>
   <jsp:setProperty name="produce" property="price" value="185.03"/>
   <jsp:setProperty name="produce" property="count" value="50"/>
   <jsp:setProperty name="produce" property="factoryAdd" value="浙江省杭州市XX地区"/>
<div > -------------- 分割线--------------div>
<div>
    商品名称:<jsp:getProperty name="produce" property="name"/>
div>
<div>
    商品价格:<jsp:getProperty name="produce" property="price"/>
div>
<div>
    商品数量:<jsp:getProperty name="produce" property="count"/>
div>
<div>
    商品厂址:<jsp:getProperty name="produce" property="factoryAdd"/>
div>
body>
html>

最后浏览器中打开就是这个样子——
很简单的应用;

javabean中的定义的属性名称一定要与jsp中
property的名称一样,哟啊不然页面会报错
javaweb中javaBean的简单应用(动作表示读取,设置)_第1张图片

你可能感兴趣的:(后端)