"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!
You need to bind the first component by binding and pass it as attribute of the component you're validating. You also need to use validator attribute of the input field instead of f:validator when you want to call a validator method inside a managed bean. Finally you should get rid of the implements Validator on the bean class.
Up vote 1 down vote favorite share g+ share fb share tw.
I need a multiple field validation for login page(many fields validated at once in one same method). I don't know how to implement it correctly. I am following an example I found at: balusc.blogspot.com/2007/12/validator-fo... I get a bit confused in the JSF part.
Can somebody give me a hand, what I am missing? The page: This is the managed bean with the validate method: @ManagedBean @RequestScoped public class SecurityController implements Validator { @EJB private IAuthentificationEJB authentificationEJB; private String email; private String password; private String notificationValue; public String logIn() { if (authentificationEJB. SaveUserState(email, password)) { notificationValue = "Dobro dosli"; return "main.
Xhtml"; } else { return ""; } } public void validate(FacesContext context, UIComponent validate, Object value) { String emailInput = (String) value; String emailPatternText = "^_A-Za-z0-9-+(\\. _A-Za-z0-9-+)*@A-Za-z0-9+(\\. A-Za-z0-9+)*(\\.
A-Za-z{2,})$"; Pattern emailPattern = null; Matcher emailMatcher = null; emailPattern = Pattern. Compile(emailPatternText); emailMatcher = emailPattern. Matcher(emailInput); String inputFromField = (String) value; String alphanumericPattern = "^a-zA-Z0-9+$"; Pattern passwordPattern = null; Matcher passwordMatcher = null; passwordPattern = Pattern.
Compile(alphanumericPattern); passwordMatcher = passwordPattern. Matcher(inputFromField); if (!emailMatcher.matches() &&!passwordMatcher.matches()) { if (authentificationEJB. CheckCredentials(email,password) == false) { FacesMessage msg = new FacesMessage( "Pogresan email ili lozinka"); throw new ValidatorException(msg); } } } public String getEmail() { return email; } public String getPassword() { return password; } public void setEmail(String email) { this.
Email = email; } public void setPassword(String password) { this. Password = password; } public String getNotificationValue() { return notificationValue; } public void setNotificationValue(String notificationValue) { this. NotificationValue = notificationValue; } EJB that interacts with database to check credentials: @Stateful(name = "ejbs/AuthentificationEJB") public class AuthentificationEJB implements IAuthentificationEJB { @PersistenceContext private EntityManager em; // Login public boolean saveUserState(String email, String password) { // 1-Send query to database to see if that user exist Query query = em .
CreateQuery("SELECT r FROM Role r WHERE r. Email=:emailparam AND r. Password=:passwordparam"); query.
SetParameter("emailparam", email); query. SetParameter("passwordparam", password); // 2-If the query returns the user(Role) object, store it somewhere in // the session Role role = (Role) query.getSingleResult(); if (role! = null && role.getEmail().
Equals(email) && role.getPassword(). Equals(password)) { FacesContext. GetCurrentInstance().
GetExternalContext() .getSessionMap(). Put("userRole", role); // 3-return true if the user state was saved System.out. Println(role.getEmail() + role.getPassword()); return true; } // 4-return false otherwise System.out.
Println(role.getEmail() + role.getPassword()); return false; } // Logout public void releaseUserState() { // 1-Check if there is something saved in the session(or wherever the // state is saved) if (!FacesContext. GetCurrentInstance(). GetExternalContext() .getSessionMap().isEmpty()) { FacesContext.
GetCurrentInstance().release(); } // 2-If 1 then flush it } // Check if user is logged in public boolean checkAuthentificationStatus() { // 1-Check if there is something saved in the session(This means the // user is logged in) if ((FacesContext. GetCurrentInstance(). GetExternalContext() .getSessionMap().
Get("userRole")! = null)) { // 2-If there is not a user already loged, then return false return true; } return false; } @Override public boolean checkCredentials(String email,String password) { Query checkEmailExists = em . CreateQuery("SELECT COUNT(r.
Email) FROM Role r WHERE r. Email=:emailparam AND r. Password=:passwordparam"); checkEmailExists.
SetParameter("emailparam", email); checkEmailExists. SetParameter("passwordparam", password); long matchCounter = 0; matchCounter = (Long) checkEmailExists.getSingleResult(); if (matchCounter > 0) { return true; } return false; } } Update Removed LoginValidator The modified managed bean: @ManagedBean @RequestScoped public class SecurityController { @EJB private IAuthentificationEJB authentificationEJB; private String email; private String password; private String notificationValue; public String logIn() { I f (authentificationEJB. SaveUserState(email, password)) { notificationValue = "Dobro dosli"; return "main.
Xhtml"; } else { return ""; } } public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { UIInput emailComponent = (UIInput) component.getAttributes(). Get( "emailComponent"); String email = ""; String password = ""; email = (String) emailComponent.getValue(); password = (String) value; String emailInput = email; String emailPatternText = "^_A-Za-z0-9-+(\\. _A-Za-z0-9-+)*@A-Za-z0-9+(\\.
A-Za-z0-9+)*(\\. A-Za-z{2,})$"; Pattern emailPattern = null; Matcher emailMatcher = null; emailPattern = Pattern. Compile(emailPatternText); emailMatcher = emailPattern.
Matcher(emailInput); String passwordInput = password; String alphanumericPattern = "^a-zA-Z0-9+$"; Pattern passwordPattern = null; Matcher passwordMatcher = null; passwordPattern = Pattern. Compile(alphanumericPattern); passwordMatcher = passwordPattern. Matcher(passwordInput); if (!emailMatcher.matches() &&!passwordMatcher.matches()) { if (authentificationEJB.
CheckCredentials(emailInput, passwordInput) == false) { FacesMessage msg = new FacesMessage( "Pogresan email ili lozinka"); throw new ValidatorException(msg); } } if(emailInput == null || passwordInput == null) { FacesMessage msg = new FacesMessage("Zaboraviliste nesto"); throw new ValidatorException(msg); } if (passwordInput.length() Modified The saveUserState() method at the EJB: // Login public boolean saveUserState(String email, String password) { // 1-Send query to database to see if that user exist Query query = em . CreateQuery("SELECT r FROM Role r WHERE r. Email=:emailparam AND r.
Password=:passwordparam"); query. SetParameter("emailparam", email); query. SetParameter("passwordparam", password); // 2-If the query returns the user(Role) object, store it somewhere in // the session try { Role role = (Role) query.getSingleResult(); if (role!
= null && role.getEmail(). Equals(email) && role.getPassword(). Equals(password)) { FacesContext.
GetCurrentInstance(). GetExternalContext() .getSessionMap(). Put("userRole", role); // 3-return true if the user state was saved System.out.
Println(role.getEmail() + role.getPassword()); return true; } } catch (Exception e) { //This fix the bug that does not display the message when wrong password! FacesMessage msg = new FacesMessage("Pogresan email ili lozinka"); throw new ValidatorException(msg); } // 4-return false otherwise return false; } java jsf java-ee jsf-2.0 java-ee-6 link|improve this question edited Apr 13 '11 at 16:16 asked Apr 13 '11 at 14:14sfrj2,684948 100% accept rate.
You need to bind the first component by binding and pass it as attribute of the component you're validating. You also need to use validator attribute of the input field instead of when you want to call a validator method inside a managed bean. Finally you should get rid of the implements Validator on the bean class.
With public void validateEmailAndPassword(FacesContext context, UIComponent component, Object value) throws ValidatorException { UIInput emailComponent = (UIInput) component.getAttributes(). Get("emailComponent"); String email = (String) emailComponent.getValue(); String password = (String) value; // ... }.
I added the LoginValidator as an standalone class not a managed bean, and I removed the validation method from the managed bean. I get a NullPointerException at the LoginValidator where it says:String password = (String) value; Why is that? Is it ok that I injected the EJB in this class too?
– sfrj Apr 13 '11 at 14:49 That line can impossibly throw a NPE. Isn't it another line or did you mean that the value is null? If the latter, just add a if (password!
= null) check before accessing/manipulating it. – BalusC Apr 13 '11 at 14:53 Sorry it is line 27 that throws NPE: String email = emailComponent.getValue().toString(); – sfrj Apr 13 '11 at 14:56 Then the emailComponent is null. Did you bind it properly by binding="#{emailComponent}"?
– BalusC Apr 13 '11 at 14:57 No I didn't I forgot it. I just added the binding. It works fine now butonly when I give a correct credentials, when credentials are wrong ore the email field is empty I get NPE – sfrj Apr 13 '11 at 15:08.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.