I want add support to JavaServer Faces to a spring project mine, but the tutorials I found on the web teach that for setting this to a project, add this line to the file web.xml:
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<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>
</servlet-mapping>
but my spring project don't use XML files for configuration, only java classes. Anyone can tell me how to configure JavaServer Faces in this scenario?
Equivalent class based configuration would be:
public class MyInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
ServletRegistration.Dynamic facesServlet = servletContext.addServlet("Faces Servlet", new FacesServlet());
facesServlet.setLoadOnStartup(1);
facesServlet.addMapping("/faces/*");
}
}
More details here
Related
I have a little problem in integrating spring with JSF.
I understood that either Spring and JSF have distinct containers. So I can't mix their annotations because registered beans will not be visible to each other.
However, I've read an article : Spring DAO is not injected in JSF managed bean where guy says that It's possible to annotate everything with spring annotations make visible these components to JSF.
I'm really confused with xml configuration because sometimes I read that I don't need it but in most cases I see people usually do this.
My question is what need to be configured if I have this source code and I want to make visible spring beans to JSF :
package pl.catarina.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import pl.catarina.domain.Patient;
import pl.catarina.repository.PatientRepository;
import javax.annotation.PostConstruct;
#Component
#Scope("request")
public class RegisterController {
#Autowired
private PatientRepository patientRepository;
private Patient patient;
#PostConstruct
public void init(){
patient = new Patient();
patient.setName("jan");
patient.setSurname("Way");
patient.setPhoneNumber("21421131");
patient.setEmail("asdpad#gmail.com");
patient.setPassword("pdsad223Sdsd");
}
public void save(){
patientRepository.save(patient);
}
}
REPOSITORY
package pl.catarina.repository;
import org.springframework.stereotype.Repository;
import pl.catarina.domain.Patient;
#Repository
public interface PatientRepository extends UserBaseRepository<Patient> {
Patient findByEmail(String email);
}
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_2_5.xsd"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id = "WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>pages/home.xhtml</welcome-file>
</welcome-file-list>
<!--
FacesServlet is main servlet responsible to handle all request.
It acts as central controller.
This servlet initializes the JSF components before the JSP is displayed.
-->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<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>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>com.ocpsoft.pretty.DEVELOPMENT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>bootstrap</param-value>
</context-param>
</web-app>
I'm getting the java.lang.IllegalStateException: Could not find backup for factory javax.faces.context.FacesContextFactory. when I deploy my webapp as a .war. Does anyone have an idea where to begin looking for the root cause of this problem? The application runs as expected in the Eclipse IDE, but when exported as a war for deploying on the publicly available stand-alone TomEE environment, it fails.
Many thanks.
My configuration:
Apache TomEE - WebProfile 1.6.0.2
JSF 2.2.7 (com.sun.faces)(Mojarra lib 2.2.0) <-- This may be a problem? But why does the app run in the Eclipse environment but not the stand-alone TomEE?
Maven 4.0.0
java.lang.IllegalStateException: Could not find backup for factory javax.faces.context.FacesContextFactory.
javax.faces.FactoryFinder$FactoryManager.getFactory(FactoryFinder.java:1004)
javax.faces.FactoryFinder.getFactory(FactoryFinder.java:316)
javax.faces.webapp.FacesServlet.init(FacesServlet.java:302)
org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:744)
The web.xml:
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
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>ProjName</display-name>
<welcome-file-list>
<welcome-file>login.xhtml</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.forceLoadConfiguration</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<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>
The jars that are exported in the .war WEB-INF/lib directory:
WEB-INF/lib/javax.faces.jar
WEB-INF/lib/javax.faces-api-2.2.jar
WEB-INF/lib/jsf-api-2.2.7.jar
WEB-INF/lib/jsf-impl-2.2.7.jar
WEB-INF/lib/poi-3.10-FINAL.jar
WEB-INF/lib/commons-codec-1.5.jar
WEB-INF/lib/poi-ooxml-3.10-FINAL.jar
WEB-INF/lib/poi-ooxml-schemas-3.10-FINAL.jar
WEB-INF/lib/xmlbeans-2.3.0.jar
WEB-INF/lib/stax-api-1.0.1.jar
WEB-INF/lib/dom4j-1.6.1.jar
WEB-INF/lib/xml-apis-1.0.b2.jar
WEB-INF/lib/cxf-bundle-jaxrs-2.7.11.jar
WEB-INF/lib/woodstox-core-asl-4.2.1.jar
WEB-INF/lib/stax2-api-3.1.4.jar
WEB-INF/lib/xmlschema-core-2.1.0.jar
WEB-INF/lib/geronimo-javamail_1.4_spec-1.7.1.jar
WEB-INF/lib/wsdl4j-1.6.3.jar
WEB-INF/lib/jaxb-impl-2.1.13.jar
WEB-INF/lib/jetty-server-8.1.14.v20131031.jar
WEB-INF/lib/jetty-continuation-8.1.14.v20131031.jar
WEB-INF/lib/jetty-http-8.1.14.v20131031.jar
WEB-INF/lib/jetty-io-8.1.14.v20131031.jar
WEB-INF/lib/jetty-util-8.1.14.v20131031.jar
WEB-INF/lib/jetty-security-8.1.14.v20131031.jar
WEB-INF/lib/slf4j-api-1.7.6.jar
WEB-INF/lib/geronimo-servlet_3.0_spec-1.0.jar
WEB-INF/lib/javax.ws.rs-api-2.0-m10.jar
WEB-INF/lib/postgresql-9.3-1101-jdbc41.jar
WEB-INF/lib/commons-httpclient-3.1.jar
WEB-INF/lib/commons-logging-1.0.4.jar
WEB-INF/lib/hamcrest-all-1.3.jar
WEB-INF/lib/junit-4.11.jar
WEB-INF/lib/hamcrest-core-1.3.jar
WEB-INF/lib/httpclient-4.3.3.jar
WEB-INF/lib/httpcore-4.3.2.jar
WEB-INF/lib/commons-dbcp2-2.0.jar
WEB-INF/lib/commons-pool2-2.2.jar
WEB-INF/lib/mockito-core-1.9.5.jar
WEB-INF/lib/objenesis-1.0.jar
I resolved this by removing the parameter from the web.xml file.
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
Removing the above text, rebuilding, exporting the war from Eclipse and deploying with the Tomcat Web Application Manager UI resolved the problem. Apparently, one of the jsf jars has this code built-in since http://java.sun.com/xml/ns/javaee/web-app_3_0 . I don't know this to be a fact, but read it in could not find Factory: javax.faces.context.FacesContextFactory. The difference here being that I removed the parameter instead of adding it.
I resolved this issue by removing the web.xml file and put these two methods in the Spring configuration file.
#Bean
public ServletContextInitializer servletContextCustomizer() {
return new ServletContextInitializer() {
#Override
public void onStartup(ServletContext sc) throws ServletException {
sc.setInitParameter(Constants.ContextParams.THEME, "bootstrap");
sc.setInitParameter(Constants.ContextParams.FONT_AWESOME, "true");
sc.setInitParameter(ProjectStage.PROJECT_STAGE_PARAM_NAME,ProjectStage.Development.name());
sc.setInitParameter("com.sun.faces.forceLoadConfiguration", "true");
sc.setInitParameter("contextConfigLocation", "test");
}
};
}
#Bean
public ServletRegistrationBean servletRegistrationBean() {
FacesServlet servlet = new FacesServlet();
ServletRegistrationBean registration = new ServletRegistrationBean(servlet, "*.jsf");
registration.setName("Faces Servlet");
registration.addUrlMappings("*.jsf");
registration.setMultipartConfig(new MultipartConfigElement((String) null));
registration.setEnabled(true);
registration.setLoadOnStartup(1);
return registration;
}
I have spring application, in which I use org.apache.cxf for soap and spring MVC for displayng some pages.
My web.xml contains two servlets :CXFServlet and mvc-dispatcher
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
When I has been used #ResponseBody in my controller everything was fine.
#Controller
#RequestMapping("/hello")
#ResponseBody
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome() {
return "hello" ;
}
}
but then i was needed to use jsp I have to use the following
#Controller
#RequestMapping("/hello")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView printWelcome(ModelMap model) {
model.addAttribute("message", "hello");
return new ModelAndView("hello") ;
}
}
and when I request http://localhost:8080/hello I get "No service was found" instead of "hello"
I found that if I delete following from web.xml
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
my controller works fine.
The Servlet container you are using is matching CXFServlet instead of mvc-dispatcher for the URI http://localhost:8080/hello, resulting in your request being sent to CXFServlet, and the error message "No service was found" being returned by CXFServlet. To quote the Servlet 3.0 spec,
Versions of this specification prior to 2.5 made use of these mapping
techniques as a suggestion rather than a requirement, allowing servlet
containers to each have their different schemes for mapping client
requests to servlets.
http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/
You will likely need to configure you CXFServlet mapping to something else, e.g.
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
You might want to mention the container (Tomcat, Glassfish, etc.) that you are using, as there could also be a bug preventing this from working correctly.
I can start grizzly and deploy Jersey webservices on it with the following lines.
protected HttpServer create() throws Throwable {
ResourceConfig rc = new PackagesResourceConfig("com.resource", "com.provider");
HttpServer server = GrizzlyServerFactory.createHttpServer(uri, rc);
return server;
}
But is there a way to load a web.xml instead of a ResourceConfig?
<web-app>
<servlet>
<servlet-name>Jersey</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.resource, com.provider</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
It seems that there is currently no direct way to configure grizzly with a web.xml. However I have used a partial solution that may be a beginning.
web.xml
First to understand the solution, we must understand what is the meaning of using a web.xml. It is basically use for configure your web application (see this answer for a more detail). In this case we are configuring init-params for the servlet.
The (partial) solution
Instead of using the web.xml and instead of using ResouceConfig.class, we can use Grizzly as our servlet and initializing the parameters.
For example
<web-app>
<servlet>
<servlet-name>Jersey</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.resource, com.provider</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
would give something like :
protected HttpServer create() throws Throwable {
HashMap<String, String> initParams = new HashMap<>();
//ServerProperties.PROVIDER_PACKAGES is equal to "jersey.config.server.provider.packages"
initParams.put(ServerProperties.PROVIDER_PACKAGES, "com.resource,com.provider");
//Make sure to end the URI with a forward slash
HttpServer server = GrizzlyWebContainerFactory.create("http://localhost:8080/", initParams);
return server;
}
With this, we can therefore put all the init-params that we want to.
However this solution cannot replace a whole web.xml.
I am using Apache CXF with Spring , please tell me how the CXFServlet reads the myapp-ws-context.xml
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:myapp-ws-context.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<display-name>CXF Servlet</display-name>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Have you seen sources of org.apache.cxf.transport.servlet.CXFServlet (open source)?
Everything is more than explicit:
#Override
protected void loadBus(ServletConfig sc) {
ApplicationContext wac = WebApplicationContextUtils.
getWebApplicationContext(sc.getServletContext());
String configLocation = sc.getInitParameter("config-location");
if (configLocation == null) {
try {
InputStream is = sc.getServletContext().getResourceAsStream("/WEB-INF/cxf-servlet.xml");
if (is != null && is.available() > 0) {
is.close();
configLocation = "/WEB-INF/cxf-servlet.xml";
}
} catch (Exception ex) {
//ignore
}
}
if (configLocation != null) {
wac = createSpringContext(wac, sc, configLocation);
}
if (wac != null) {
setBus(wac.getBean("cxf", Bus.class));
} else {
setBus(BusFactory.newInstance().createBus());
}
}
Note that WebApplicationContextUtils is a Spring class that tries to find an application context in servlet context attribute named: org.springframework.web.context.WebApplicationContext.ROOT.
Actually your classpath:myapp-ws-context.xml is read by Spring, not CXF.
By adding below configuration in your web.xml, it would be read by Spring and the context would be loaded:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:myapp-ws-context.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
But you could configure your Servlet/WebApp scope of Spring objects, like multipartResolver etc., to make the objects' scopes clearly, by enhancing your CXFServlet configuration like below:
<servlet>
<display-name>CXF Servlet</display-name>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<init-param>
<param-name>config-location</param-name>
<param-value>/WEB-INF/your-webapp-scope-spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
*Please take note that from your web-app context, you can access all objects in the context loaded from contextConfigLocation. *