如何在Spring Maven项目中加载JSP中的CSS,JS,图像?

时间:2017-09-16 13:11:56

标签: spring maven jsp

 `<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
            <html>
            <head>
             <link href='<c:url value="/resources/css/epds1.css" />' rel='stylesheet'>


            <title>FOOD SECURITY CARDS</title>
            </head>`[![This is My Jsp Page][ In this Page i need load js ,css,images]][i tried doing from all sources but no use]
       ----------
    **Spring Application config file**

     `<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd    
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx.xsd">

            <context:annotation-config />
            <context:component-scan base-package="com.nic.controller"></context:component-scan>
            <bean id="viewResolver"
                      class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
                      <property name="prefix">
                          <value>/WEB-INF/views/</value>
                       </property>
                      <property name="suffix">
                         <value>.jsp</value>
                      </property>
                </bean>


            <mvc:annotation-driven />
            <mvc:resources location="/resources/**" mapping="/resources/" />`

以上是项目结构。我想知道如何在Spring JSP页面中加载所有静态资源。 我尝试从所有来源和不同的方式做到:<mvc:resources location="/resources/**" mapping="/resources/" /> <link rel='stylesheet' href='<c:url value="/resources/css/epds1.css" />' />但仍然没有为我工作。

1 个答案:

答案 0 :(得分:0)

尝试在.jsp文件中添加:

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

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

然后您可以访问.css文件

<link href="${contextPath}/resources/css/common.css" rel="stylesheet">