使用bean的表单输出

发布于 2024-12-06 18:14:29 字数 2493 浏览 3 评论 0原文

让我的脚接触 JSP/bean 动作。我在输出页面上遇到 处理 JSP 页面 /foo/output.jsp 第 14 行时发生异常。 java文件编译正常。

输入.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;
    }
}

Getting my feet wet with JSP/bean action. I'm getting An exception occurred processing JSP page /foo/output.jsp at line 14 on my output page. The java file compiles ok.

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2024-12-13 18:14:29

看这里,

<jsp:getProperty name="foo" property="calculation" />
<p>The result: <%= foo.getCalculation() %></p>

您期望页面作用域 bean ${foo}scriptlet 作用域 <% foo %> 中可用,如下所示出色地。这不是真的。它们不共享相同的变量范围。这只会导致 getCalculation() 调用出现 NullPointerException,因为 <% foo %>null.

使用 EL 代替。

<p>The result: ${foo.calculation}</p>

(注意: 行在这里是多余的,所以我将其删除)

Look here,

<jsp:getProperty name="foo" property="calculation" />
<p>The result: <%= foo.getCalculation() %></p>

You're expecting that the page scoped bean ${foo} is available in scriptlet scope <% foo %> as well. This is not true. They do not share the same variable scope. This would only result in a NullPointerException on the getCalculation() call, because <% foo %> is null.

Use EL instead.

<p>The result: ${foo.calculation}</p>

(Note: that <jsp:getProperty> line is superfluous here, so I removed it)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文