[英]how can I add css and js files with jsps with spring Mvc
I'm currently developing a JavaEE spring based application using spring MVC but when I added css and js files,the jsps doesn't read them,after some researches I added this line on my dispatcher servlet file 我目前正在使用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>
and this is the project hierarchy 这是项目层次结构
try this, 尝试这个,
on your jsp page, if you are using jstl then do this 在您的jsp页面上,如果您使用的是jstl,请执行此操作
First include the jstl tag library, 首先包括jstl标记库,
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
then 然后
<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>
Say, if you are already using Spring tag library then can you do like this, 说,如果您已经在使用Spring标记库,那么您可以这样做吗?
First include the tag library, as bellow 首先包括标签库,如下
<%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>
and then do this, 然后这样做
<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>
Do let me know If i missed something to explain here 如果我错过了一些要解释的内容,请告诉我
You need to add the css/js files to <script>
and <link>
tags in the .jsp's themselves like you would an HTML file. 您需要像在HTML文件中一样,将css / js文件添加到.jsp自身的
<script>
和<link>
标记中。
CSS and JS are both client side and need to be sent / loaded on the client rather than the server. CSS和JS都是客户端,需要在客户端而不是服务器上发送/加载。
Edit to be a bit more clear: JSPs are "formatted" on the backend (Spring) and then sent to the client as if it were static HTML, so loading the JS/CSS on the server would be like running all the client code on the server, stop running the client code, then send the static page to the client. 编辑得更清楚一点:JSP在后端(Spring)上被“格式化”,然后像静态HTML一样被发送到客户端,因此在服务器上加载JS / CSS就像在客户端上运行所有客户端代码一样。服务器,停止运行客户端代码,然后将静态页面发送给客户端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.