Css not found html page - Springframework

Asked

Viewed 407 times

1

Hello.

I’m having trouble getting the css, js, fonts etc of my project on my pages. I am using Spring, typing the address of the css files in the browser but the system is not able to find the correct path. If anyone can help..

Structure of project folders

inserir a descrição da imagem aqui

application-context.xml

<context:component-scan base-package="com.gervasios.sgr" />

    <mvc:annotation-driven/>

    <mvc:view-controller path="/" view-name="index"/>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".html" />
    </bean>

    <mvc:resources mapping="/resources/**" location="/resources/" cache-period="120"/>

    <mvc:default-servlet-handler />

web xml.

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         metadata-complete="true" version="3.0">
    <display-name>sgr-application</display-name>
    <servlet>
        <servlet-name>Spring-Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/application-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring-Servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

login.html

<!DOCTYPE html>
<html lang="pt-BR">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Login</title>

    <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css"/>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/resources/css/bootstrap.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="/resources/fonts/font-awesome/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/resources/css/animate.min.css"/>
    <link rel="stylesheet" href="/resources/css/login.css"/>
    <link rel="stylesheet" href="/resources/css/custom.css">
    <link rel="stylesheet" href="/resources/css/icheck/flat/green.css">

    <script src="/resources/js/jquery-1.11.1.min.js"></script>

</head>

<body style="background:#F7F7F7;">
   //codigos.....
   //codigos.....
</body>
</html>

1 answer

1


Taken from this link:

http://www.mkyong.com/spring-mvc/spring-mvc-how-to-include-js-or-css-files-in-a-jsp-page/

1 - Place your static files in this webapp Resources folder

2 - Create spring Mapping

3 - Include the link in the view via JSTL tag c:url or Spring tag spring:url

Spring Resource Mapping

In your configuration file you will map where your static will be:

<mvc:resources mapping="/resources/**" location="/resources/seus-estaticos/" />

Including in the view

With JSTL tag

<head>
    <link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">
    <script src="<c:url value="/resources/js/jquery.1.10.2.min.js" />"></script>
    <script src="<c:url value="/resources/js/main.js" />"></script>
</head>

With sprint tag

<head>
    <spring:url value="/resources/css/main.css" var="mainCss" />
    <spring:url value="/resources/js/jquery.1.10.2.min.js" var="jqueryJs" />
    <spring:url value="/resources/js/main.js" var="mainJs" />

    <link href="${mainCss}" rel="stylesheet" />
    <script src="${jqueryJs}"></script>
    <script src="${mainJs}"></script>
</head>

I hope I helped! D

  • Thank you Daniel Beff, I switched to . jsp my pages and did as you described. It all worked out.

  • You’re welcome! I’m glad I could help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.