4
respostas

CSS não carrega

Olá, tentei fazer as configurações informadas nos outros tópicos, porém, o CSS continua não carregando.

Abaixo como está o código:

package br.com.casadocodigo.loja.conf;

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.casadocodigo.loja.controllers.HomeController;
import br.com.casadocodigo.loja.daos.ProdutoDAO;
import br.com.casadocodigo.loja.infra.FileSaver;
import br.com.casadocodigo.loja.models.CarrinhoCompras;

@EnableWebMvc
@ComponentScan(basePackageClasses={HomeController.class, ProdutoDAO.class, FileSaver.class, CarrinhoCompras.class})
public class AppWebConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public InternalResourceViewResolver internalResourceViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposedContextBeanNames("carrinhoCompras");
        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 formatterRegistrar = new DateFormatterRegistrar();
        formatterRegistrar.setFormatter(new DateFormatter("dd/MM/yyyy"));
        formatterRegistrar.registerFormatters(conversionService);

        return conversionService;
    }

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

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

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}
<%@ 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://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>

<!DOCTYPE html>
<html>
<head>
    <c:url value="/" var="contextPath" />
      <meta charset="utf-8"/>
          <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
          <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
        <link rel="icon" href="//cdn.shopify.com/s/files/1/0155/7645/t/177/assets/favicon.ico?11981592617154272979" type="image/ico" />
        <link href="https://plus.googlecom/108540024862647200608" rel="publisher"/>
        <title>${produto.titulo} - Casa do Código</title>
        <link href="../../../resources/css/cssbase-min.css" rel="stylesheet" type="text/css" media="all" />
        <link href='http://fonts.googleapis.com/css?family=Droid+Sans:400,700' rel='stylesheet'/>
        <link href="../../../resources/css/fonts.css" rel="stylesheet" type="text/css" media="all" />
        <link href="../../../resources/css/fontello-ie7.css" rel="stylesheet" type="text/css" media="all" />
        <link href="../../../resources/css/fontello-embedded.css" rel="stylesheet" type="text/css" media="all" />
        <link href="../../../resources/css/fontello.css" rel="stylesheet" type="text/css" media="all" />
        <link href="../../../resources/css/style.css" rel="stylesheet" type="text/css" media="all" />
        <link href="../../../resources/css/layout-colors.css" rel="stylesheet" type="text/css" media="all" />
        <link href="../../../resources/css/responsive-style.css" rel="stylesheet" type="text/css" media="all" />
        <link href="../../../resources/css/guia-do-programador-style.css" rel="stylesheet" type="text/css"  media="all"  />
        <link href="../../../resources/css/produtos.css" rel="stylesheet" type="text/css"  media="all"  />
        <link rel="canonical" href="http://www.casadocodigo.com.br/" />    
</head>

O projeto está organizado conforme árvore abaixo:

src main webapp arquivos-servidorresources css imagens WEB-INF views carrinho produtos detalhe.jsp

4 respostas

Bom, acho que vc pode fazer assim: apaga esse método:

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

Esse aqui já faz o spring delegar a chamada para o container, caso não encontre algum recurso.

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

Coloca seus arquivos css/js/imagens em alguma pasta dentro de webapp e fora do WEB-INF. Agora, na página, vc vai fazer assim:

<link href="${contextPath}/nomeDaSuaPastaCss/cssbase-min.css" rel="stylesheet" type="text/css" media="all" />

Aí tenta aplicar a mesma lógica para os outros.

Agora funcionou, muito obrigado!

Necessita extender WebMvcConfigurerAdapter na classe AppWebConfiguration e adicionar o seguinte método:

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

Estou com o mesmo problema. Ja tentei todas as alternativas citadas, mas sem sucesso.

Alguem me ajuda por favor?