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

新手上路,请多包涵

我想在我的 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 文件夹中。

原文由 user2409128 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 878
2 个回答

首先,您需要像这样在 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 文件。

希望这能解决您的问题。

原文由 Atul Sharma 发布,翻译遵循 CC BY-SA 3.0 许可协议

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"/>

我希望它会起作用。

原文由 Saurabh Gaur 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题