0

I'm trying to integrate JSF 2 with Spring and I was following this example and making some modifications to access a DB and execute a Stored Procedure.

But when I run the project I'm getting a Status 404 - Not found from the GlassFish Server. In the console log I'm getting the message:

Warning:   No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'dispatcher'

Here is my resulting code:

Folder Structure

Project Structure

Initializer.java

public class Initializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppConfig.class);
        ctx.setServletContext(servletContext);
        servletContext.addListener(new ContextLoaderListener(ctx));
        servletContext.addListener(new RequestContextListener());
        Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        dynamic.addMapping("/");
        dynamic.setLoadOnStartup(1);
   }
}

AppConfig.java

@Configuration
@ComponentScan("source")
class AppConfig {

    @Bean
    public Service service() {
        DriverManagerDataSource ds = new DriverManagerDataSource("jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull", "root", "rootPass");
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        return new ServiceImpl(ds);
    }
}

ProcBean.java

@ManagedBean(name = "procBean", eager = true)
@RequestScoped
@Component
@RequestMapping("/")
public class ProcBean {

    private int input;
    private int output;
    @Autowired public Service procService;

    // Empty constructor, getters/setters

    public String callStoredProcedure() {
        this.output = procService.callStoredProcedure(input);
        return "output";
    }
}

ServiceImpl.java

public class ServiceImpl implements Service {

    private DataSource dataSource;
    private StoredProcedurePrueba prueba;

    public ServiceImpl(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    // get/set dataSource

    @Override
    public int callStoredProcedure(int input) {
        this.prueba = new StoredProcedurePrueba(dataSource);
        return this.prueba.execute(input);
    }

    private class StoredProcedurePrueba extends StoredProcedure {
        // Implementation tested separately and working correctly
    }
}

Configuration

faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">

  <application>
    <el-resolver>
      org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
  </application>
</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 
         xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

  <display-name>JSF 2 + Spring 4 Integration Example</display-name>

    <servlet>
      <servlet-name>FacesServlet</servlet-name>
      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
      <servlet-name>FacesServlet</servlet-name>
      <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <session-config>
      <session-timeout>30</session-timeout>
    </session-config>
</web-app>

Web pages

<!-- input.xhtml -->
<h:body>
  <h3>JSF 2 + Spring 4 Integration Example</h3>
  <h:form id="studentForm">
    <h:outputLabel value="Enter Student id:" />
    <h:inputText value="#{procBean.input}" /> <br />
    <h:commandButton value="Submit" action="#{procBean.callStoredProcedure()}"/>
  </h:form>
</h:body>

<!-- output.xhtml -->
<h:body>
  <h3>JSF 2 + Spring 4 Integration Example</h3>
  <p>#{procBean.output}</p>
</h:body>

I was trying some other solutions but none of them works for me. Any idea? What am I missing?

Thanks in advance for your answers.

Alvaro Pedraza
  • 1,176
  • 6
  • 20
  • 44
  • 1
    You're using GlassFish instead of Tomcat and yet you're attempting to use Spring instead of Java EE? Why? As to the attempt to use Spring MVC's `DispatcherServlet` on JSF, here's some food for read: http://stackoverflow.com/q/18744910 – BalusC Oct 19 '15 at 08:43
  • I chose GlassFish as it is the recomended server in the JEE7 documentation. Thanks for clearing the difference between Spring MVC and Framework, I need to study a lot about it – Alvaro Pedraza Oct 19 '15 at 14:34
  • I forgot to ask: is there any difference/advantage/disadvantage in using GlassFish instead of Tomcat when I'm working with Spring? – Alvaro Pedraza Oct 19 '15 at 15:23
  • 1
    Java EE (GlassFish) + Spring is like driving a Ferrari in a traffic jam. Tomcat + Spring would be more like driving a (full packed) motorbike. Alternatively, just get rid of Spring and stick to standard Java EE. Spring was useful during J2EE era decades back. These days it's only for hipsters. Another food for read: http://stackoverflow.com/q/18369356 (in case you didn't bother to click through "See also" links in the link in my 1st comment) – BalusC Oct 19 '15 at 15:28
  • @BalusC, in fact, I saw your "See also" section, but after posting the comment. Anyway, thanks for your comments, they're really helpful. Java it's a big world and I am overwhelmed. I need a sabbatical to get updated and put things to work. – Alvaro Pedraza Oct 19 '15 at 21:50

1 Answers1

1

Not using Spring but I would think you need a welcome-file in web.xml like

<welcome-file-list>
    <welcome-file>input.xhtml</welcome-file>
</welcome-file-list>
Jaqen H'ghar
  • 4,305
  • 2
  • 14
  • 26