在測試jsp的動作元素<jsp:useBean >時,寫了一個計數器的栗子:
JavaBean:

1 package com.pers.count; 2 /**
3 * @author liangyadong 4 * @date 2017年4月11日 下午3:10:05 5 * @version 1.0 6 */
7 public class Counter { 8 int count = 0; 9 public Counter(){} 10 public int getCount() { 11 count++; 12 return count; 13 } 14 public void setCount(int count) { 15 this.count = count; 16 } 17
18 }
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 <%-- 指定JavaBean實例,相應的生存范圍及全限定類名 --%>
11 <jsp:useBean id="countbean" scope="application" class="count.Counter" />
12 <%-- 使用getProperty動作元素獲得count屬性值 --%>
13 the number of requests is: 14 <jsp:getProperty property="countbean" name="count"/>
15 </body>
16 </html>
上圖:
解決見圖中注釋.但是!!!凡事都有個但是!頁面雖然不報錯了,但是啟動tomcat后訪問該jsp,又出現了錯誤:The value for the useBean class attribute com.pers.count.Counter is invalid.
這特么就尷尬了.統共兩行代碼還給報了個這錯???
原因:<jsp:getProperty property="" name="">這個動作元素中的property和name的值寫反了!此處的name的值應該和上面<jsp:useBean id="" scope="" class="">中的id對應!!!
解決:
line14改為:
<jsp:getProperty property="count" name="countbean"/>
好了,重啟湯姆凱特,訪問頁面並刷新,計數器好使了.