首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用spring MVC在JSP中包含js和CSS

如何使用spring MVC在JSP中包含js和CSS
EN

Stack Overflow用户
提问于 2014-10-09 18:53:32
回答 8查看 111.8K关注 0票数 25

我想在我的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>

我的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>

我的控制器是这样的。

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;
        }
    }

有人能在这方面帮我吗?我在苦苦挣扎,但我没有得到任何解决方案。我将js和css文件保存在WEB-INF文件夹中。

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2014-10-09 21:40:12

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

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

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

现在,在jsp文件中,您需要像这样包含css文件:

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

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

希望这能解决你的问题。

票数 23
EN

Stack Overflow用户

发布于 2014-10-09 20:57:00

style.css直接放入webapp/css文件夹,而不是WEB-INF文件夹。

然后将以下代码添加到您的spring-dispatcher-servlet.xml

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

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

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

我希望它能起作用。

票数 21
EN

Stack Overflow用户

发布于 2016-07-07 18:33:41

在只使用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 application.Same适用于javascript文件,如果你需要字体,等等。

然后,您可以根据需要访问这些资源,如下所示

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

我相信有人会发现这很有用,因为大多数示例都是用spring mvc编写的。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26276603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档