Java 如何使用 spring MVC 在 JSP 中包含 js 和 CSS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26276603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include js and CSS in JSP with spring MVC
提问by user2409128
I want to include js and css files in my jsp, but I'm not able to do so. I'm new to the concept of spring MVC. For a long time, I've been working on this same topic. My index Page is like this
我想在我的 jsp 中包含 js 和 css 文件,但我无法这样做。我是 Spring MVC 概念的新手。很长一段时间以来,我一直在研究这个相同的主题。我的索引页是这样的
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style.css"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/LoginPageScrip.js">
</script>
<style type="text/css">
body {
background-image: url("LoginPageBackgroundImage.jpg");
}
</style>
</head>
<body >
<h6>Please login in google Chrome</h6>
<h1 align="center">Welcome to my Twitter Clone</h1>
<div class="m" style="margin-left: 401px; margin-top: 70px;">
<form method="post" action="LoginForExistingUser" onsubmit="return Validate(this);">
<fieldset>
<legend align="center">Login</legend>
<div class="a">
<div class="l">User Name</div>
<div class="r">
<INPUT type="text" name="userName">
</div>
</div>
<div class="a">
<div class="l">Password</div>
<div class="r">
<INPUT type="password" name="password">
</div>
</div>
<div class="a">
<div class="r">
<INPUT class="button" type="submit" name="submit"
value="Login">
</div>
</div>
<i align="center" style="margin-left: 183px;">New User? <a href="signup.html"><u>Signup</u></a></i>
</fieldset>
</form>
</div>
</body>
</html>
And my spring-dispatcher-servlet.xml is like this.
而我的spring-dispatcher-servlet.xml就是这样的。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<context:component-scan base-package="com.csc.student" />
<mvc:annotation-driven/>
<!--<bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />-->
<!-- <bean name="/welcome.html" class ="csc.csc.helloController.HelloController" /> -->
<bean id="viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name ="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
My controller is like this.
我的控制器是这样的。
package com.csc.student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class StudentInfoController {
@RequestMapping(value = "/indexPage.html", method = RequestMethod.GET)
public ModelAndView getAdmissionFrom() {
ModelAndView model = new ModelAndView("indexPage");
return model;
}
}
Can some one help me in this? I'm struggling very hard but I'm not getting any solution. I have kept my js and css file in WEB-INF folder.
有人可以帮助我吗?我很努力地挣扎,但我没有得到任何解决方案。我将我的 js 和 css 文件保存在 WEB-INF 文件夹中。
采纳答案by Atul Sharma
First you need to declare your resources in dispatcher-servlet file like this :
首先,您需要在 dispatcher-servlet 文件中声明您的资源,如下所示:
<mvc:resources mapping="/resources/**" location="/resources/folder/" />
Any request with url mapping /resources/** will directly look for /resources/folder/.
任何带有 url 映射 /resources/** 的请求将直接查找 /resources/folder/。
Now in jsp file you need to include your css file like this :
现在在jsp文件中你需要像这样包含你的css文件:
<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">
Similarly you can include js files.
同样,您可以包含 js 文件。
Hope this solves your problem.
希望这能解决您的问题。
回答by Santhosh
You cant directly access anything under the WEB-INF
foldere. When browsers request your CSS file, they can not see inside the WEB-INF folder.
您不能直接访问文件WEB-INF
夹下的任何内容。当浏览器请求你的 CSS 文件时,他们看不到里面的 WEB-INF 文件夹。
Try putting your files css/css
folder under WebContent
.
尝试将您的文件css/css
夹放在WebContent
.
And add the following in dispatcher servlet to grant access ,
并在调度程序 servlet 中添加以下内容以授予访问权限,
<mvc:resources mapping="/css/**" location="/css/" />
similarly for your js files . A Nice example hereon this
与您的 js 文件类似。一个很好的例子在这里对这个
回答by Saurabh Gaur
Put your style.css
directly into the webapp/css
folder, not into the WEB-INF
folder.
将你的style.css
直接放入webapp/css
文件夹,而不是放入WEB-INF
文件夹。
Then add the following code into your spring-dispatcher-servlet.xml
然后将以下代码添加到您的 spring-dispatcher-servlet.xml
<mvc:resources mapping="/css/**" location="/css/" />
and then add following code into your jsp page
然后将以下代码添加到您的jsp页面中
<link rel="stylesheet" type="text/css" href="css/style.css"/>
I hope it will work.
我希望它会起作用。
回答by rajeesh
you need declare resources in dispatcher servelet file.below is two declarations
您需要在调度程序 serverlet 文件中声明资源。下面是两个声明
<mvc:annotation-driven />
<mvc:resources location="/resources/" mapping="/resources/**" />
回答by Francis Benyah
In a situation where you are using just spring and not spring mvc, take the following approach.
在您只使用 spring 而不是 spring mvc 的情况下,请采用以下方法。
Place the following in servlet dispatcher
将以下内容放在 servlet 调度程序中
<mvc:annotation-driven />
<mvc:resources mapping="/css/**" location="/WEB-INF/assets/css/"/>
<mvc:resources mapping="/js/**" location="/WEB-INF/assets/js/"/>
As you will notice /css for stylesheet location, doesn't have to be in /resources folder if you don't have the folder structure required for spring mvc as is the case with a spring application.Same applies to javascript files, fonts if you need them etc.
正如您会注意到样式表位置的 /css,如果您没有 spring mvc 所需的文件夹结构,则不必位于 /resources 文件夹中,就像 spring 应用程序一样。同样适用于 javascript 文件,字体,如果你需要它们等等。
You can then access the resources as you need them like so
然后,您可以像这样根据需要访问资源
<link rel="stylesheet" href="css/vendor/swiper.min.css" type="text/css" />
<link rel="stylesheet" href="css/styles.css" type="text/css" />
I am sure someone will find this useful as most examples are with spring mvc
我相信有人会发现这很有用,因为大多数示例都使用 spring mvc
回答by pipi
Put your css/js files in folder src/main/webapp/resources
. Don't put them in WEB-INF
or src/main/resources
.
将您的 css/js 文件放在文件夹中src/main/webapp/resources
。不要将它们放入WEB-INF
或 中src/main/resources
。
Then add this line to spring-dispatcher-servlet.xml
然后将此行添加到 spring-dispatcher-servlet.xml
<mvc:resources mapping="/resources/**" location="/resources/" />
Include css/js files in jsp pages
在jsp页面中包含css/js文件
<link href="<c:url value="/resources/style.css" />" rel="stylesheet">
Don't forget to declare taglib in your jsp
不要忘记在你的jsp中声明taglib
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
回答by ImHereToHelpYou
You should put the folder containing css and js files into "webapp/resources". If you've put them in "src/main/java", you must change it. It worked for me.
您应该将包含 css 和 js 文件的文件夹放入“webapp/resources”。如果你把它们放在“src/main/java”中,你必须改变它。它对我有用。
回答by Denis Deniben
If you using java-based annotation you can do this:
如果您使用基于 Java 的注释,您可以这样做:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
}
Where static folder
其中静态文件夹
src
│
└───main
└───java
└───resources
└───webapp
└───static
└───css
└───....