Os arquivos css e js para o spring e thymeleaf
01/09/2017 17:15
0
Configurei o spring assim e está funcionando.

Só que não consigo acessar a pasta aonde fica os css e js.

O que pode estar de errado ?


package br.com.netsoft.desif;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.format.datetime.DateFormatterRegistrar;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
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.InternalResourceViewResolver;

import br.com.netsoft.desif.controller.HomeController;

@EnableWebMvc
@ComponentScan(basePackageClasses = { HomeController.class })
// @Configuration
public class AppWebConfiguration extends WebMvcConfigurerAdapter {

 @Bean
 public InternalResourceViewResolver internalResourceViewResolver() {
 InternalResourceViewResolver resolver = new InternalResourceViewResolver();
 resolver.setPrefix("/WEB-INF/views/");
 resolver.setSuffix(".html");
 return resolver;
 }

 @Bean
 public MessageSource messageSource() {
 ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
 messageSource.setBasename("/WEB-INF/messages");
 messageSource.setDefaultEncoding("UTF-8");
 messageSource.setCacheSeconds(1);
 return messageSource;
 }

 @Bean
 public FormattingConversionService mvcConversionService() {
 DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
 DateFormatterRegistrar registrar = new DateFormatterRegistrar();
 registrar.setFormatter(new DateFormatter("dd/MM/yyyy"));
 registrar.registerFormatters(conversionService);
 return conversionService;
 }

 @Bean
 public MultipartResolver multipartResolver() {
 return new StandardServletMultipartResolver();
 }

 @Override
 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
 configurer.enable();
 }

 @Override
 public void addResourceHandlers(ResourceHandlerRegistry registry) {
 registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
 registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/").resourceChain(true);
 }
}





package br.com.netsoft.desif;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableJpaRepositories(basePackageClasses = SpringWebInitializer.class)
@EnableTransactionManagement
@Configuration
public class JPAConfiguration {

 @Bean
 public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
 LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
 JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
 factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
 DriverManagerDataSource dataSource = new DriverManagerDataSource();
 dataSource.setUsername("postgres");
 dataSource.setPassword("63Netsis417");
 dataSource.setUrl("jdbc:postgresql://localhost:5432/desif");
 dataSource.setDriverClassName("org.postgresql.Driver");
 factoryBean.setDataSource(dataSource);
 Properties props = new Properties();
 props.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
 props.setProperty("hibernate.show_sql", "true");
 props.setProperty("hibernate.hbm2ddl.auto", "none");
 factoryBean.setJpaProperties(props);
 factoryBean.setPackagesToScan("br.com.netsoft.desif");
 return factoryBean;
 }

 @Bean
 public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
 return new JpaTransactionManager(emf);
 }
}






package br.com.netsoft.desif;

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

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class SpringWebInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
  AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
  applicationContext.scan(SpringWebInitializer.class.getPackage().getName());
  servletContext.addListener(new ContextLoaderListener(applicationContext));
  servletContext.addListener(new RequestContextListener());
  ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet(applicationContext));
  dispatcher.setAsyncSupported(true);
  dispatcher.setLoadOnStartup(1);
  dispatcher.addMapping("/");
}
 
private DispatcherServlet dispatcherServlet(WebApplicationContext applicationContext) {
  return new DispatcherServlet(applicationContext);
}
}





package br.com.netsoft.desif;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.thymeleaf.ITemplateEngine;
import org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;

import nz.net.ultraq.thymeleaf.LayoutDialect;

@Configuration
public class ThymeleafConfig {

 @Autowired
 private ApplicationContext applicationContext;

 @Bean
 public ViewResolver thymeleafViewResolver(ITemplateEngine templateEngine) {
 ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
 viewResolver.setTemplateEngine(templateEngine);
 viewResolver.setCharacterEncoding("UTF-8");
 return viewResolver;
 }

 @Bean
 public ITemplateEngine templateEngine(ITemplateResolver templateResolver) {
 SpringTemplateEngine templateEngine = new SpringTemplateEngine();
 templateEngine.setTemplateResolver(templateResolver);
 templateEngine.setEnableSpringELCompiler(true);
 templateEngine.addDialect(new LayoutDialect());
 templateEngine.addDialect(new SpringSecurityDialect());
 return templateEngine;
 }

 @Bean
 public ITemplateResolver templateResolver() {
 SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
 templateResolver.setApplicationContext(this.applicationContext);
 templateResolver.setTemplateMode(TemplateMode.HTML);
 templateResolver.setPrefix("/WEB-INF/views/");
 templateResolver.setSuffix(".html");
 templateResolver.setCharacterEncoding("UTF-8");
 templateResolver.setCacheable(false);
 return templateResolver;
 }
}
Tags: spring, thymeleaf, java


Ainda não faz parte da comunidade???

Para se registrar, clique aqui.


Aprenda Groovy e Grails, Spring e mais com a Formação itexto!

Livro de Spring


/dev/All

Os melhores blogs de TI
em um único lugar!

 
Spring Brasil é mantido por itexto Consultoria.
Em caso de problemas contacte Henrique Lobo Weissmann (Kico) por e-mail: kico@itexto.com.br
Todo o conteúdo presente neste site adota o Creative Commons como licença padrão.