-1

i want to integrate jsf with spring without using any xml configuration to configure spring i used :

public class SpringWebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
    appContext.register(ApplicationContextConfig.class);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
            "SpringDispatcher", new DispatcherServlet(appContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");

}

and the ApplicationContextConfig for Spring hibernate and jsf :

public class ApplicationContextConfig {
@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsf");
    return viewResolver;
}

@Bean(name = "dataSource")
public DataSource getDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/usersdb");
    dataSource.setUsername("root");
    dataSource.setPassword("   ");

    return dataSource;
}


private Properties getHibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.show_sql", "true");
    properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
    return properties;
}


@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {
    LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
    sessionBuilder.addProperties(getHibernateProperties());
    sessionBuilder.addAnnotatedClasses(User.class, BookBean.class);
    return sessionBuilder.buildSessionFactory();
}




@Autowired
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(
        SessionFactory sessionFactory) {
    HibernateTransactionManager transactionManager = new HibernateTransactionManager(
            sessionFactory);

    return transactionManager;
}

But now i need to configure the faces-config.xml , the idea is to create a Controller and map my pages using the @RequestMapping annotation but it seems that i'm missing something. so Can that solution replace the faces-config.xml or is there any java config for that file?

Elias
  • 89
  • 10

1 Answers1

0

you also needs to configure JSF's FAces Servlet with something like below:

ServletRegistration.Dynamic facesServlet = servletContext.addServlet("facesServlet", new FacesServlet());
facesServlet.setLoadOnStartup(1);
facesServlet.addMapping("*.xhtml");

Again, here you will have to use faces-config.xml, if you want to use JSF. You can use annotation driven Managed beans and JSF's implicit navigation, but there is not such thing as requestmapping in Spring.

If you want to use @RequestMapping, that is available with Spring MVC, and unless you have a strong reason to use JSF, I'd recommend taking a look at Spring MVC, though it is not component based framework.

Tejas
  • 6,508
  • 1
  • 21
  • 25
  • with the annotation driven Managed bean an JSF's implicit navigation do i have to use faces-config.xml – Elias Mar 24 '17 at 14:29
  • yes, faces-config file will still be needed if you decide to go ahead with JSF. It's the basic config file. You can use implicit navigation and @ManagedBean annotations, but still, there are some basic configurations that goes in faces-config file. – Tejas Mar 24 '17 at 15:31