java - 如何使用 Spring MVC 在 JSP 中包含 js 和 CSS

标签 java spring jsp spring-mvc

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

我的 Controller 是这样的。

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 文件夹中。

最佳答案

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

希望这能解决您的问题。

关于java - 如何使用 Spring MVC 在 JSP 中包含 js 和 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26276603/

相关文章:

c# - C# 中的 Jersey Restful Web 服务(Java) 相当于什么?

java - spring security 中 auto-config=true 有什么用

java - 如何使用 Spring boot 应用程序连接具有不同 Ip 和凭据的 2 个 mySql 数据库

java - 如何将调用对象方法的 JSP servlet 转换为 JSTL?

java - JSP:使用 'out' ( jspWriter) 的委托(delegate)和 jsp includes 来改变表达式的行为

java - 通过jsp更新数据库表到jsp

java - HTTP Status 500 – Internal Server Error-JSP-Tomcat 服务器

java - Entity Beans 不允许定义 Map<String, Object> 属性

java - JAVA中如何从URL中获取不省略空格的字符串

java - Maven 构建失败并显示错误消息 Plugin org.apache.maven.plugins :maven-surefire-plugin:2. 12.4 或其依赖项之一无法解析