Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Erro 404

Boa tarde!

Ao inspecionar uma página, que foi criada utilizando JSP e bootstrap, recebo o erro 404 para todos os scripts.

Ao ir no console do navegador, ocorre os seguintes erros:

GET http://localhost:8090/bootstrap.js 404 ()
GET http://localhost:8090/jquery.js 404 ()

Segue abaixo o código da minha JSP.

<%@ 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>
<meta charset="utf-8">
<meta name="viewport"
    content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Cadastro de motivo de devolução</title>

<c:url value="/resources/css" var="cssPath" />
<c:url value="/resources/js" var="javaScriptPath" />
<script src="${jsPath}/bootstrap.js"></script>
<script src="${jsPath}/jquery.js"></script>
<link rel="stylesheet" href="${cssPath}/bootstrap.css">
</head>

<body>

    <div class="container">

        <div>
            <header>
                <div id="principal" class="container">
                    <h1>Cadastro de motivo de devolução</h1>
                </div>
            </header>
        </div>

        <div class="container">
            <form action="" method="post">

                <div class="form-group">
                    <label for="id">ID</label> <input id="id" name="id" type="text"
                        class="form-control" readonly>
                </div>

                <div class="form-group">
                    <label for="descricao">Descrição</label> <input id="descricao"
                        name="descricao" type="text" class="form-control">
                </div>

                <div class="form-group">
                    <button type="submit" class="btn btn-primary">Salvar</button>
                </div>

            </form>
        </div>
    </div>
</body>
</html>
3 respostas

Segue abaixo código do AppWebConfigurator:

package br.com.sitiorecantodoqueijo.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
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.sitiorecantodoqueijo.controller.HomeController;

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

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

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

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

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

}

Segue abaixo código do ServletSpringMVC:

package br.com.sitiorecantodoqueijo.conf;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class ServletSpringMVC extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { AppWebConfiguration.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

Segue código do controller:

package br.com.sitiorecantodoqueijo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/cadastros/")
public class CadastrosLogistica {

    @RequestMapping("motivoDevolucao")
    public String motivoDevolucao() {
        return "formMotivoDevolucao";
    }

}

Conteúdo do pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>br.com.sitiorecantodoqueijo.devolucoes</groupId>
    <artifactId>devolucoes</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <finalName>gerenciador</finalName>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8
solução!

Olá Matheus,

a var da c:url está com nome de javaScriptPath

<c:url value="/resources/js" var="javaScriptPath" />

Só que na hora de chamá-la nos scripts ficou com outro nome, de jsPath:

<script src="${jsPath}/bootstrap.js"></script>
<script src="${jsPath}/jquery.js"></script>

Boa tarde! Consegui resolver. Foram duas alterações:

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

O código acima alterei para:

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

Na parte:

<c:url value="/resources/css" var="cssPath" />
<c:url value="/resources/js" var="javaScriptPath" />
<script src="${jsPath}/bootstrap.js"></script>
<script src="${jsPath}/jquery.js"></script>
<link rel="stylesheet" href="${cssPath}/bootstrap.css">

Eu alterei para:

<c:url value="/resources/css" var="cssPath" />
<c:url value="/resources/js" var="jsPath" />
<script src="${jsPath}/bootstrap.js"></script>
<script src="${jsPath}/jquery.js"></script>
<link rel="stylesheet" href="${cssPath}/bootstrap.css">