Part.java
package part; public class Part { String pName;//零件名字 String pColor;//零件颜色 int pQuantity;//零件数量 public Part() {//默认构造函数 this.pName="Y1524"; this.pColor="red"; this.pQuantity=30; } public String getpName() {//获取零件名字 return pName; } public void setpName(String pName) {//设置零件名字 this.pName = pName; } public String getpColor() {//获取零件颜色 return pColor; } public void setpColor(String pColor) {//设置零件颜色 this.pColor = pColor; } public int getpQuantity() {//获取零件数量 return pQuantity; } public void setpQuantity(int pQuantity) {//设置零件质量 this.pQuantity = pQuantity; } }
Part.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>使用Javabean</title> </head> <body> <jsp:useBean id="pr" scope="application" class="part.Part"> 修改前 <BR>使用getProperty取得零件的属性值 <BR>pName:<jsp:getProperty name="pr" property="pName"/> <BR>pColor:<jsp:getProperty name="pr" property="pColor"/> <BR>pQuantity:<jsp:getProperty name="pr" property="pQuantity"/> <BR><BR>使用类定义的方法获取零件的属性值 <BR>pName:<%=pr.getpName() %> <BR>pColor:<%=pr.getpColor() %> <BR>pQuantity:<%=pr.getpQuantity() %> <HR><BR>修改后 <jsp:setProperty name="pr" property="pName" value="K1365"/> <jsp:setProperty name="pr" property="pColor" value="blue"/> <jsp:setProperty name="pr" property="pQuantity" value="135"/> <BR>使用getProperty取得零件的属性值 <BR>pName:<jsp:getProperty name="pr" property="pName"/> <BR>pColor:<jsp:getProperty name="pr" property="pColor"/> <BR>pQuantity:<jsp:getProperty name="pr" property="pQuantity"/> <BR><BR>使用类定义的方法获取零件的属性值 <BR>pName:<%=pr.getpName() %> <BR>pColor:<%=pr.getpColor() %> <BR>pQuantity:<%=pr.getpQuantity() %> </jsp:useBean> </body> </html>
原文:https://www.cnblogs.com/sancha/p/10667268.html