我想将jsf与spring集成,而不使用任何xml配置来配置我使用的spring:
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("/");
}
和Spring Hibernate的ApplicationContextConfig和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;
}
但现在我需要配置faces-config.xml,我的想法是使用@RequestMapping注释创建一个Controller并映射我的页面,但似乎我错过了一些东西。 那么该解决方案可以替换faces-config.xml,还是该文件有任何java配置?
答案 0 :(得分:0)
您还需要使用以下内容配置JSF的FAces Servlet:
ServletRegistration.Dynamic facesServlet = servletContext.addServlet("facesServlet", new FacesServlet());
facesServlet.setLoadOnStartup(1);
facesServlet.addMapping("*.xhtml");
同样,如果你想使用JSF,你必须使用faces-config.xml。你可以使用注释驱动的Managed bean和JSF的隐式导航,但Spring中没有requestmapping这样的东西。
如果你想使用Spring MVC提供的@RequestMapping,除非你有充分的理由使用JSF,我建议你看一下Spring MVC,虽然它不是基于组件的框架。