useBean类属性的值无效 [英] The value for the useBean class attribute is invalid
问题描述
我是JSP的新手,正在尝试简化功耗计算器.所以我从用户那里取了2个数字,后来我得到了计算结果并显示在页面上.这是我的bean类:
I am new in JSP and trying to to simple power calculater. So I take 2 numbers from user and later I get result of calculation and show on page. Here is my bean class:
package org.mypackage.power;
public class MyPow {
private double base;
private double pow;
private double result;
MyPow()
{
base = 0;
pow=1;
}
/**
* @return the base
*/
public double getBase() {
return base;
}
/**
* @param base the base to set
*/
public void setBase(double base) {
this.base = base;
}
/**
* @return the pow
*/
public double getPow() {
return pow;
}
/**
* @param pow the pow to set
*/
public void setPow(double pow) {
this.pow = pow;
}
/**
* @return the result
*/
public double getResult() {
return Math.pow(base, pow);
}
/**
* @param result the result to set
*/
public void setResult(double result) {
this.result = result;
}
}
这是索引页:
<HTML>
<BODY>
<FORM METHOD=POST ACTION="result.jsp">
What's your base? <INPUT TYPE=TEXT NAME=base SIZE=20>
What is your power <INPUT TYPE=TEXT NAME=power SIZE=10>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
这是将显示结果的JSP页面
And here is the JSP page that will show the result
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<jsp:useBean id="powerBean" scope="session" class="org.mypackage.power.MyPow" />
<jsp:setProperty name="powerBean" property="*"/>
<jsp:getProperty name="powerBean" property="result"/>
</body>
</html>
这段代码给出
useBean类属性的值无效
The value for the useBean class attribute is invalid
我的课程在the org.mypackage.power.MyPow
软件包下.在更新之前,这是一个简单的问候世界,并且可以正常工作.但是我只是更改类并添加新字段并更改了JSP页面.有人可以帮我吗?
My class is under the org.mypackage.power.MyPow
package. Before I update this it was a simple hello world and was working correctly. But I just change class and add new fields and changed JSP page. Could anyone help me please?
我正在使用Tomcat 7.0.14和Netbeans 7.01
I am using Tomcat 7.0.14 and Netbeans 7.01
推荐答案
此错误基本上意味着
MyPow powerBean = new MyPow();
失败了.
bean必须具有public
构造函数.因此,更改package-private构造函数
The beans are required to have a public
constructor. So, change the package-private constructor
MyPow() {
// ...
}
给公共建设者
public MyPow() {
// ...
}
通过这种方式,JSP(本身位于不同的程序包中)将能够访问和调用Bean的构造函数.
This way JSP (which is by itself in a different package) will be able to access and invoke the bean's constructor.
这篇关于useBean类属性的值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!