If a user can't remember even the username, he/she just has to access this page and insert their email, and he/she will receive an email with the username information.
This feature is very similar to the reset password one, so we will just present the code to be added along with a few comments.
First, add another support bean called ForgotUsernameHelper
to the book.richfaces.advcm.modules.login
package using the following code:
@Name("forgotUsernameHelper") public class ForgotUsernameHelper { forgot username boxforgot username boxForgotUsernameHelper bean, adding@In(create = true) Renderer renderer; @In(create = true) EntityManager entityManager; @In FacesMessages facesMessages; private String usernameFound; private String email; public void sendUsername() { try { String query = "select c from Contact c where c.email =:email"; Contact actor = (Contact) (entityManager. createQuery(query). setParameter("email", getEmail()).getSingleResult()); // Saving the username (it will be used into the // email) setUsernameFound(actor.getUsername()); // Sending the email renderer.render("/mailTemplates/forgotUsernameMail.xhtml"); facesMessages.add(Severity.INFO, "#{messages['newUsernameSent']}"); // Empty the data setEmail(null); setUsernameFound(null); } catch (NoResultException e) { facesMessages.addToControl("email", Severity.ERROR, "#{messages['emailNotFound']}"); } } @NotNull @Email public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getUsernameFound() { return usernameFound; } public void setUsernameFound(String forgotPwdNewPassword) { this.usernameFound = forgotPwdNewPassword; } }
This code is the same as that for ResetPwdHelper
(the reset password feature) we just get an email and not the username. Therefore, we also added the Hibernate validator @Email
annotation in order to inform JSF that this might be a well-formed email address.
Now let's create the forgotUsername.xhtml
page inside the /view/user/
directory (use home.xhtml
as the template) and add the following code inside its body definition:
<h:form id="login"> <rich:panel> <f:facet name="header"> #{messages['forgotUsername']} </f:facet> <div class="dialog"> <h:panelGrid columns="3" rowClasses="prop" columnClasses="name,value,validatormsg"> <h:outputLabel for="email"> #{messages['email']}: </h:outputLabel> <h:inputText id="email" value="#{forgotUsernameHelper.email}"> <rich:beanValidator /> </h:inputText> <rich:message for="email" styleClass="messagesingle" errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"> </rich:message> </h:panelGrid> </div> </rich:panel> <div class="actionButtons"> <a:commandButton value="#{messages['sendUsername']}" action="#{forgotUsernameHelper.sendUsername}" reRender="email"> <s:defaultAction /> </a:commandButton> </div> </h:form>
Also, in this case, the code is very similar to the one we've seen for the resetPassword.xhtml
page.
Now the email template: let's add a new file called forgotUsernameMail.xhtml
into the /view/mailTemplates/
directory. Open this new file and add the following code:
<m:message xmlns="http://www.w3.org/1999/xhtml" xmlns:m="http://jboss.com/products/seam/mail" xmlns:h="http://java.sun.com/jsf/html"> <m:from name="Advanced Contact Manager administration"> [email protected] </m:from> <m:to name="#{forgotUsernameHelper.email}"> #{forgotUsernameHelper.email} </m:to> <m:subject> #{messages['forgotUsernameMailSubject']} </m:subject> <m:body> <p> <h:outputText value="#{messages['forgotUsernameMailContent']}" /> <br/><br/> <h:outputText value="#{messages['forgotUsernameMailUsernameLabel']}: "/> <h:outputText value="#{forgotUsernameHelper. forgotUsernameNewPassword}" style="font-weight: bold;" /> </p> </m:body> </m:message>
Nothing new here too we have created a new feature in a very simple way! Here is the screenshot:
3.144.109.34