前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何在JSP里使用Java bean

如何在JSP里使用Java bean

作者头像
Jerry Wang
发布2020-07-21 11:15:20
1.6K0
发布2020-07-21 11:15:20
举报

JavaBean是特殊的Java类,是用Java语言写成的可重用组件,并且遵守JavaBeans API规范:

  • 提供一个默认的无参构造函数。
  • 需要被序列化并且实现了Serializable接口。
  • 可能有一系列可读写属性。
  • 可能有一系列的"getter"或"setter"方法。

一个例子:

代码语言:javascript
复制
public class StudentsBean implements java.io.Serializable
{
   private String firstName = null;
   private String lastName = null;
   private int age = 0;

   public StudentsBean() {
   }
   public String getFirstName(){
      return firstName;
   }
   public String getLastName(){
      return lastName;
   }
   public int getAge(){
      return age;
   }
   public void setFirstName(String firstName){
      this.firstName = firstName;
   }
   public void setLastName(String lastName){
      this.lastName = lastName;
   }
   public void setAge(Integer age){
      this.age = age;
   }
}

在JSP里使用java bean

jsp:useBean标签可以在JSP中声明一个JavaBean,然后使用。声明后,JavaBean对象就成了脚本变量,可以通过脚本元素或其他自定义标签来访问。jsp:useBean标签的语法格式如下:

<jsp:useBean id=“bean’s name” scope=“bean’s scope” typeSpec/>

其中,根据具体情况,scope的值可以是page,request,session或application。id值可任意只要不和同一JSP文件中其它jsp:useBean中id值一样就行了。

代码语言:javascript
复制
<html>
<head>
<title>get and set properties Example</title>
</head>
<body>

<jsp:useBean id="students" class="action.Students"> 
   <jsp:setProperty name="students" property="firstName" value="Zara"/>
   <jsp:setProperty name="students" property="lastName" value="Ali"/>
   <jsp:setProperty name="students" property="age" value="10"/>
</jsp:useBean>

<p>Student First Name: 
   <jsp:getProperty name="students" property="firstName"/>
</p>
<p>Student Last Name: 
   <jsp:getProperty name="students" property="lastName"/>
</p>
<p>Student Age: 
   <jsp:getProperty name="students" property="age"/>
</p>

</body>
</html>

注意这里有一个错误:undefined type:action.Students

我把Students类的implements java.io.Serializable语句删除,错误就消失了:

但是运行时又遇到新的错误:

代码语言:javascript
复制
org.apache.jasper.JasperException: Cannot find a method to write property [age] of type [int] in a bean of type [action.Students]
	org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:367)
	org.apache.jsp.main_jsp._jspService(main_jsp.java:135)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Note The full stack trace of the root cause is available in the server logs.

把此处的Integer改成int,错误消失:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 在JSP里使用java bean
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档