Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用SpringMVC在JSP中包含js和CSS_Java_Spring_Jsp_Spring Mvc - Fatal编程技术网

Java 如何使用SpringMVC在JSP中包含js和CSS

Java 如何使用SpringMVC在JSP中包含js和CSS,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,我想在jsp中包含js和css文件,但我不能这样做。我对SpringMVC的概念还不熟悉。很长一段时间以来,我一直在研究同一个话题。 我的索引页是这样的 <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style.css"/> <script type="text/java

我想在jsp中包含js和css文件,但我不能这样做。我对SpringMVC的概念还不熟悉。很长一段时间以来,我一直在研究同一个话题。 我的索引页是这样的

<!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>

有人能帮我吗?我很努力,但我没有得到任何解决办法。我已将我的js和css文件保存在WEB-INF文件夹中。

您无法直接访问WEB-INF文件夹下的任何内容。当浏览器请求您的CSS文件时,他们无法看到WEB-INF文件夹中的内容

尝试将文件
css/css
文件夹放在
WebContent

并在dispatcher servlet中添加以下内容以授予访问权限

<mvc:resources mapping="/css/**" location="/css/" />


对于js文件也是如此。A在此

上,将您的
style.css
直接放入
webapp/css
文件夹,而不是
WEB-INF
文件夹

然后将以下代码添加到您的
springdispatcherservlet.xml

<mvc:resources mapping="/css/**" location="/css/" />

然后将以下代码添加到jsp页面中

<link rel="stylesheet" type="text/css" href="css/style.css"/>


我希望它能起作用。

首先,您需要在dispatcher servlet文件中声明您的资源,如下所示:

<mvc:resources mapping="/resources/**" location="/resources/folder/" />
<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">

任何url映射为/resources/**的请求都将直接查找/resources/folder/

现在,在jsp文件中,您需要包含如下css文件:

<mvc:resources mapping="/resources/**" location="/resources/folder/" />
<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">

类似地,您可以包含js文件


希望这能解决您的问题。

您需要在dispatcher servelet文件中声明资源。下面是两个声明

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

在只使用spring而不使用spring mvc的情况下,请采用以下方法

在servlet dispatcher中放置以下内容

<mvc:annotation-driven />               
<mvc:resources mapping="/css/**" location="/WEB-INF/assets/css/"/>
<mvc:resources mapping="/js/**" location="/WEB-INF/assets/js/"/>

正如您将注意到的,样式表位置的/css,如果您没有spring mvc所需的文件夹结构,则不必位于/resources文件夹中,就像spring应用程序一样。这同样适用于javascript文件、字体(如果需要)等

然后,您可以按需要访问这些资源

<link rel="stylesheet" href="css/vendor/swiper.min.css" type="text/css" />
<link rel="stylesheet" href="css/styles.css" type="text/css" />


我相信有人会发现这很有用,因为大多数例子都是使用SpringMVC的

将css/js文件放在文件夹
src/main/webapp/resources
中。不要把它们放在
WEB-INF
src/main/resources

然后将这一行添加到spring-dispatcher-servlet.xml

<mvc:resources mapping="/resources/**" location="/resources/" />

在jsp页面中包含css/js文件

<link href="<c:url value="/resources/style.css" />" rel="stylesheet">

不要忘记在jsp中声明taglib

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

您应该将包含css和js文件的文件夹放入“webapp/resources”中。
如果将它们放在“src/main/java”中,则必须对其进行更改。这对我有用

如果使用基于java的注释,则可以执行以下操作:

 @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    }
静态文件夹在哪里

src  
│
└───main
    └───java
    └───resources
    └───webapp
        └───static
            └───css
            └───....



它不起作用。我也尝试过,但仍然是相同的输出。您可以在浏览器控制台中查看截获的路径。请解释使用这种方法的好处,如等。为什么Spring选择这种方式而不是webapps文件夹下的传统访问方式。也欢迎提供链接。