2014-11-20 46 views
0

我是Spring的新手,但直到知道我沒有遇到任何嚴重問題。

我想用spring的一些templeate機制,所以我選擇了Apache Tiles。 我認爲這將是最簡單的。但我現在爲此奮鬥了近2天。

我有問題詭計與Apache Tiles3配置Spring4 這裏是我的配置:

package com.derp.common.init; 

import java.util.Properties; 

import javax.annotation.Resource; 
import javax.sql.DataSource; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.core.env.Environment; 
import org.springframework.http.MediaType; 
import org.springframework.jdbc.datasource.DriverManagerDataSource; 
import org.springframework.orm.hibernate4.HibernateTransactionManager; 
import org.springframework.orm.hibernate4.LocalSessionFactoryBean; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
import org.springframework.web.servlet.view.UrlBasedViewResolver; 
import org.springframework.web.servlet.view.tiles3.TilesConfigurer; 
import org.springframework.web.servlet.view.tiles3.TilesView; 
import org.springframework.web.servlet.view.tiles3.TilesViewResolver; 

//import com.derp.common.wicketView.HomePage; 

@Configuration 
@ComponentScan("com.derp") 
@EnableWebMvc 
@EnableTransactionManagement 
@PropertySource("classpath:application.properties") 
public class WebAppConfig extends WebMvcConfigurerAdapter { 

    private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver"; 
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password"; 
    private static final String PROPERTY_NAME_DATABASE_URL = "db.url"; 
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username"; 

    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect"; 
    private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql"; 
    private static final String PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO = "hibernate.hbm2ddl.auto"; 
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_SERVICES = "services.entitymanager.packages.to.scan"; 
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_COMMON = "common.entitymanager.packages.to.scan"; 
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_CMS = "cms.entitymanager.packages.to.scan"; 

    @Resource 
    private Environment env; 

    @Bean 
    public DataSource dataSource() { 
     DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
     dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER)); 
     dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL)); 
     dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME)); 
     dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD)); 
     return dataSource; 
    } 

    @Bean 
    public LocalSessionFactoryBean sessionFactory() { 
     LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); 
     sessionFactoryBean.setDataSource(dataSource()); 
     //sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN)); 
     sessionFactoryBean.setPackagesToScan(new String[] { 
       env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_SERVICES), 
       env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_COMMON), 
       env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN_CMS) 
       }); 
     sessionFactoryBean.setHibernateProperties(hibProperties()); 
     return sessionFactoryBean; 
    } 

    private Properties hibProperties() { 
     Properties properties = new Properties(); 
     properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT)); 
     properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL)); 
     properties.put(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO)); 
     return properties; 
    } 

    @Bean 
    public HibernateTransactionManager transactionManager() { 
     HibernateTransactionManager transactionManager = new HibernateTransactionManager(); 
     transactionManager.setSessionFactory(sessionFactory().getObject()); 
     return transactionManager; 
    } 

    @Bean 
    public UrlBasedViewResolver setupViewResolver() { 
     TilesViewResolver resolver = new TilesViewResolver(); 
     resolver.setPrefix("/WEB-INF/views/"); 
     resolver.setSuffix(".jsp"); 
     resolver.setViewClass(TilesView.class); 
     return resolver; 
    } 

    @Bean 
    public TilesConfigurer getTilesConfigurer() { 
     TilesConfigurer tilesConfigurer = new TilesConfigurer(); 
     tilesConfigurer.setCheckRefresh(true); 
     tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/tiles.xml" }); 
     return tilesConfigurer; 
    } 
    @Override 
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { 
     configurer.defaultContentType(MediaType.ALL) 
       .mediaType("json", MediaType.APPLICATION_JSON) 
       .mediaType("xml", MediaType.APPLICATION_XML) 
       .favorPathExtension(true); // default is true. just for clarity 
    } 
    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/css/*").addResourceLocations("/WEB-INF/css/*"); 
     registry.addResourceHandler("/js/*").addResourceLocations("/WEB-INF/js/*"); 
     registry.addResourceHandler("/img/*").addResourceLocations("/WEB-INF/img/*"); 
    } 
} 

和:

package com.derp.common.init; 

import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRegistration.Dynamic; 

import org.springframework.web.WebApplicationInitializer; 
import org.springframework.web.context.ContextLoaderListener; 
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 
import org.springframework.web.filter.HiddenHttpMethodFilter; 
import org.springframework.web.servlet.DispatcherServlet; 

public class Initializer implements WebApplicationInitializer { 


    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); 
     ctx.register(WebAppConfig.class); 
     servletContext.addListener(new ContextLoaderListener(ctx)); 

     ctx.setServletContext(servletContext); 

     Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); 
     servlet.addMapping("/"); 
     servlet.setAsyncSupported(true);  
     servlet.setLoadOnStartup(1); 
     // Allow to use Put and Delete method for REST architecture 
     registerHiddenFieldFilter(servletContext); 
    } 

    private void registerHiddenFieldFilter(ServletContext aContext) { 
     aContext.addFilter("hiddenHttpMethodFilter", new HiddenHttpMethodFilter()).addMappingForUrlPatterns(null ,true, "/*"); 
    } 
} 

tiles.xml

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!DOCTYPE tiles-definitions PUBLIC 
     "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" 
     "http://tiles.apache.org/dtds/tiles-config_3_0.dtd"> 
<tiles-definitions> 

    <definition name="defaultTemplate" template="/WEB-INF/layout/layout.jsp"> 
     <put-attribute name="header" value="/WEB-INF/layout/header.jsp" /> 
     <put-attribute name="menu" value="/WEB-INF/layout/menu.jsp" /> 
     <put-attribute name="footer" value="/WEB-INF/layout/footer.jsp" /> 
    </definition> 

</tiles-definitions> 

控制器:

package com.derp.common.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
public class IndexController { 
    @RequestMapping(value="/", method=RequestMethod.GET) 
    public ModelAndView mainPage() { 
     return new ModelAndView("home"); 
    } 

任何人都可以告訴我在這裏錯過了什麼?

回答

0

我認爲你必須定義一個叫做「家」的延伸模板「defaultTemplate」這樣的新定義:

<!-- Home page --> 
<definition name="home" extends="defaultTemplate" /> 

而在你的控制器一樣,返回一個字符串值:

@Controller 
public class IndexController { 
    @RequestMapping(value="/", method=RequestMethod.GET) 
    public String mainPage() { 
     return "home"; 
    } 
} 
+0

感謝您的回覆。不幸的是我無法檢查你的解決方案,因爲我放棄了Apache Tiles3。我已經轉向工作解決方案(Thymeleaf); – masterdany88 2015-01-12 07:43:04

相關問題