Full WebApplication JSF EJB JPA JAAS

69

Posted on January 2, 2012 by

Share it now!

View – Creation and JSF set up

Let us create a new Dynamic Web Project. Create it like the image bellow:

Pay attention: in some moment the Eclipse will ask you if you want to add the JSF Capabilities (auto complete), enable it. Like the screens bellow:

After the project creation, let us edit the “web.xml” file; it should have the same code as bellow:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>CrudJSF</display-name>
	<welcome-file-list>
		<welcome-file>pages/protected/user/listAllDogs.xhtml</welcome-file>
	</welcome-file-list>
	<servlet>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>/faces/*</url-pattern>
		<url-pattern>*.jsf</url-pattern>
		<url-pattern>*.xhtml</url-pattern>
	</servlet-mapping>

	<!-- Protected area definition -->
	<security-constraint>
		<web-resource-collection>
			<web-resource-name>Restricted Area - ADMIN Only</web-resource-name>
			<url-pattern>/pages/protected/admin/*</url-pattern>
		</web-resource-collection>
		<auth-constraint>
			<role-name>ADMIN</role-name>
		</auth-constraint>
	</security-constraint>
	<security-constraint>
		<web-resource-collection>
			<web-resource-name>Restricted Area - USER and ADMIN</web-resource-name>
			<url-pattern>/pages/protected/user/*</url-pattern>
		</web-resource-collection>
		<auth-constraint>
			<role-name>USER</role-name>
			<role-name>ADMIN</role-name>
		</auth-constraint>
	</security-constraint>

	<!-- Login page -->
	<login-config>
		<auth-method>FORM</auth-method>
		<form-login-config>
			<form-login-page>/pages/public/login.xhtml</form-login-page>
			<form-error-page>/pages/public/loginError.xhtml</form-error-page>
		</form-login-config>
	</login-config>

	<!-- System roles -->
	<security-role>
		<role-name>ADMIN</role-name>
	</security-role>
	<security-role>
		<role-name>USER</role-name>
	</security-role>
</web-app>

You do not have to worry if some warning/error shows up; we will solve them later. Notice that I have added all the JAAS code that we will need (If you want a detailed post about these JAAS configurations you can check it here: User Login Validation with JAAS and JSF).

According to the JAAS configurations a regular user (USER role) will only see the files inside the user folder, that will be only the listing of the dogs recorded in our database; the ADMIN will be able to do all the CRUD actions because all the pages are inside the admins folder.

Our “faces-config.xml” should have the code bellow:

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">

	<navigation-rule>
		<navigation-case>
			<from-outcome>logout</from-outcome>
			<to-view-id>/pages/protected/user/listAllDogs.xhtml</to-view-id>
			<redirect/>
		</navigation-case>
	</navigation-rule>
	<navigation-rule>
		<navigation-case>
			<from-outcome>listAllDogs</from-outcome>
			<to-view-id>/pages/protected/user/listAllDogs.xhtml</to-view-id>
		</navigation-case>
	</navigation-rule>
	<navigation-rule>
		<navigation-case>
			<from-outcome>createDog</from-outcome>
			<to-view-id>/pages/protected/admin/createDog.xhtml</to-view-id>
			<redirect/>
		</navigation-case>
	</navigation-rule>
	<navigation-rule>
		<navigation-case>
			<from-outcome>updateDog</from-outcome>
			<to-view-id>/pages/protected/admin/updateDog.xhtml</to-view-id>
		</navigation-case>
	</navigation-rule>
	<navigation-rule>
		<navigation-case>
			<from-outcome>deleteDog</from-outcome>
			<to-view-id>/pages/protected/admin/deleteDog.xhtml</to-view-id>
		</navigation-case>
	</navigation-rule>

	<application>
		<resource-bundle>
			<base-name>messages</base-name>
			<var>msgs</var>
		</resource-bundle>
	</application>

</faces-config>

Notice that to some actions I used the redirect action. With this action we will update the requested link in the URL bar of the browser, after the URL get updated the JAAS will deny access to an illegal user.

We also have a file that will contain all the messages of our system. You will notice that all the texts displayed in our pages are in this file (create a file named “messages.properties” inside the src folder):

#Dog
dog=Dog
dogName=Name
dogWeight=Weight

#Dog messages
dogCreateHeader=Create a new Dog
dogUpdateHeader=Update the Dog
dogDeleteHeader=Delete this Dog
dogNameRequired=The dog needs a name.
dogWeightRequired=The dog needs a weight.

#Actions
update=Update
create=Create
delete=Delete
cancel=Cancel

#Login
loginHello=Hello
loginErrorMessage=Could not login. Check you UserName/Password
loginUserName=Username
loginPassword=Password
logout=Log Out

Response to Full WebApplication JSF EJB JPA JAAS

  1. uddy

    Great tuto but I’ve this errors can you help me.

    Thanks.!!

    15:33:23,897 INFO [org.jboss.modules] JBoss Modules version 1.1.1.GA
    15:33:24,186 INFO [org.jboss.msc] JBoss MSC version 1.0.2.GA
    15:33:24,256 INFO [org.jboss.as] JBAS015899: JBoss AS 7.1.1.Final “Brontes” starting
    15:33:25,084 ERROR [org.jboss.as.server] JBAS015956: Caught exception during boot: org.jboss.as.controller.persistence.ConfigurationPersistenceException: JBAS014676: Failed to parse configuration
    at org.jboss.as.controller.persistence.XmlConfigurationPersister.load(XmlConfigurationPersister.java:141) [jboss-as-controller-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.server.ServerService.boot(ServerService.java:266) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.controller.AbstractControllerService$1.run(AbstractControllerService.java:155) [jboss-as-controller-7.1.1.Final.jar:7.1.1.Final]
    at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_26]
    Caused by: javax.xml.stream.XMLStreamException: com.ctc.wstx.exc.WstxParsingException: Duplicate attribute ‘use-java-context’.
    at [row,col {unknown-source}]: [105,154]
    at org.jboss.as.connector.subsystems.datasources.DataSourcesExtension$DataSourceSubsystemParser.readElement(DataSourcesExtension.java:784)
    at org.jboss.as.connector.subsystems.datasources.DataSourcesExtension$DataSourceSubsystemParser.readElement(DataSourcesExtension.java:333)
    at org.jboss.staxmapper.XMLMapperImpl.processNested(XMLMapperImpl.java:110) [staxmapper-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.staxmapper.XMLExtendedStreamReaderImpl.handleAny(XMLExtendedStreamReaderImpl.java:69) [staxmapper-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.server.parsing.StandaloneXml.parseServerProfile(StandaloneXml.java:894) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.server.parsing.StandaloneXml.readServerElement_1_1(StandaloneXml.java:330) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.server.parsing.StandaloneXml.readElement(StandaloneXml.java:127) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.server.parsing.StandaloneXml.readElement(StandaloneXml.java:100) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.staxmapper.XMLMapperImpl.processNested(XMLMapperImpl.java:110) [staxmapper-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.staxmapper.XMLMapperImpl.parseDocument(XMLMapperImpl.java:69) [staxmapper-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.controller.persistence.XmlConfigurationPersister.load(XmlConfigurationPersister.java:133) [jboss-as-controller-7.1.1.Final.jar:7.1.1.Final]
    … 3 more
    Caused by: com.ctc.wstx.exc.WstxParsingException: Duplicate attribute ‘use-java-context’.
    at [row,col {unknown-source}]: [105,154]
    at com.ctc.wstx.sr.StreamScanner.constructWfcException(StreamScanner.java:606)
    at com.ctc.wstx.sr.StreamScanner.throwParseError(StreamScanner.java:479)
    at com.ctc.wstx.sr.StreamScanner.throwParseError(StreamScanner.java:464)
    at com.ctc.wstx.sr.AttributeCollector.throwDupAttr(AttributeCollector.java:1143)
    at com.ctc.wstx.sr.AttributeCollector.resolveNamespaces(AttributeCollector.java:997)
    at com.ctc.wstx.sr.InputElementStack.resolveAndValidateElement(InputElementStack.java:498)
    at com.ctc.wstx.sr.BasicStreamReader.handleStartElem(BasicStreamReader.java:2942)
    at com.ctc.wstx.sr.BasicStreamReader.nextFromTree(BasicStreamReader.java:2802)
    at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1050)
    at com.ctc.wstx.sr.BasicStreamReader.nextTag(BasicStreamReader.java:1125)
    at org.jboss.staxmapper.XMLExtendedStreamReaderImpl.nextTag(XMLExtendedStreamReaderImpl.java:152) [staxmapper-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.connector.subsystems.datasources.DsParser.parseDataSources(DsParser.java:185)
    at org.jboss.as.connector.subsystems.datasources.DsParser.parse(DsParser.java:165)
    at org.jboss.as.connector.subsystems.datasources.DataSourcesExtension$DataSourceSubsystemParser.readElement(DataSourcesExtension.java:776)
    … 13 more

    15:33:25,114 FATAL [org.jboss.as.server] JBAS015957: Server boot has failed in an unrecoverable manner; exiting. See previous messages for details.
    15:33:25,137 INFO [org.jboss.as] JBAS015950: JBoss AS 7.1.1.Final “Brontes” stopped in 14ms

    • uaihebert Post author

      Hello uddy,

      It looks like you edited a xml with an error. Check the edited xml.

      Thanks for passing by. I am sorry for the late response, I was very busy this last month.

  2. WarKar

    Hello,

    You’ve created great tutorial, but I have problem with the last step – deploy CrudEAR project. This is the console output:


    20:57:07,839 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-8) MSC00001: Failed to start service jboss.deployment.subunit."CrudEAR.ear"."CrudJSF.war".INSTALL: org.jboss.msc.service.StartException in service jboss.deployment.subunit."CrudEAR.ear"."CrudJSF.war".INSTALL: Failed to process phase INSTALL of subdeployment "CrudJSF.war" of deployment "CrudEAR.ear"
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:121) [jboss-as-server-7.0.2.Final.jar:7.0.2.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1824) [jboss-msc-1.0.1.GA.jar:1.0.1.GA]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1759) [jboss-msc-1.0.1.GA.jar:1.0.1.GA]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [:1.7.0_45]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [:1.7.0_45]
    at java.lang.Thread.run(Unknown Source) [:1.7.0_45]
    Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: No component found for type 'com.facade.UserFacade' with name null
    at org.jboss.as.ejb3.deployment.processors.EjbInjectionSource.getResourceValue(EjbInjectionSource.java:68)
    at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor.addJndiBinding(ModuleJndiBindingProcessor.java:200)
    at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor$1.handle(ModuleJndiBindingProcessor.java:168)
    at org.jboss.as.ee.component.ClassDescriptionTraversal.run(ClassDescriptionTraversal.java:52)
    at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor.processClassConfigurations(ModuleJndiBindingProcessor.java:140)
    at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor.deploy(ModuleJndiBindingProcessor.java:133)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:115) [jboss-as-server-7.0.2.Final.jar:7.0.2.Final]
    ... 5 more

    Could you help me please, and explain what I did wrong?
    Kind regards,
    Karolina

  3. Pingback: Full Web App JSF – EJB – Hibernate Tutorial | snippetjournal

  4. Stefanie

    Hi,

    you’ve done a great job with this tutorial! It was a hell of a help for me starting with this topic!
    Nevertheless I found out that I had a problem with caching data:
    When creating a group and assining it to a user the information was stored in the database but not in cache. That results in wrong actions when trying to delete the group (it was not encountered that there is a user having this group). Restarting the application solved the problem as the cache was fresh but I wanted the cache to be refreshed as soon as I do any modifying action like insert, update or delete.

    Therefore I added the following code to my abstract class GenericDao (just used the update method – same has to be applied for save and delete method):

    @Override
    public E update(final E entity) {
    em.merge(entity);
    refresh(entity);
    return entity;
    }

    @Override
    public void refresh(E entity) {
    em.getEntityManagerFactory().getCache().evict(this.entityClass, entity);
    }

    What do you think about this solution? Maybe it is a help for anybody who is looking for the same problem I was :-)

    Greetings,
    Stefi

    • uaihebert Post author

      Stefanie, good morning.

      Thank you for the feedback.

      I cannot tell you if this is the best solution for the problem because I would need to understand every detail of what is going on.

      I appreciate your time in leaving the solution here to help others with the same kind of problem.

      Thank you!

  5. Ali

    Hello,

    Very nice tutorial! Thank You.
    I would ask as a beginner, I want to use Glassfish instead of JBoss. Is it possible and where must I make changes ?

    Regards,
    Ali

    • uaihebert Post author

      Hello Ali,

      Yes, you can use it with Glassfish but I do not have here the files that you should change.

      Sorry about that.

      Thanks for passing by and the support.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *