gpt4 book ai didi

javascript - 如何使用 spring Mvc 添加带有 jsps 的 css 和 js 文件

转载 作者:太空宇宙 更新时间:2023-11-04 08:46:31 27 4
gpt4 key购买 nike

我目前正在使用 spring MVC 开发一个基于 JavaEE spring 的应用程序,但是当我添加 css 和 js 文件时,jsps 不读取它们,经过一些研究后,我在我的调度程序 servlet 文件中添加了这一行

<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd">


<context:component-scan base-package="controllers"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>


</beans>

这是元素层次结构

enter image description here

最佳答案

试试这个,

在你的 jsp 页面上,如果你使用的是 jSTL 那么就这样做

首先包含jSTL标签库,

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

然后

    <link href="<c:url value="/resources/css/myCSSFile.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>

比如说,如果你已经在使用 Spring 标签库,那么你可以这样做吗,

首先引入标签库,如下

<%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>

然后这样做,

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

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

如果我在这里遗漏了要解释的内容,请告诉我

关于javascript - 如何使用 spring Mvc 添加带有 jsps 的 css 和 js 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43740408/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com