剛學jsp就遇到了這個問題,寫bean的時候想不要弄那麼複雜,就沒有將類打包,即沒有使用package語句,bean編譯的時候是正確的,就把編譯後的class檔案放到了WEB-INF/classes目錄下,接下來用
- <jsp:useBean id="onlineCount" class="mypack.OnlineCount" />
- <jsp:setProperty name="onlineCount" property="count" value="8" />
- <jsp:getProperty name="onlineCount" property="count" />
結果編譯錯誤了:
- org.apache.jasper.JasperException: Unable to compile class for JSP:
- An error occurred at line: 12 in the jsp file: /index.jsp
- OnlineCount cannot be resolved to a type
- 9: <title>鎴戠殑緗戠珯</title>
- 10: </head>
- 11: <body>
- 12: <jsp:useBean id="onlineCount" class="OnlineCount" />
- 13: <jsp:setProperty name="onlineCount" property="count" value="8" />
- 14: <jsp:getProperty name="onlineCount" property="count" />
- 15: </body>
- An error occurred at line: 12 in the jsp file: /index.jsp
- OnlineCount cannot be resolved to a type
- 9: <title>鎴戠殑緗戠珯</title>
- 10: </head>
- 11: <body>
- 12: <jsp:useBean id="onlineCount" class="OnlineCount" />
- 13: <jsp:setProperty name="onlineCount" property="count" value="8" />
- 14: <jsp:getProperty name="onlineCount" property="count" />
- 15: </body>
- An error occurred at line: 12 in the jsp file: /index.jsp
- OnlineCount cannot be resolved to a type
- 9: <title>鎴戠殑緗戠珯</title>
- 10: </head>
- 11: <body>
- 12: <jsp:useBean id="onlineCount" class="OnlineCount" />
- 13: <jsp:setProperty name="onlineCount" property="count" value="8" />
- 14: <jsp:getProperty name="onlineCount" property="count" />
- 15: </body>
- An error occurred at line: 14 in the jsp file: /index.jsp
- OnlineCount cannot be resolved to a type
- 11: <body>
- 12: <jsp:useBean id="onlineCount" class="OnlineCount" />
- 13: <jsp:setProperty name="onlineCount" property="count" value="8" />
- 14: <jsp:getProperty name="onlineCount" property="count" />
- 15: </body>
- 16: </html>
- Stacktrace:
- org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
- org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
- org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
- org.apache.jasper.compiler.Compiler.compile(Compiler.java:317)
- org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
- org.apache.jasper.compiler.Compiler.compile(Compiler.java:282)
- org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
- org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
- org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
- org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
- javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
查了好久的資料才知道了要怎麼解決,必須要把類放在包裡:
- <jsp:useBean id="onlineCount" class="mypack.OnlineCount" />
- <jsp:setProperty name="onlineCount" property="count" value="8" />
- <jsp:getProperty name="onlineCount" property="count" />
這樣才運行正確了。
但是我到現在還是不明白為什麼非要把類放在包裡呢?在部署servlet時卻可以不用放在包下面,問題現在是解決了,可是還是不知道為什麼,還望知道的朋友告訴我一聲,謝謝。