【问题标题】:Communication between servlet and jsp doesn't work - PropertyNotFoundExceptionservlet 和 jsp 之间的通信不起作用 - PropertyNotFoundException
【发布时间】:2012-08-02 11:29:14
【问题描述】:

我正在尝试将信息从我的 servlet 传递到 jsp ,其中一些字段成功传递,而另一些则没有。

这是我输入输入的初始页面:

index.html的代码

<!DOCTYPE html>
<html>
<head><title>Bank application</title>
<link rel="stylesheet"
      href="./css/styles.css"
      type="text/css"/>
</head>
<body>
<table class="title">
  <tr><th>Web Bank application</th></tr>
</table>


<br/>
<fieldset>
  <legend>Registration</legend>
  <form action="register"> 
    First name: <input type="text" name="firstName"><br>
    Last name : <input type="text" name="lastName"><br>
    Address   : <input type="text" name="address"><br>
    ID-number : <input type="text" name="idnumber"><br>
    User-Name : <input type="text" name="userName"><br>
    Password  : <input type="text" name="password"><br>
    <input type="submit" value="Register">
  </form>
</fieldset>

我想将数据传递给一个名为 show-name.jsp 的 jsp:

<!DOCTYPE html>
<html>
<head><title>Thanks for Registering</title>
<link rel="stylesheet"
      href="./css/styles.css"
      type="text/css"/>
</head>
<body>
<h1>Congratulations ! You are now registered to our bank</h1>
<h2>First Name: ${name.firstName}</h2>  // that's okay 
<h2>Last Name: ${name.lastName}</h2>    // that's okay  
<h2>Address: ${name.address}</h2>       // that's okay 
<h2>Password: ${name.password}</h2>     // that's okay 
<h2>User-Name: ${name.userName}</h2>    // that's not okay 
<h2>Id Number: ${name.idnumber}</h2>    // that's not okay 
</body></html>

我在 Eclipse 中得到以下异常:

root cause 

javax.el.PropertyNotFoundException: Property 'userName' not found on type model.Person

或:

root cause 

javax.el.PropertyNotFoundException: Property 'idnumber' not found on type model.Person

奇怪的是,所有其他 4 字段都显示得很好,这意味着 name.firstName & name.lastName & name.address & name.password ,但其他两个不是,即使我添加了一对System.out.prinln-s 的:

      System.out.println(person.getID());
      System.out.println(person.getUsername());

在servlet中,数据成功呈现给console,意思是我得到了:

4444
5555

。但是由于某种原因,数据部分没有传递给 jsp

为什么?

谢谢

【问题讨论】:

    标签: java html jsp web-applications servlets


    【解决方案1】:

    我认为这是因为 EL 使用 getter 来获取 bean 的属性。

    idnumberPerson bean getter 中,getIdnumberisIdnumber 不是 getID,因此您可以看到错误,指出在 model.Person 类型上找不到属性“idnumber”。

    userName 的吸气剂也一样,它不是getUserName,而是getUsername

    【讨论】:

      【解决方案2】:

      您应该将您的 EL 更改为 name.username 或者您应该更改您的 Person 类并将方法重命名为 getUserName()(EL 中 userName 中的 N 大写,但在您的 Person 类中不大写)

      【讨论】:

        猜你喜欢
        • 2011-01-25
        • 1970-01-01
        • 1970-01-01
        • 2012-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-15
        • 1970-01-01
        相关资源
        最近更新 更多