我对JSP和JavaBeans很陌生,并尝试如何将参数值从一个JSP(theForm.jsp)中的表单传递到另一个JSP(theResult.jsp),如下所示:
在我的theResult.jsp
:
<body>
<jsp:useBean id="user" class="people.User" scope="application"/>
Your username is: <jsp:getProperty name="user" property="username"/><br>
Your password is: <jsp:getProperty name="user" property="password"/><br>
</body>
在我的theForm.jsp
文件中:
<body>
<jsp:useBean id="user" class="people.User" scope="application">
<jsp:setProperty name="user" property="username" value='<%=request.getParameter("username")%>'/>
<jsp:setProperty name="user" property="password" param="password"/> // another way to use setProperty, I read
</jsp:useBean>
<form action="theResult.jsp" method="post">
<input type="text" name="username" placeholder="Type your username"><br>
<input type="password" name="password" placeholder="Type your password"><br>
<input type="submit" value="submit">
</form>
</body>
但我的结果显示是:
您的用户名是:null
您的密码为:null
我的people.user
javabean:
package people;
public class User implements java.io.Serializable {
private String username;
private String password;
public User(){}
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
//... and same thing for 'password'
}
答案 0 :(得分:0)
<body>
<jsp:useBean id="user" class="people.User"/>
<jsp:setProperty name="user" property="*"/>
<%-- the (*) symbol indicates the value should come
from request parameter whose name matches property
name . Thus simple automatic conversion should be
performed --%/>
Your username is: <jsp:getProperty name="user" property="username"/><br>
Your password is: <jsp:getProperty name="password"/><br>