【问题标题】:JSP bean value not getting updatedJSP bean 值未更新
【发布时间】:2015-08-25 20:33:45
【问题描述】:

我在这里有 2 页,在第一页我接受输入,然后通过使用 java 类用户验证输入数据在秒页面上显示它。

输入表格

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="/myweb/formbean.jsp" method="post">

Username : <input type="text" name="name" placeholder="name"><br>
Password : <input type="text" name="password" placeholder="password">

<input type="submit" value="OK">
</form>

</body>
</html>

显示表格

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<jsp:useBean id="id1" class="test.user" scope="session" ></jsp:useBean>

<jsp:setProperty property="*" name="id1" />

<h1> <jsp:getProperty property="name" name="id1"/> </h1>
<h1> <jsp:getProperty property="password" name="id1"/> </h1>
<h1> <jsp:getProperty property="message" name="id1"/> </h1>

<%= id1.validate() %>
</body>
</html>

JAVA 用户类

public class user {

    private String name;
    private String password;
    private String message;

    public user() {

        validate();
    }

    public user(String name, String password) {

        this.name = name;
        this.password = password;
        validate();
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getMessage() {
        return message;
    }



    public void validate() {

        if (name == null) {
            message = "name cannot be null";

        } else if (password == null) {
            message = "passowrd cannot be null";

        }
        message="form validatuion ok";

    }

}

当我提交表单参数时,消息值永远不会更新,它总是显示消息“名称不能为空” 不知道我在这里做错了什么,请帮我弄清楚。

【问题讨论】:

  • 您从session 获得user instance,您在哪里reading input from values 并创建user instance 并将其添加到session?你做了这部分吗?如果是,请显示代码

标签: java jsp javabeans


【解决方案1】:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<jsp:useBean id="id1" class="test.user" scope="session" ></jsp:useBean>

<jsp:setProperty property="*" name="id1" />

<h1> <jsp:getProperty property="name" name="id1"/> </h1>
<h1> <jsp:getProperty property="password" name="id1"/> </h1>
//==========================================
<%= id1.validate() %>
<h1> <jsp:getProperty property="message" name="id1"/> </h1>
//==========================================

</body>
</html>
//==============================================================
    public void validate() {

        if (name==null){message="name cannot be null";}
        else if(password==null) {message="passowrd cannot be null";}
        else{message="form validatuion ok";}
    }

【讨论】:

    猜你喜欢
    • 2012-07-22
    • 2012-11-15
    • 2021-11-02
    • 2013-12-30
    • 2017-01-18
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多