如何使用 spring MVC 在 JSP 中包含 js 和 CSS [英] How to include js and CSS in JSP with spring MVC

查看:36
本文介绍了如何使用 spring MVC 在 JSP 中包含 js 和 CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 jsp 中包含 js 和 css 文件,但我无法这样做.我对 spring MVC 的概念很陌生.很长一段时间以来,我一直在研究这个相同的主题.我的索引页是这样的

<头><link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style.css"/><script type="text/javascript" src="${pageContext.request.contextPath}/LoginPageScrip.js"><style type="text/css">身体 {背景图片:url("LoginPageBackgroundImage.jpg");}</风格><身体><h6>请在谷歌浏览器中登录</h6><h1 align="center">欢迎使用我的推特克隆</h1><div class="m" style="margin-left: 401px; margin-top: 70px;"><form method="post" action="LoginForExistingUser" onsubmit="return Validate(this);"><字段集><legend align="center">登录</legend><div class="a"><div class="l">用户名</div><div class="r"><INPUT type="text" name="userName">

<div class="a"><div class="l">密码</div><div class="r"><输入类型=密码"名称=密码">

<div class="a"><div class="r"><INPUT class="button" type="submit" name="submit"值="登录">

<i align="center" style="margin-left: 183px;">新用户?<a href="signup.html"><u>Signup</u></a></i></fieldset></表单>

而我的 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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package="com.csc.student"/><mvc:注解驱动/><!--<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" ><属性名称=前缀"><value>/WEB-INF/</value></属性><属性名称=后缀"><value>.jsp</value></属性></bean></豆类>

我的控制器是这样的.

package com.csc.student;导入 org.springframework.stereotype.Controller;导入 org.springframework.web.bind.annotation.RequestMapping;导入 org.springframework.web.bind.annotation.RequestMethod;导入 org.springframework.web.servlet.ModelAndView;@控制器公共类 StudentInfoController {@RequestMapping(value = "/indexPage.html", method = RequestMethod.GET)公共 ModelAndView getAdmissionFrom() {ModelAndView 模型 = new ModelAndView("indexPage");退货模式;}}

有人可以帮助我吗?我很努力地挣扎,但我没有得到任何解决方案.我将我的 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="样式表">

同样,您可以包含 js 文件.

希望这能解决您的问题.

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

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

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

解决方案

First you need to declare your resources in dispatcher-servlet file like this :

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

Any request with url mapping /resources/** will directly look for /resources/folder/.

Now in jsp file you need to include your css file like this :

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

Similarly you can include js files.

Hope this solves your problem.

这篇关于如何使用 spring MVC 在 JSP 中包含 js 和 CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆