1

I just started learning spring framework.
I have met a problem and decided to share it to help you.
First I added the project bootstrap library for a project.
Then I downloaded a free template from the Internet. I put the resources of this template in the project in the following location (see WebContent / WEB-INF / view / resources)

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%> 
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">
  <title>SB Admin - Start Bootstrap Template</title>
  <!-- Bootstrap core CSS-->
  <link href="${contextPath}/WebContent/WEB-INF/view/resources/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <!-- Custom fonts for this template-->
  <link href="${contextPath}/WebContent/WEB-INF/view/resources/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
  <!-- Custom styles for this template-->
  <link href="${contextPath}/WebContent/WEB-INF/view/resources/css/sb-admin.css" rel="stylesheet">
</head>

Then I copied the codes of the login page from the source code into the project and defined the locations of the files used for HTML at the top.(you can see codes below)

When I compile and run my code, I do not have libraries added and I see a page with simple HTML code. What is wrong with me? How can I fix this problem? If you tell me my mistakes and you help me, I'm happy. Everyone thanks in advance

fatih
  • 67
  • 1
  • 12
  • Try changing `${contextPath}/WebContent/WEB-INF/......` to `${contextPath}/......` – Jorge Campos Apr 03 '18 at 22:01
  • I tried your suggestion but I still have a simple html code without resources. Is there another way? – fatih Apr 04 '18 at 09:28
  • did you add the 'view' in the path? Like `${contextPath}/view/resources/........` – Jorge Campos Apr 04 '18 at 12:02
  • yes I add the ‘view’ this -> link=href=“${contextPath}/view/resources/.... but it didn’t work – fatih Apr 04 '18 at 12:07
  • What does ´${contextPath}´ results in the html code? Inspect it and see – Jorge Campos Apr 04 '18 at 12:09
  • @JorgeCampos I see result in html code my project name: “Lomis” – fatih Apr 04 '18 at 12:11
  • And also see this thread. I think your question is really a duplicate of it: https://stackoverflow.com/questions/4764405/how-to-use-relative-paths-without-including-the-context-root-name?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Jorge Campos Apr 04 '18 at 12:11

1 Answers1

0

I solved the problem. First, in my project "application-context.xml" I added the following code to the page. I moved my template files to "/ WebContent / resources / thema /" :

 <mvc:resources mapping="/resources/**" location="/resources/thema/"  
cache-period="31556926"/

I defined contextPath :

<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

Don't forget to define this library for this :

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

After, I changed the path to the template folder in my .jsp file as follows:

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>SB Admin - Start Bootstrap Template</title>
<!-- Bootstrap core CSS-->
<link href="${contextPath}/resources/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom fonts for this template-->
<link href="${contextPath}/resources/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- Custom styles for this template-->
<link href="${contextPath}/resources/css/sb-admin.css" rel="stylesheet">
</head>

This solved my problem.

I got help here for the solution:

Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP

and

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

Thank you for your good help : Jorge Campos

fatih
  • 67
  • 1
  • 12