Application with Spring + BootStrap with problem to find contextPath and apply CSS

0

The page is working normally, but CSS is not being applied, as images within the resources are not being localized. Here are methods of the configuration class to locate resources, and it does not work.

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

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

Page:

<c:url value="/resources/css" var="cssPath" />
<link rel="stylesheet" href="${cssPath }/bootstrap.min.css">
<link rel="stylesheet" href="${cssPath }/bootstrap-theme.min.css">

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Livros de java, Android, Iphone, PHP, Ruby e muito mais - Casa do código</title>


<c:url value="/resources/css" var="cssPath" />
<link rel="stylesheet" href="/resources/css/bootstrap.min.css">
<link rel="stylesheet" href="/resources/css/bootstrap-theme.min.css">
<!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> -->
<!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> -->


<style type="text/css">
    body{
            padding: 0px 30px;
    }
</style>


</head>
<body>


    <h1>Cadastro de Jidoka</h1>
    <form:form action="${s:mvcUrl('CC#gravarJidoka').build() }" method="post" commandName="jidoka" enctype="multipart/form-data">
        <div class="form-group">
            <label>Título</label>
            <form:input path="titulo" cssClass="form-control" />
            <form:errors path="titulo" />
        </div>
        <div class="form-group">
            <label>Descrição</label>
            <form:textarea path="descricao" cssClass="form-control" />
            <form:errors path="descricao" />
        </div>
        <div class="form-group">
            <label>Solicitante</label>
            <form:input path="solicitante" cssClass="form-control"/>
            <form:errors path="solicitante" />
        </div>
        <div class="form-group">
            <label>Setor Solicitante</label>
            <form:input path="setorSolicitante" cssClass="form-control" />
            <form:errors path="setorSolicitante" />
        </div>

        <button type="submit" class="btn btn-primary">Cadastrar</button>
    </form:form>
</body>
</html>

Class:

package br.com.schmersal.chamadostv.conf;

import java.util.concurrent.TimeUnit;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCacheManager;
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.client.RestTemplate;
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 com.google.common.cache.CacheBuilder;

import br.com.schmersal.chamadostv.controllers.ChamadosController;
import br.com.schmersal.chamadostv.daos.ProdutoDao;

@EnableWebMvc
@ComponentScan(basePackageClasses = { ChamadosController.class, ProdutoDao.class })
@EnableCaching
public class AppWebConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public InternalResourceViewResolver internalResourceViewResolve() {
        InternalResourceViewResolver resolve = new InternalResourceViewResolver();
        resolve.setPrefix("/WEB-INF/views/");
        resolve.setSuffix(".jsp");

        // resolver.setExposeContextBeansAsAttributes(true); poder pegar
        // qualquer variavel
        resolve.setExposedContextBeanNames("carrinhoCompras");
        return resolve;

    }

    @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 registra = new DateFormatterRegistrar();
        registra.setFormatter(new DateFormatter("dd/MM/yyyy"));
        registra.registerFormatters(conversionService);

        return conversionService;

    }

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

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/");
    }

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

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    public CacheManager cacheManager() {
        CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().maximumSize(100).expireAfterAccess(1,
                TimeUnit.MINUTES);
        GuavaCacheManager manager = new GuavaCacheManager();
        manager.setCacheBuilder(builder);
        return manager;
    }

    // @Bean
    // public ViewResolver
    // contentNegotiationViewResolver(ContentNegotiationManager manager) {
    // List<ViewResolver> viewResolvers = new ArrayList<>();
    // viewResolvers.add(internalResourceViewResolve());
    // viewResolvers.add(new JsonViewResolver());
    // ContentNegotiatingViewResolver resolver = new
    // ContentNegotiatingViewResolver();
    // resolver.setViewResolvers(viewResolvers);
    // resolver.setContentNegotiationManager(manager);
    // return resolver;
    // }

}

    
asked by anonymous 06.07.2017 / 21:38

1 answer

0

Place classpath: for spring to find the resources

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/");
}

To load HTML

<link href="/resources/css/style.css" rel="stylesheet" />
<script src="/resources/js/general.js"></script>
    
06.07.2017 / 21:56