Full Web Application Tomcat JSF Primefaces JPA Hibernate – Part 2
ManagedBeans
This post continues from part 1 of this tutorial.
In the “com.mb” package you will need to create the classes bellow:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.mb; import org.primefaces.context.RequestContext; import com.util.JSFMessageUtil; public class AbstractMB { private static final String KEEP_DIALOG_OPENED = 'KEEP_DIALOG_OPENED' ; public AbstractMB() { super (); } protected void displayErrorMessageToUser(String message) { JSFMessageUtil messageUtil = new JSFMessageUtil(); messageUtil.sendErrorMessageToUser(message); } protected void displayInfoMessageToUser(String message) { JSFMessageUtil messageUtil = new JSFMessageUtil(); messageUtil.sendInfoMessageToUser(message); } protected void closeDialog(){ getRequestContext().addCallbackParam(KEEP_DIALOG_OPENED, false ); } protected void keepDialogOpen(){ getRequestContext().addCallbackParam(KEEP_DIALOG_OPENED, true ); } protected RequestContext getRequestContext(){ return RequestContext.getCurrentInstance(); } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | package com.mb; import java.io.Serializable; import java.util.List; import javax.faces.bean.*; import com.facade.DogFacade; import com.model.Dog; @ViewScoped @ManagedBean public class DogMB extends AbstractMB implements Serializable { private static final long serialVersionUID = 1L; private Dog dog; private List<Dog> dogs; private DogFacade dogFacade; public DogFacade getDogFacade() { if (dogFacade == null ) { dogFacade = new DogFacade(); } return dogFacade; } public Dog getDog() { if (dog == null ) { dog = new Dog(); } return dog; } public void setDog(Dog dog) { this .dog = dog; } public void createDog() { try { getDogFacade().createDog(dog); closeDialog(); displayInfoMessageToUser( 'Created With Sucess' ); loadDogs(); resetDog(); } catch (Exception e) { keepDialogOpen(); displayErrorMessageToUser( 'Ops, we could not create. Try again later' ); e.printStackTrace(); } } public void updateDog() { try { getDogFacade().updateDog(dog); closeDialog(); displayInfoMessageToUser( 'Updated With Sucess' ); loadDogs(); resetDog(); } catch (Exception e) { keepDialogOpen(); displayErrorMessageToUser( 'Ops, we could not create. Try again later' ); e.printStackTrace(); } } public void deleteDog() { try { getDogFacade().deleteDog(dog); closeDialog(); displayInfoMessageToUser( 'Deleted With Sucess' ); loadDogs(); resetDog(); } catch (Exception e) { keepDialogOpen(); displayErrorMessageToUser( 'Ops, we could not create. Try again later' ); e.printStackTrace(); } } public List<Dog> getAllDogs() { if (dogs == null ) { loadDogs(); } return dogs; } private void loadDogs() { dogs = getDogFacade().listAll(); } public void resetDog() { dog = new Dog(); } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package com.mb; import java.io.Serializable; import javax.faces.bean.*; import javax.faces.context.FacesContext; import javax.servlet.http.HttpServletRequest; import com.model.User; @SessionScoped @ManagedBean (name= 'userMB' ) public class UserMB implements Serializable { public static final String INJECTION_NAME = '#{userMB}' ; private static final long serialVersionUID = 1L; private User user; public boolean isAdmin() { return user.isAdmin(); } public boolean isDefaultUser() { return user.isUser(); } public String logOut() { getRequest().getSession().invalidate(); return '/pages/public/login.xhtml' ; } private HttpServletRequest getRequest() { return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); } public User getUser() { return user; } public void setUser(User user) { this .user = user; } } |
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | package com.mb; import java.io.Serializable; import java.util.*; import javax.faces.bean.*; import com.facade.*; import com.model.*; import com.sun.faces.context.flash.ELFlash; @ViewScoped @ManagedBean public class PersonMB extends AbstractMB implements Serializable { private static final long serialVersionUID = 1L; private static final String SELECTED_PERSON = 'selectedPerson' ; private Dog dog; private Person person; private Person personWithDogs; private Person personWithDogsForDetail; private List<Dog> allDogs; private List<Person> persons; private DogFacade dogFacade; private PersonFacade personFacade; public void createPerson() { try { getPersonFacade().createPerson(person); closeDialog(); displayInfoMessageToUser( 'Created With Sucess' ); loadPersons(); resetPerson(); } catch (Exception e) { keepDialogOpen(); displayErrorMessageToUser( 'Ops, we could not create. Try again later' ); e.printStackTrace(); } } public void updatePerson() { try { getPersonFacade().updatePerson(person); closeDialog(); displayInfoMessageToUser( 'Updated With Sucess' ); loadPersons(); resetPerson(); } catch (Exception e) { keepDialogOpen(); displayErrorMessageToUser( 'Ops, we could not create. Try again later' ); e.printStackTrace(); } } public void deletePerson() { try { getPersonFacade().deletePerson(person); closeDialog(); displayInfoMessageToUser( 'Deleted With Sucess' ); loadPersons(); resetPerson(); } catch (Exception e) { keepDialogOpen(); displayErrorMessageToUser( 'Ops, we could not create. Try again later' ); e.printStackTrace(); } } public void addDogToPerson() { try { getPersonFacade().addDogToPerson(dog.getId(), personWithDogs.getId()); closeDialog(); displayInfoMessageToUser( 'Added With Sucess' ); reloadPersonWithDogs(); resetDog(); } catch (Exception e) { keepDialogOpen(); displayErrorMessageToUser( 'Ops, we could not create. Try again later' ); e.printStackTrace(); } } public void removeDogFromPerson() { try { getPersonFacade().removeDogFromPerson(dog.getId(), personWithDogs.getId()); closeDialog(); displayInfoMessageToUser( 'Removed With Sucess' ); reloadPersonWithDogs(); resetDog(); } catch (Exception e) { keepDialogOpen(); displayErrorMessageToUser( 'Ops, we could not create. Try again later' ); e.printStackTrace(); } } public Person getPersonWithDogs() { if (personWithDogs == null ) { if (person == null ) { person = (Person) ELFlash.getFlash().get(SELECTED_PERSON); } personWithDogs = getPersonFacade().findPersonWithAllDogs(person.getId()); } return personWithDogs; } public void setPersonWithDogsForDetail(Person person) { personWithDogsForDetail = getPersonFacade().findPersonWithAllDogs(person.getId()); } public Person getPersonWithDogsForDetail() { if (personWithDogsForDetail == null ) { personWithDogsForDetail = new Person(); personWithDogsForDetail.setDogs( new ArrayList<Dog>()); } return personWithDogsForDetail; } public void resetPersonWithDogsForDetail(){ personWithDogsForDetail = new Person(); } public String editPersonDogs() { ELFlash.getFlash().put(SELECTED_PERSON, person); return '/pages/protected/defaultUser/personDogs/personDogs.xhtml' ; } public List<Dog> complete(String name) { List<Dog> queryResult = new ArrayList<Dog>(); if (allDogs == null ) { dogFacade = new DogFacade(); allDogs = dogFacade.listAll(); } allDogs.removeAll(personWithDogs.getDogs()); for (Dog dog : allDogs) { if (dog.getName().toLowerCase().contains(name.toLowerCase())) { queryResult.add(dog); } } return queryResult; } public PersonFacade getPersonFacade() { if (personFacade == null ) { personFacade = new PersonFacade(); } return personFacade; } public Person getPerson() { if (person == null ) { person = new Person(); } return person; } public void setPerson(Person person) { this .person = person; } public List<Person> getAllPersons() { if (persons == null ) { loadPersons(); } return persons; } private void loadPersons() { persons = getPersonFacade().listAll(); } public void resetPerson() { person = new Person(); } public Dog getDog() { if (dog == null ) { dog = new Dog(); } return dog; } public void setDog(Dog dog) { this .dog = dog; } public void resetDog() { dog = new Dog(); } private void reloadPersonWithDogs() { personWithDogs = getPersonFacade().findPersonWithAllDogs(person.getId()); } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package com.mb; import javax.faces.bean.*; import javax.faces.context.FacesContext; import javax.servlet.http.HttpServletRequest; import com.facade.UserFacade; import com.model.User; @RequestScoped @ManagedBean public class LoginMB extends AbstractMB { @ManagedProperty (value = UserMB.INJECTION_NAME) private UserMB userMB; private String email; private String password; public String getEmail() { return email; } public void setEmail(String email) { this .email = email; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } public String login() { UserFacade userFacade = new UserFacade(); User user = userFacade.isValidLogin(email, password); if (user != null ){ userMB.setUser(user); FacesContext context = FacesContext.getCurrentInstance(); HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest(); request.getSession().setAttribute( 'user' , user); return '/pages/protected/index.xhtml' ; } displayErrorMessageToUser( 'Check your email/password' ); return null ; } public void setUserMB(UserMB userMB) { this .userMB = userMB; } } |
About the above code:
- All ManagedBeans are responsible only for the VIEW actions; the ManagedBeans should be responsible to handle the only outcome of the business methods. There are no business rules in the ManagedBeans. It is very easy to do some business actions in the view layer but is not a good practice.
- The LoginMB class uses another ManagedBean (UserMB) there were injected inside of it. To inject a ManagedBean inside another ManagedBean you must do as bellow:
- Uses the @ManagedProperty on the top of the injected ManagedBean
- Create a set method to the property like “loginMB.setUserMB(…)“
- The PersonMB class could receive refactoring actions because it is too big. The PersonMB is like that to make easer for rookie developers to understand the code faster.
Observations about @ViewScoped
You will see in the managed beans with the @ViewScoped annotation some reload and reset methods. Both methods are required to reset the objects state; e.g. a dog object would hold values from the view after a method execution (persist in the database, display values in a dialog). If the user open de create dialog and successfully create a dog this dog object will hold all values while the user stays in the same page. If the user opens the create dialog again all the data of the last recorded dog will be displayed there. That is why we have the reset methods.
If you update an object in the database the object in the user view must receive this update too, the ManagedBean objects must receive this new data. If you updated a dog name in the database the list of dog should receive this updated dog too. You can query this new data in the database or just update the managed bean values.
A developer must be aware of:
- Reload the managed bean data querying the database (the reload methods): if the fired query to reload the ManagedBean object comes with a huge amount of data his query may affect the application performance. A developer could use a datatable with lazy load. Click here to see more about Lazy Datatable.
- Reload the updated object directly in the managed bean without querying the database: imagine that the user1 updates the dog1 name in the database and at the same time user2 updates the dog2 age. The user1 will see the old data about the dog2 that could cause a database integrity issue if the user1 updates the dog2. A solution to this approach could be a version field in the database table. Before the update takes place this field would be checked. If the version field does not hold the same value found in the database an exception could be raised. With this approach if the user1 updates the dog2 the version value would not be the same.
JSFMessageUtil
In the package “com.util” create the class bellow:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.util; import javax.faces.application.FacesMessage; import javax.faces.application.FacesMessage.Severity; import javax.faces.context.FacesContext; public class JSFMessageUtil { public void sendInfoMessageToUser(String message) { FacesMessage facesMessage = createMessage(FacesMessage.SEVERITY_INFO, message); addMessageToJsfContext(facesMessage); } public void sendErrorMessageToUser(String message) { FacesMessage facesMessage = createMessage(FacesMessage.SEVERITY_WARN, message); addMessageToJsfContext(facesMessage); } private FacesMessage createMessage(Severity severity, String mensagemErro) { return new FacesMessage(severity, mensagemErro, mensagemErro); } private void addMessageToJsfContext(FacesMessage facesMessage) { FacesContext.getCurrentInstance().addMessage( null , facesMessage); } } |
This class will handle all JSF Messages that will be displayed to the user. This class will help our ManagedBeans to lose coupling between the classes.
It is also a good idea to create a class to handle the dialogs actions.
Configurations file
In the source “src” folder create the following files:
“log4.properties”
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | # Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n # Root logger option log4j.rootLogger=ERROR, stdout # Hibernate logging options (INFO only shows startup messages) log4j.logger.org.hibernate=ERROR # Log JDBC bind parameter runtime arguments #log4j.logger.org.hibernate.type=TRACE |
“messages.properties”
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #Actions welcomeMessage=Hello! Show me the best soccer team logo ever update=Update create=Create delete=Delete cancel=Cancel detail=Detail logIn=Log In add=Add remove=Remove ok=Ok logOut= Log Out javax.faces.component.UIInput.REQUIRED={ 0 }: is empty. Please, provide some value javax.faces.validator.LengthValidator.MINIMUM={ 1 }: Length is less than allowable minimum of u2018u2019{ 0 }u2019u2019 noRecords=No data to display deleteRecord=Do you want do delete the record #Login / Roles Validations loginHello=Login to access secure pages loginUserName=Username loginPassword=Password logout=Log Out loginWelcomeMessage=Welcome accessDeniedHeader=Wow, our ninja cat found you! accessDeniedText=Sorry but you can not access that page. If you try again, that ninja cat gonna kick you harder! >= ) accessDeniedButton=You got-me, take me out. =/ #Person person=Person personPlural=Persons personName=Name personAge=Age personDogs=These dogs belongs to personAddDogTo=Add the selected Dog To personRemoveDogFrom=Remove the selected Dog from personEditDogs=Edit Dogs #Dog dog=Dog dogPlural=Dogs dogName=Name dogAge=Age |
Take a look at the “lo4j.properties” the line #log4j.logger.org.hibernate.type=TRACE is commented. If you want to see the created query by the Hibernate you need to edit other configurations of the file from ERROR to DEBUG and remove the # from the line above.
You will be able to see the Hibernate executed query and its parameters.
xhtml Pages, Facelets
Inside the WebContent folder you will find the following files:
Let us see how to apply Facelets to a project. Create the files bellow inside the folder “/WebContent/pages/protected/templates/”:
“left.xhtml”
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' <html xmlns= 'http://www.w3.org/1999/xhtml' <h:body> <ui:composition> <h:form> <p:commandButton styleClass= 'menuButton' icon= 'ui-icon-arrowstop-1-e' rendered= '#{userMB.admin or userMB.defaultUser}' action= '/pages/protected/defaultUser/defaultUserIndex.xhtml' value= '#{bundle.personPlural}' ajax= 'false' immediate= 'true' /> <br /> <p:commandButton styleClass= 'menuButton' icon= 'ui-icon-arrowstop-1-e' rendered= '#{userMB.admin}' action= '/pages/protected/admin/adminIndex.xhtml' value= '#{bundle.dogPlural}' ajax= 'false' immediate= 'true' /> <br /> </h:form> </ui:composition> </h:body> </html> |
“master.xhtml”
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?xml version= '1.0' encoding= 'UTF-8' ?> <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' <html xmlns= 'http://www.w3.org/1999/xhtml' <h:head> <title>CrudJSF</title> <h:outputStylesheet library= 'css' name= 'main.css' /> <meta http-equiv= 'Content-Type' content= 'text/html; charset=UTF-8' /> </h:head> <h:body> <f:view contentType= 'text/html; charset=UTF-8' encoding= 'UTF-8' > <div id= 'divTop' style= 'vertical-align: middle;' > <ui:insert name= 'divTop' > <ui:include src= 'top.xhtml' /> </ui:insert> </div> <div id= 'divLeft' > <ui:insert name= 'divLeft' > <ui:include src= 'left.xhtml' /> </ui:insert> </div> <div id= 'divMain' > <p:growl id= 'messageGrowl' /> <ui:insert name= 'divMain' /> </div> <h:outputScript library= 'javascript' name= 'jscodes.js' /> </f:view> </h:body> </html> |
“top.xhtml”
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' <html xmlns= 'http://www.w3.org/1999/xhtml' <h:body> <ui:composition> <div id= 'topMessage' > <h1> <h:form> #{bundle.loginWelcomeMessage}: #{userMB.user.name} | <p:commandButton value= '#{bundle.logOut}' action= '#{userMB.logOut()}' ajax= 'false' style= 'font-size: 20px;' /> </h:form> </h1> </div> </ui:composition> </h:body> </html> |
The above code will be the base for all the xhtml pages of the application. It is very important to apply the Facelets pattern to re-use the xhtml code. Bellow you can see how to apply Facelets in the xhtml page, notice that the developer just need to overwrite the desired section:
01 02 03 04 05 06 07 08 09 10 11 12 | <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd' > <html xmlns= 'http://www.w3.org/1999/xhtml' xmlns:ui= 'http://java.sun.com/jsf/facelets' xmlns:h= 'http://java.sun.com/jsf/html' <h:body> <ui:composition template= '/pages/protected/templates/master.xhtml' > <ui:define name= 'divMain' > #{bundle.welcomeMessage} :<br/> <h:graphicImage library= 'images' name= 'logoReal.png' /> </ui:define> </ui:composition> </h:body> </html> |
Notice that only the “divMain” were overwritten and the other sections remained the same. That is the greatest advantage of the Facelets, you do not need to use the include all areas of the site in every page.
Continue to the third part.
Reference: Full Web Application with Tomcat JSF Primefaces JPA Hibernate from our JCG partner Hebert Coelho at the uaiHebert blog.