We need a web.xml file:
JSFDemoSecurity.war/web/WEB-INF/web.xml
<?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_3_0.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>JavaServerFaces</display-name>
<!-- Add Support for Spring -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<!-- Database Support -->
<persistence-unit-ref>
<persistence-unit-ref-name>JSFDemoJNDI</persistence-unit-ref-name>
<persistence-unit-name>JSFDemoPU</persistence-unit-name>
</persistence-unit-ref>
</web-app>
Two important differences in this file from a "typical" web.xml for JSF. The first is we have to enable Spring as we did up top. And the second is our persistence unit needs to be referenced as we show at the bottom.
And, we need a faces-config.xml.
JSFDemoSpring.war/web/WEB-INF/faces-config.xml
<?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">
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
</faces-config>
Note: the only thing defined here is the SpringBeanFacesELResolver which allows us to reference the Spring Beans in the JSF page as though they were JSF Beans.
Next, we need a test page:
JSFDemoSpring.war/web/testPage.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html">
<h:head>
</h:head>
<h:body>
<h:form>
Number of parts current in the database is: <h:outputText value="#{testBean.count()}"/><br/>
#{testBean.message}<br/>
<h:commandButton value="testbean" actionListener="#{testBean.listener}" /><br/>
</h:form>
</h:body>
</html>
And we need a bean. TestBean.java:
package com.sample.beans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.event.ActionEvent;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
@Controller
@Scope("session")
public class TestBean implements Serializable {
@PersistenceUnit(unitName="JSFDemoPU")
private EntityManagerFactory emf;
private String message = "Test Bean Message";
private static final Logger logger = Logger.getLogger("com.sample.TestBean");
public TestBean() {
}
public int count() {
EntityManager em = emf.createEntityManager();
List results = new ArrayList();
logger.log(Level.FINE, "Counting how many parts are in the database.");
try {
results = em.createQuery("select p from PartEntity p").getResultList();
} catch (Exception e) {
logger.log(Level.SEVERE, "Fatal error in persistence environment: ", e);
}
return results.size();
}
public void listener(ActionEvent ae) {
message = "Executed Listener in Test Bean";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Now, in this bean, we defined it with annotations. The persistence unit was previously defined in the xml file, and we injected it here for a quick test. Notice we used a scope of session, and a controller stereo type. The controller stereo type is similar to the @ManagedBean we would normally use for JSF or the @Named for CDI. Because we annotated it, we do not reference it in the xml files.
If we now navigate to localhost:8080/JSFDemoSpring/testPage.xhtml, we will get a count of the parts in the parts table, and we will have a button that will change the message displayed. Note: this bean went around the design patterns discussed later in order to perform this simple test.
Success!
No comments:
Post a Comment