我有一个带有两个输入字段的简单jsp,当用户访问页面并将数据输入到字段中时,我希望在单击提交时填充bean。我希望jsp中的信息填充在带有set的bean中。在jsp上我使用了setproperty,但是当一切都运行时,字段只被发送到null。
JSP:
<form method=POST>
<div id="login" style="text-align: center">
Username: <input type="text" name="username" /><br>
</div>
<div id="password" style="text-align: center">
Password: <input type="password" name="password" />
</div>
<div id = "error" style="text-align: center">
<%logBean.getError();%>
</div>
<div id="login" style="text-align: center">
<input type="submit" value="Login" />
</div>
</form>
一个简单的帖子发生,那就是我希望填充bean
豆:
public class LoginBean {
String password = "";
String username = "";
String error = "";
public void setPassword(String password){
if(password.equals(null)){
password = "";
}
this.password = password.trim();
}
public String getPassword(){
return password;
}
public void setUsername(String username){
if(username.equals(null)){
username = "";
}
this.username = username.trim();
}
public String getUsername(){
return username;
}
public void setError(String error){
this.error = error.trim();
}
public String getError(){
return error;
}
}
我需要使用bean的操作:
String username = loginBean.getUsername();
String password = loginBean.getPassword();
Stacktrace错误:
java.lang.NullPointerException
Actions.LoginAction.verify(LoginAction.java:18)
org.apache.jsp.content.login_jsp._jspService(login_jsp.java:92)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
如何让jsp使用bean设置字段,以便我可以在不同的java方法中调用这些值?