Aplicación basada en:
Para los que ya tienen experiencia y sólo necesitan ver los archivos, estos se encuentran en mi proyecto demo en github.
Primero necesitas crear el proyecto base con
Y buscar la opción de aplicación web JEE ó crear una aplicación web con maven con cualquier IDE que lo soporte. Luego necesitamos asegurarnos de tener estas dependencias (el orden es importante):
Puedes encontrar el pom.xml aquí
El web.xml ya no es necesario en una aplicación web JEE 6, pero dado que estoy utilizando los perfiles de Spring (aunque esos creo que se pueden configurar sin el XML) y unas páginas de errores, estas necesitan estar en el web.xml, como lo muestro aquí, claro que puedes poner más cosas pero son las que necesitamos para nuestro proyecto.
Ahora los archivos que configuran Spring, aunque todos pueden estar en uno sólo, decidí separarlos para cuestiones de legibilidad (todos los puedes encontrar aquí) (Para que no tengas errores de compilación al hacer el DataConfig.java, necesitas hacer primero el PropertyPlaceHolder.java):
Con el ComponentConfig habilitamos la búsqueda automática de componentes de Spring en la ruta que especificamos en @ComponentScan, en el DataConfig configuramos el acceso a base de datos y en este ejemplo utilizo dos ambientes: production y tests. Cuando corre toma la configuración que va a utilizar del web.xml. En el PropertyPlaceHolder configuramos propiedades que no queremos que estén en nuestro control de versiones(IP's, contraseñas, etc.) y por último el WebConfig, donde habilitamos Spring MVC, configuramos algunas cosas como si vamos a utilizar jsp's y dónde las tiene que encontrar, la internacionalización y también tengo un filtro que intercepta las peticiones para ponerles el idioma que el usuario quiere utilizar en la página y la cambia con ?locale=xx, en donde xx se sustituye por el idioma (es, en, pt, etc). Ahora si arrancamos nuestra aplicación ahora Spring todavía no estará cargado en nuestra aplicación. Esto se debe a que no lo configuramos en el web.xml y porque nos falta o ya sea cargarlo ahí o cargarlo con Java, vamos a hacer está última:
En esta clase configuramos Spring, y otros filtros como el de codificación de caracteres para que intercepte las peticiones y las haga UTF-8. Y ahora sí, cuando levantemos nuestra aplicación esta va a estar trabajando con Spring MVC 3.2 funcionando. Para que la aplicación tenga una base sólida en HTML5 pueden utilizar http://html5boilerplate.com/ y http://getbootstrap.com/
- Spring 3.2.1
- Hibernate 4.1.9
Para los que ya tienen experiencia y sólo necesitan ver los archivos, estos se encuentran en mi proyecto demo en github.
Primero necesitas crear el proyecto base con
mvn archetype:generate
Y buscar la opción de aplicación web JEE ó crear una aplicación web con maven con cualquier IDE que lo soporte. Luego necesitamos asegurarnos de tener estas dependencias (el orden es importante):
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependencies> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>jstl</artifactId> | |
<version>1.2</version> | |
</dependency> | |
<dependency> | |
<groupId>taglibs</groupId> | |
<artifactId>standard</artifactId> | |
<version>1.1.2</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.mail</groupId> | |
<artifactId>mail</artifactId> | |
<version>1.4.5</version> | |
</dependency> | |
<dependency> | |
<groupId>org.jboss.spec</groupId> | |
<artifactId>jboss-javaee-6.0</artifactId> | |
<version>1.0.0.Final</version> | |
<type>pom</type> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>javax</groupId> | |
<artifactId>javaee-web-api</artifactId> | |
<version>6.0</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>commons-logging</groupId> | |
<artifactId>commons-logging</artifactId> | |
<version>1.1.1</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>jcl-over-slf4j</artifactId> | |
<version>1.7.2</version> | |
</dependency> | |
<dependency> | |
<groupId>ch.qos.logback</groupId> | |
<artifactId>logback-classic</artifactId> | |
<version>1.0.9</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hamcrest</groupId> | |
<artifactId>hamcrest-all</artifactId> | |
<version>1.3</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-aop</artifactId> | |
<version>3.2.1.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-jdbc</artifactId> | |
<version>3.2.1.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-tx</artifactId> | |
<version>3.2.1.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-orm</artifactId> | |
<version>3.2.1.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-web</artifactId> | |
<version>3.2.1.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-webmvc</artifactId> | |
<version>3.2.1.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context-support</artifactId> | |
<version>3.2.1.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-test</artifactId> | |
<version>3.2.1.RELEASE</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.security</groupId> | |
<artifactId>spring-security-openid</artifactId> | |
<version>3.1.3.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.security</groupId> | |
<artifactId>spring-security-web</artifactId> | |
<version>3.1.3.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.security</groupId> | |
<artifactId>spring-security-config</artifactId> | |
<version>3.1.3.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.security</groupId> | |
<artifactId>spring-security-taglibs</artifactId> | |
<version>3.1.3.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-core</artifactId> | |
<version>4.1.9.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-ehcache</artifactId> | |
<version>4.1.9.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-validator</artifactId> | |
<version>4.3.1.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.11</version> | |
<scope>test</scope> | |
<exclusions> | |
<exclusion> | |
<artifactId>hamcrest-core</artifactId> | |
<groupId>org.hamcrest</groupId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
<dependency> | |
<groupId>commons-dbcp</groupId> | |
<artifactId>commons-dbcp</artifactId> | |
<version>1.4</version> | |
</dependency> | |
<dependency> | |
<groupId>commons-pool</groupId> | |
<artifactId>commons-pool</artifactId> | |
<version>1.6</version> | |
</dependency> | |
<dependency> | |
<groupId>postgresql</groupId> | |
<artifactId>postgresql</artifactId> | |
<version>9.1-901-1.jdbc4</version> | |
<scope>runtime</scope> | |
</dependency> | |
<dependency> | |
<groupId>commons-fileupload</groupId> | |
<artifactId>commons-fileupload</artifactId> | |
<version>1.2.2</version> | |
</dependency> | |
<dependency> | |
<groupId>commons-io</groupId> | |
<artifactId>commons-io</artifactId> | |
<version>2.4</version> | |
</dependency> | |
<dependency> | |
<groupId>cglib</groupId> | |
<artifactId>cglib</artifactId> | |
<version>3.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.sitemesh</groupId> | |
<artifactId>sitemesh</artifactId> | |
<version>3.0-alpha-2</version> | |
</dependency> | |
<dependency> | |
<groupId>commons-lang</groupId> | |
<artifactId>commons-lang</artifactId> | |
<version>2.6</version> | |
</dependency> | |
<dependency> | |
<groupId>net.sf.jasperreports</groupId> | |
<artifactId>jasperreports</artifactId> | |
<version>5.0.0</version> | |
<exclusions> | |
<exclusion> | |
<artifactId>bcmail-jdk14</artifactId> | |
<groupId>bouncycastle</groupId> | |
</exclusion> | |
<exclusion> | |
<artifactId>bcprov-jdk14</artifactId> | |
<groupId>bouncycastle</groupId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
<dependency> | |
<groupId>org.codehaus.jackson</groupId> | |
<artifactId>jackson-mapper-asl</artifactId> | |
<version>1.9.11</version> | |
</dependency> | |
<dependency> | |
<groupId>joda-time</groupId> | |
<artifactId>joda-time</artifactId> | |
<version>2.1</version> | |
</dependency> | |
</dependencies> |
El web.xml ya no es necesario en una aplicación web JEE 6, pero dado que estoy utilizando los perfiles de Spring (aunque esos creo que se pueden configurar sin el XML) y unas páginas de errores, estas necesitan estar en el web.xml, como lo muestro aquí, claro que puedes poner más cosas pero son las que necesitamos para nuestro proyecto.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<web-app | |
version="3.0" | |
xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> | |
<display-name>Demo</display-name> | |
<context-param> | |
<param-name>spring.profiles.active</param-name> | |
<param-value>production</param-value> | |
</context-param> | |
<session-config> | |
<session-timeout>30</session-timeout> | |
</session-config> | |
<welcome-file-list> | |
<welcome-file>index.jsp</welcome-file> | |
</welcome-file-list> | |
<error-page> | |
<error-code>404</error-code> | |
<location>/404.html</location> | |
</error-page> | |
<error-page> | |
<error-code>405</error-code> | |
<location>/405.html</location> | |
</error-page> | |
</web-app> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.davidmendoza.demo.config; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.PropertySource; | |
@Configuration | |
@ComponentScan(basePackages = "org.davidmendoza.demo") | |
@PropertySource("file:${user.home}/.demo.properties") | |
public class ComponentConfig { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.davidmendoza.demo.config; | |
import org.hibernate.SessionFactory; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Import; | |
import org.springframework.context.annotation.Profile; | |
import org.springframework.jdbc.datasource.DriverManagerDataSource; | |
import org.springframework.orm.hibernate4.HibernateExceptionTranslator; | |
import org.springframework.orm.hibernate4.HibernateTransactionManager; | |
import org.springframework.orm.hibernate4.LocalSessionFactoryBean; | |
import org.springframework.transaction.PlatformTransactionManager; | |
import org.springframework.transaction.annotation.EnableTransactionManagement; | |
import javax.sql.DataSource; | |
import java.util.Properties; | |
@Configuration | |
@EnableTransactionManagement | |
public class DataConfig { | |
private static final Logger log = LoggerFactory.getLogger(DataConfig.class); | |
@Autowired | |
private SessionFactory sessionFactory; | |
@Bean | |
public PlatformTransactionManager transactionManager() { | |
return new HibernateTransactionManager(sessionFactory); | |
} | |
@Bean | |
public HibernateExceptionTranslator hibernateExceptionTranslator() { | |
return new HibernateExceptionTranslator(); | |
} | |
@Configuration | |
@Profile("production") | |
@Import(PropertyPlaceholderConfig.class) | |
static class Production { | |
@Value("${hibernate.dialect}") | |
protected String hibernateDialect; | |
@Value("${hibernate.show_sql}") | |
protected String hibernateShowSql; | |
@Value("${hibernate.hbm2ddl.auto}") | |
protected String hibernateHbm2DDL; | |
@Value("${hibernate.cache.use_second_level_cache}") | |
protected String hibernateSecondLevelCache; | |
@Value("${hibernate.cache.provider_class}") | |
protected String hibernateCacheClass; | |
@Value("${hibernate.default_schema}") | |
protected String hibernateSchema; | |
@Value("${jdbc.driverClassName}") | |
protected String jdbcDriver; | |
@Value("${jdbc.username}") | |
protected String jdbcUsername; | |
@Value("${jdbc.password}") | |
protected String jdbcPassword; | |
@Value("${jdbc.url}") | |
protected String jdbcUrl; | |
@Bean | |
public SessionFactory sessionFactory() { | |
LocalSessionFactoryBean factoryBean; | |
try { | |
factoryBean = new LocalSessionFactoryBean(); | |
Properties pp = new Properties(); | |
pp.setProperty("hibernate.dialect", hibernateDialect); | |
pp.setProperty("hibernate.show_sql", hibernateShowSql); | |
pp.setProperty("hibernate.hbm2ddl.auto", hibernateHbm2DDL); | |
pp.setProperty("hibernate.cache.use_second_level_cache", hibernateSecondLevelCache); | |
pp.setProperty("hibernate.cache.provider_class", hibernateCacheClass); | |
pp.setProperty("hibernate.default_schema", hibernateSchema); | |
factoryBean.setDataSource(dataSource()); | |
factoryBean.setPackagesToScan("org.davidmendoza.demo.model"); | |
factoryBean.setHibernateProperties(pp); | |
factoryBean.afterPropertiesSet(); | |
return factoryBean.getObject(); | |
} catch (Exception e) { | |
log.error("Couldn't configure the sessionFactory bean", e); | |
} | |
throw new RuntimeException("Couldn't configure the sessionFactory bean"); | |
} | |
@Bean | |
public DataSource dataSource() { | |
DriverManagerDataSource ds = new DriverManagerDataSource(); | |
ds.setDriverClassName(jdbcDriver); | |
ds.setUsername(jdbcUsername); | |
ds.setPassword(jdbcPassword); | |
ds.setUrl(jdbcUrl); | |
return ds; | |
} | |
} | |
@Configuration | |
@Profile("tests") | |
@Import(PropertyPlaceholderConfig.class) | |
static class Tests { | |
@Value("${test.hibernate.dialect}") | |
protected String testHibernateDialect; | |
@Value("${test.hibernate.show_sql}") | |
protected String testHibernateShowSql; | |
@Value("${test.hibernate.hbm2ddl.auto}") | |
protected String testHibernateHbm2DDL; | |
@Value("${test.hibernate.cache.use_second_level_cache}") | |
protected String testHibernateSecondLevelCache; | |
@Value("${test.hibernate.cache.provider_class}") | |
protected String testHibernateCacheClass; | |
@Value("${test.hibernate.default_schema}") | |
protected String testHibernateSchema; | |
@Value("${test.jdbc.driverClassName}") | |
protected String testJdbcDriver; | |
@Value("${test.jdbc.username}") | |
protected String testJdbcUsername; | |
@Value("${test.jdbc.password}") | |
protected String testJdbcPassword; | |
@Value("${test.jdbc.url}") | |
protected String testJdbcUrl; | |
@Bean | |
public SessionFactory sessionFactory() { | |
LocalSessionFactoryBean factoryBean; | |
try { | |
factoryBean = new LocalSessionFactoryBean(); | |
Properties pp = new Properties(); | |
pp.setProperty("hibernate.dialect", testHibernateDialect); | |
pp.setProperty("hibernate.show_sql", testHibernateShowSql); | |
pp.setProperty("hibernate.hbm2ddl.auto", testHibernateHbm2DDL); | |
pp.setProperty("hibernate.cache.use_second_level_cache", testHibernateSecondLevelCache); | |
pp.setProperty("hibernate.cache.provider_class", testHibernateCacheClass); | |
pp.setProperty("hibernate.default_schema", testHibernateSchema); | |
factoryBean.setDataSource(dataSource()); | |
factoryBean.setPackagesToScan("edu.swau.forms.model"); | |
factoryBean.setHibernateProperties(pp); | |
factoryBean.afterPropertiesSet(); | |
return factoryBean.getObject(); | |
} catch (Exception e) { | |
log.error("Couldn't configure the sessionFactory bean", e); | |
} | |
throw new RuntimeException("Couldn't configure the sessionFactory bean"); | |
} | |
@Bean | |
public DataSource dataSource() { | |
DriverManagerDataSource ds = new DriverManagerDataSource(); | |
ds.setDriverClassName(testJdbcDriver); | |
ds.setUsername(testJdbcUsername); | |
ds.setPassword(testJdbcPassword); | |
ds.setUrl(testJdbcUrl); | |
return ds; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.davidmendoza.demo.config; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.PropertySource; | |
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; | |
@Configuration | |
@PropertySource("file:${user.home}/.demo.properties") | |
public class PropertyPlaceholderConfig { | |
@Bean | |
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { | |
return new PropertySourcesPlaceholderConfigurer(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.davidmendoza.demo.config; | |
import org.springframework.context.MessageSource; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.support.ReloadableResourceBundleMessageSource; | |
import org.springframework.http.converter.HttpMessageConverter; | |
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; | |
import org.springframework.validation.Validator; | |
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; | |
import org.springframework.web.multipart.MultipartResolver; | |
import org.springframework.web.multipart.commons.CommonsMultipartResolver; | |
import org.springframework.web.servlet.LocaleResolver; | |
import org.springframework.web.servlet.ViewResolver; | |
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | |
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | |
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | |
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; | |
import org.springframework.web.servlet.i18n.SessionLocaleResolver; | |
import org.springframework.web.servlet.view.InternalResourceViewResolver; | |
import org.springframework.web.servlet.view.JstlView; | |
import java.util.List; | |
import java.util.Locale; | |
@Configuration | |
@EnableWebMvc | |
public class WebConfig extends WebMvcConfigurerAdapter { | |
/** | |
* Messages to support internationalization/localization. | |
*/ | |
@Bean | |
public MessageSource messageSource() { | |
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); | |
messageSource.setBasename("classpath:messages"); | |
messageSource.setDefaultEncoding("UTF-8"); | |
return messageSource; | |
} | |
// additional webmvc-related beans | |
@Bean | |
public ViewResolver viewResolver() { | |
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); | |
resolver.setPrefix("/WEB-INF/views/"); | |
resolver.setSuffix(".jsp"); | |
resolver.setCache(false); | |
resolver.setViewClass(JstlView.class); | |
return resolver; | |
} | |
/** | |
* Supports FileUploads. | |
*/ | |
@Bean | |
public MultipartResolver multipartResolver() { | |
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); | |
multipartResolver.setMaxUploadSize(500000); | |
return multipartResolver; | |
} | |
@Bean | |
public LocaleChangeInterceptor localeChangeInterceptor() { | |
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); | |
localeChangeInterceptor.setParamName("locale"); | |
return localeChangeInterceptor; | |
} | |
@Bean | |
public LocaleResolver localeResolver() { | |
SessionLocaleResolver localeResolver = new SessionLocaleResolver(); | |
localeResolver.setDefaultLocale(new Locale("en_US")); | |
return localeResolver; | |
} | |
// implementing WebMvcConfigurer | |
@Override | |
public void addInterceptors(InterceptorRegistry registry) { | |
registry.addInterceptor(localeChangeInterceptor()); | |
} | |
@Override | |
public void addResourceHandlers(ResourceHandlerRegistry registry) { | |
registry.addResourceHandler("/css/**").addResourceLocations("/css/"); | |
registry.addResourceHandler("/img/**").addResourceLocations("/img/"); | |
registry.addResourceHandler("/js/**").addResourceLocations("/js/"); | |
registry.addResourceHandler("/crossdomain.xml").addResourceLocations("/crossdomain.xml"); | |
registry.addResourceHandler("/favicon.ico").addResourceLocations("/favicon.ico"); | |
registry.addResourceHandler("/robots.txt").addResourceLocations("/robots.txt"); | |
registry.addResourceHandler("/humans.txt").addResourceLocations("/humans.txt"); | |
registry.addResourceHandler("/404.html").addResourceLocations("/404.html"); | |
registry.addResourceHandler("/405.html").addResourceLocations("/405.html"); | |
registry.addResourceHandler("/apple-touch-icon-114x114-precomposed.png").addResourceLocations("/apple-touch-icon-114x114-precomposed.png"); | |
registry.addResourceHandler("/apple-touch-icon-144x144-precomposed.png").addResourceLocations("/apple-touch-icon-144x144-precomposed.png"); | |
registry.addResourceHandler("/apple-touch-icon-57x57-precomposed.png").addResourceLocations("/apple-touch-icon-57x57-precomposed.png"); | |
registry.addResourceHandler("/apple-touch-icon-72x72-precomposed.png").addResourceLocations("/apple-touch-icon-72x72-precomposed.png"); | |
registry.addResourceHandler("/apple-touch-icon-precomposed.png").addResourceLocations("/apple-touch-icon-precomposed.png"); | |
registry.addResourceHandler("/apple-touch-icon.png").addResourceLocations("/apple-touch-icon.png"); | |
} | |
@Override | |
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { | |
converters.add(new MappingJacksonHttpMessageConverter()); | |
} | |
@Override | |
public Validator getValidator() { | |
LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean(); | |
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); | |
messageSource.setBasename("classpath:messages"); | |
messageSource.setDefaultEncoding("UTF-8"); | |
factory.setValidationMessageSource(messageSource); | |
return factory; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.davidmendoza.demo.config; | |
import org.sitemesh.config.ConfigurableSiteMeshFilter; | |
import org.springframework.web.WebApplicationInitializer; | |
import org.springframework.web.context.ContextLoaderListener; | |
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; | |
import org.springframework.web.filter.CharacterEncodingFilter; | |
import org.springframework.web.servlet.DispatcherServlet; | |
import javax.servlet.*; | |
import java.util.EnumSet; | |
import java.util.Set; | |
public class StartUpConfig implements WebApplicationInitializer { | |
@Override | |
public void onStartup(ServletContext servletContext) throws ServletException { | |
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); | |
context.register(ComponentConfig.class, DataConfig.class, MailConfig.class, WebConfig.class); | |
context.setDisplayName("DemoApp"); | |
FilterRegistration.Dynamic sitemeshFilter = servletContext.addFilter("sitemeshFilter", new ConfigurableSiteMeshFilter()); | |
sitemeshFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*"); | |
FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("characterEncodingFilter", new CharacterEncodingFilter()); | |
characterEncodingFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*"); | |
characterEncodingFilter.setInitParameter("encoding", "UTF-8"); | |
characterEncodingFilter.setInitParameter("forceEncoding", "true"); | |
servletContext.addListener(new ContextLoaderListener(context)); | |
//servletContext.setInitParameter("defaultHtmlEscape", "false"); | |
DispatcherServlet servlet = new DispatcherServlet(); | |
// no explicit configuration reference here: everything is configured in the root container for simplicity | |
servlet.setContextConfigLocation(""); | |
ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", servlet); | |
appServlet.setLoadOnStartup(1); | |
appServlet.setAsyncSupported(true); | |
Set<String> mappingConflicts = appServlet.addMapping("/"); | |
if (!mappingConflicts.isEmpty()) { | |
throw new IllegalStateException("'appServlet' cannot be mapped to '/' under Tomcat versions <= 7.0.14"); | |
} | |
} | |
} |
En esta clase configuramos Spring, y otros filtros como el de codificación de caracteres para que intercepte las peticiones y las haga UTF-8. Y ahora sí, cuando levantemos nuestra aplicación esta va a estar trabajando con Spring MVC 3.2 funcionando. Para que la aplicación tenga una base sólida en HTML5 pueden utilizar http://html5boilerplate.com/ y http://getbootstrap.com/
Comentarios