使用JSP / bean操作让我的脚步受到影响。我收到了在我的输出页面上第14行处理JSP页面/foo/output.jsp的异常。 java文件编译好了。
的Input.jsp
<p>First Number: <input type="text" name="first" value="" /></p>
<p>Second Number: <input type="text" name="second" value="" /></p>
<p>Action: <select name="compu">
<option value="1">+</option>
<option value="2">-</option>
<option value="3">*</option>
<option value="4">/</option>
</select></p>
<input type="submit" />
</form>
output.jsp的
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.util.*" errorPage="" %>
<jsp:useBean id="foo" class="stuff.DerInput" scope="page" />
<jsp:setProperty name="foo" property="*" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<jsp:getProperty name="foo" property="calculation" />
<p>The result: <%= foo.getCalculation() %></p>
</body>
</html>
DerInput.Java
package stuff;
public class DerInput {
int first, second, compu, theresult;
String notation;
public void setFirst( int value )
{
first = value;
}
public void setSecond( int value )
{
second = value;
}
public void setCompu( int value )
{
compu = value;
}
public String getCalculation() {
switch (compu) {
case 1:
theresult = first + second;
notation = "+";
break;
case 2:
theresult = first - second;
notation = "-";
break;
case 3:
theresult = first * second;
notation = "*";
break;
case 4:
theresult = first / second;
notation = "/";
break;
}
return first + " " + notation + " " + second + " = " + theresult;
}
}
答案 0 :(得分:2)
看这里,
<jsp:getProperty name="foo" property="calculation" />
<p>The result: <%= foo.getCalculation() %></p>
您希望页面范围内的bean ${foo}
也可以在 scriptlet 范围<% foo %>
中使用。这不是真的。它们不共享相同的变量范围。这只会导致NullPointerException
来电getCalculation()
,因为<% foo %>
为null
。
改为使用EL。
<p>The result: ${foo.calculation}</p>
(注意:<jsp:getProperty>
行在这里是多余的,所以我将其删除了)