我在使用bean显示错误消息时遇到问题。这就是我在登录页面上尝试做的事情:
我得到的问题是:每当我关闭tomcat并重新运行它时,第一次我不在电子邮件字段中输入电子邮件,就会显示错误。之后,如果我曾在电子邮件字段中输入一个值。错误消息将永远不再显示(当我不在电子邮件字段中输入电子邮件时)
这是我的代码:
dologin.jsp有
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.skf.setup.data.userData.userData" %>
<jsp:useBean id="loginFormData" class = "com.skf.setup.data.form.loginForm" scope="session" />
<jsp:useBean id="userData" class="com.skf.setup.data.userData.userData" scope="session" />
<jsp:useBean id="userDataBean" class="com.skf.setup.data.userData.userDataBean" scope="session" />
<jsp:useBean id="errorData" class="com.skf.setup.data.error.formError" scope="request" />
<jsp:setProperty name = "loginFormData" property="*" />
<jsp:include page="/setup/layout/header/include.jsp" />
<title>Login - SKF Cassino Database</title>
</head>
<body style="background: #006687;">
<div class="container">
<!-- LOGIN FORM -->
<form class="form-horizontal" method="post" action="doLogin.jsp" role="form">
<div class="login">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">
<img class="login-panel" alt="SKF logo"
src="<%=request.getContextPath()%>/setup/include/img/skf-logo-white.png"> Cassino Database
</h3>
</div>
<% if(request.getParameter("login")!=null)
{
out.println("Text");
if(userData.validate(loginFormData,errorData)==true)
{
// out.println("Text2");
userData udata = new userData();
userData.setData((String)loginFormData.getEmail(), udata);
// userData.setEmail((String)loginFormData.getEmail());
out.println(udata.getId());
}
}
%>
<div class="panel-body">Accedi
<% for(String currError: errorData.getGenErrors())
out.println(currError);
%>
</div>
<div style="width: 350px; margin: 10px auto;">
<input type="text" placeholder="username@email.com"
class="form-control" name="email" />
<br>
<input type="password" placeholder="password" class="form-control" name="password" required />
<br>
<select class="form-control" name="role">
<option selected>Role</option>
<option value="acquisti" >Acquisti</option>
<option value="SF" >Solution Factory</option>
<option value="cassino" >SKF Cassino</option>
</select>
<br>
<input type="submit" class="btn btn-primary" value= "Entra" name="login" type="submit" />
<input class="btn btn-warning" value="Annulla" type="reset" />
</div>
</div>
</div>
</form>
<!-- LOGIN FORM ENDS HERE-->
</div>
</body>
</html>
formError.java
/**
* package that is used to display errors on the fly after being added
*/
package com.skf.setup.data.error;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @author NADIR Ibrahim
*
*/
public class formError
{
private ArrayList<String> genError;
private HashMap<String,String> fieldErrors;
public formError()
{
this.genError = new ArrayList<String>();
this.fieldErrors = new HashMap<String,String>();
}
public ArrayList<String> getGenErrors() {
return genError;
}
public void setGenError(String error) {
this.genError.add(error);
}
public HashMap<String, String> getFieldErrors() {
return fieldErrors;
}
public void setFieldErrors(HashMap<String, String> fieldErrors) {
this.fieldErrors = fieldErrors;
}
public void addFieldErrors(String field,String error)
{
if(this.fieldErrors.containsKey(field) || !this.fieldErrors.isEmpty())
{
this.fieldErrors.put(field,error);
}
else if(this.fieldErrors.containsKey(field))
{
this.fieldErrors.remove(field);
this.fieldErrors.put(field,error);
}
}
public String getFieldErrors(String field)
{
return fieldErrors.get(field);
}
public boolean isEmpty()
{
boolean empty = true;
if(!genError.isEmpty() && !fieldErrors.isEmpty())
empty = false;
return empty;
}
public boolean hasFieldError()
{
boolean hasError = false;
if(!fieldErrors.isEmpty())
hasError = true;
return hasError;
}
public boolean hasFieldError(String field)
{
boolean hasError = false;
if(!fieldErrors.get(field).isEmpty() || fieldErrors.get(field)!=null)
hasError = true;
return hasError;
}
}
验证功能
public boolean validate(loginForm value,formError error)
{
boolean valid = false;
String email = value.getEmail();
String password = value.getPassword();
String role = value.getRole();
//check if values are entered by user or not, if not add error to error map and notify user
if(email.isEmpty())
{
//TODO ADD ERROR TO HASHMAP
error.setGenError("Email is necessary!");
}
if(password.isEmpty())
{
//TODO ADD ERROR TO HASHMAP
}
if(role.isEmpty())
{
//TODO ADD ERROR TO HASHMAP
}
//check if values entered by user are valid or not
if(!email.isEmpty() && !password.isEmpty() && !role.isEmpty())
{
Connection c = DBConnection.dbConnect();
String sql = "SELECT email,password,role FROM users WHERE email=? AND password=? AND role=?";
PreparedStatement ps;
try {
ps = c.prepareStatement(sql);
ps.setString(1,(String)value.getEmail());
ps.setString(2,(String)value.getPassword());
ps.setString(3,(String)value.getRole());
ResultSet rs = ps.executeQuery();
if(rs.next())
{
valid = true;
}
else
{
//TODO ADD ERROR THAT USER ID AND PASSWORD ARE WRONG
}
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
DBConnection.closeCon(c);
}
}
return valid;
}
我是Beans的新手并且正在学习。我尝试更改函数名称,如addGenError
而不是setGenError
,行为是相同的。任何帮助将不胜感激。
答案 0 :(得分:0)
这就是问题所在:
在发送表单数据时,我正在使用bean id:loginFormData
,它被设置为会话范围。在将其更改为请求后,我获得了所需的功能。
更改了代码:
<jsp:useBean id="loginFormData" class = "com.skf.setup.data.form.loginForm" scope="request" />