Topic: jsp中用不了javabean!! |
Print this page |
1.jsp中用不了javabean!! | Copy to clipboard |
Posted by: cmslovehxh Posted on: 2003-04-13 16:45 sample_bean.jsp如下: <%@ page language="java" %> <HTML> <BODY> <jsp:useBean id="TaxRate" scope="application" class="TaxRate" /> 修改前: <br> 产品 : <jsp:getProperty name="TaxRate" property="Product" /> <br> 税率 : <jsp:getProperty name="TaxRate" property="Rate" /> <jsp:setProperty name="TaxRate" property="Product" value="Hello" /> <jsp:setProperty name="TaxRate" property="Rate" value="2.9" /> 修改后: <br> 产品 : <jsp:getProperty name="TaxRate" property="Product" /> <br> 税率 : <jsp:getProperty name="TaxRate" property="Rate" /> </BODY></HTML> TaxRate.java如下: public class TaxRate{ String Product; double Rate; public TaxRate(){ this.Product = "A001"; this.Rate = 5; } public void setProduct (String ProductName){ this.Product = ProductName; } public String getProduct(){ return(this.Product); } public void setRate (double rateValue){ this.Rate = rateValue; } public double getRate(){ return (this.Rate); } } 错误信息: 500 Servlet Exception /sample_bean.jsp:7: bean `TaxRate' has no property named `Product' Resin 1.2.3 (built Thu Feb 15 11:31:58 PST 2001) |
2.Re:jsp中用不了javabean!! [Re: cmslovehxh] | Copy to clipboard |
Posted by: looluo Posted on: 2003-04-13 16:54 `Product'P改为小写 |
3.Re:jsp中用不了javabean!! [Re: cmslovehxh] | Copy to clipboard |
Posted by: why Posted on: 2003-04-13 20:08 Rate 也要改為 rate,這是 JavaBean 的 convention。 其實所有 Java 的變數 (variable) 的第一個字母都應小寫,例如: public void setProduct (String productName) { ... } JavaBean 的 convention 還有 all data member defined in a class be private,例如: private String product; private double rate; |
4.Re:jsp中用不了javabean!! [Re: cmslovehxh] | Copy to clipboard |
Posted by: cmslovehxh Posted on: 2003-04-13 22:46 why,thank you for you help! 可是改了之后还有这样的错误: Note: sun.tools.javac.Main has been deprecated. /sample_bean.jsp:4: Class _jsp.TaxRate not found. TaxRate TaxRate; ^ /sample_bean.jsp:6: Class _jsp.TaxRate not found. TaxRate = (TaxRate) application.getAttribute("TaxRate"); ^ /sample_bean.jsp:8: Class _jsp.TaxRate not found. TaxRate = new TaxRate(); ^ /sample_bean.jsp:7: Class _jsp.TaxRate not found. out.print(((TaxRate) pageContext.findAttribute("TaxRate")).getProduct()); ^ /sample_bean.jsp:9: Class _jsp.TaxRate not found. out.print(((TaxRate) pageContext.findAttribute("TaxRate")).getRate()); ^ /sample_bean.jsp:11: Variable TaxRate may not have been initialized. TaxRate.setProduct("Hello"); ^ /sample_bean.jsp:15: Class _jsp.TaxRate not found. out.print(((TaxRate) pageContext.findAttribute("TaxRate")).getProduct()); ^ /sample_bean.jsp:17: Class _jsp.TaxRate not found. out.print(((TaxRate) pageContext.findAttribute("TaxRate")).getRate()); ^ 8 errors, 1 warning |
5.Re:jsp中用不了javabean!! [Re: cmslovehxh] | Copy to clipboard |
Posted by: why Posted on: 2003-04-14 02:09 the error messages are clear enough! You can't use TaxRate as both variable name and class name! <jsp:useBean id="TaxRate" scope="application" class="TaxRate" /> you may want to use taxRate as id. I'm wondering you'd better work a bit harder on basic Java programming before carrying on with JSP, Servlets, etc. |
6.Re:jsp中用不了javabean!! [Re: cmslovehxh] | Copy to clipboard |
Posted by: cmslovehxh Posted on: 2003-04-14 12:58 不是这个问题,我改了后也是一样出现这样的错误。 |
7.Re:jsp中用不了javabean!! [Re: cmslovehxh] | Copy to clipboard |
Posted by: why Posted on: 2003-04-14 20:36 sorry, my mistake, you can use TaxRate as both variable name and class name, though it would be a poor practice. the error message shows that TaxRate.class can't be resolved/located while JSP requires a fully qualified name of the class. where did you put TaxRate.class? 1. you may put its folder in the CLASSPATH (used by the web container, Tomcat in your case), but this is not the common practice. 2. I'm not sure it'll work but you may try putting it under WEB-INF/classes/ you may need to add <%@ page import="TaxRate" %> but still 1.4 (maybe 1.3) JVM will complain TaxRate is not a fully qualified name while the default package class is no longer supported -- for import only, I'm not sure about the reason. (if possible, please check and let us know ) 3. anyway, what I'll do is as follows: 1. put TaxRate in a package, say xyz 2. put xyz/TaxRate.class in WEB-INF/classes/ 3. change to <jsp:useBean id="taxRate" scope="application" class="xyz.TaxRate" /> |
8.Re:jsp中用不了javabean!! [Re: cmslovehxh] | Copy to clipboard |
Posted by: cmslovehxh Posted on: 2003-04-15 14:07 再次感谢why! 因为少了<%@ page import="TaxRate" %>所以就出现了错误!其他都没有问题! |
9.Re:jsp中用不了javabean!! [Re: cmslovehxh] | Copy to clipboard |
Posted by: cmslovehxh Posted on: 2003-04-15 14:09 “Rate 也要改為 rate,這是 JavaBean 的 convention。” 这个只是编码风格而已,不用改也可以运行的。 |
10.Re:jsp中用不了javabean!! [Re: cmslovehxh] | Copy to clipboard |
Posted by: why Posted on: 2003-04-15 21:24 cmslovehxh wrote: 不一定可以運行的啦!有些 JavaBean Introspector 不是那麼寬容的喇! 小可竊以為遵守 convention 是編程的第一要項,否則無法有 teamwork,那就事事不成囉! 因为少了<%@ page import="TaxRate" %>所以就出现了错误!其他都没有问题! 用 1.4 版的 JVM 再試試 小可忠告:不用 package (請分三個音節唸), 吃虧在眼前! |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |