【问题标题】:How to load CSS, JS, images in JSP in Spring Maven project?Spring Maven项目中如何在JSP中加载CSS、JS、图片?
【发布时间】:2017-09-16 13:11:56
【问题描述】:
 `<%@ 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 页面中加载所有静态资源。 我尝试从所有来源和不同的方式做:&lt;mvc:resources location="/resources/**" mapping="/resources/" /&gt; &lt;link rel='stylesheet' href='&lt;c:url value="/resources/css/epds1.css" /&gt;' /&gt; 但仍然没有为我工作。

【问题讨论】:

  • &lt;link rel='stylesheet' href='&lt;c:url value="/resources/css/epds1.css" /&gt;' /&gt; 像您一样应该可以正常工作。可能还有其他问题,但您需要包含您的 spring 配置和 pom.xml 以找出原因。
  • 还要验证你的jsp开头有&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
  • 是的,我添加了你上面提到的所有代码,但问题仍然存在
  • 您能否在问题中添加您在 jsp 中有什么以及 HTML 输出是什么?

标签: spring maven jsp


【解决方案1】:

尝试将其添加到您的 .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">

【讨论】:

  • 不,它对我没有用 Adrian web,在运行时这是 jsp 页面中的路径渲染 添加你的代码后
猜你喜欢
  • 1970-01-01
  • 2015-10-15
  • 2020-04-15
  • 1970-01-01
  • 2023-03-22
  • 2016-11-18
  • 2020-03-01
  • 2021-06-27
  • 2016-12-19
相关资源
最近更新 更多