首页 > 解决方案 > Spring - [CSS, JS, Images] 我的超棒风格在哪里?

问题描述

我是春天的新手。

我开始了一个家庭项目,我决定使用 Spring MVC 来处理我的 Servlet 等。我下载了一个引导主题(不用担心样式)。我有一个运行良好的 Servlet,但我想提高我的 Spring 技能,所以我开始将 Servlet 重新构建为 Spring MVC servlet。完成后,我意识到.. 我所有的 css、js 内容以及显然我的图像都没有加载到页面上。我尽可能快地寻找解决它的机会..但我卡住了。加载了一些 css 和 js,但是......我两天前还没有看到我很棒的基于引导程序的主题。我现在有点绝望。这里有一些代码,向你们展示我是如何解决这个问题的:web.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

spring-mvc-servlet.xml

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

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property value="/" name="prefix" />
    <property value=".jsp" name="suffix" />
</bean>

我的项目结构:

WebContent
META-INF
resources[folder]
    public[folder]
        contactform[folder]
            js files
        css[folder]
            css files
        img[folder]
            images, and folders with more images
        js[folder]
            one js file
        libraries[folder]
            folders with js files (and it contains css files as well(in css-named folders))
WEB-INF
    lib
    spring-mvc-servlet.xml
    web.xml

头部

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-2">
<title>...</title>
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<meta content="" name="keywords">
<meta content="" name="description">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,400i,600,700|Raleway:300,400,400i,500,500i,700,800,900" rel="stylesheet">
<link href="/resources/libraries/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link  href="/resources/libraries/nivo-slider/css/nivo-slider.css" type="text/css" rel="stylesheet">
<link  href="/resources/libraries/owlcarousel/owl.carousel.css" type="text/css" rel="stylesheet">
<link  href="/resources/libraries/owlcarousel/owl.transitions.css" type="text/css" rel="stylesheet">
<link  href="/resources/libraries/font-awesome/css/font-awesome.min.css" type="text/css" rel="stylesheet">
<link  href="/resources/libraries/animate/animate.min.css" type="text/css" rel="stylesheet">
<link  href="/resources/libraries/venobox/venobox.css" type="text/css" rel="stylesheet">
 <link  href="/resources/css/nivo-slider-theme.css" type="text/css" rel="stylesheet">
<link  href="/resources/css/style.css" type="text/css" rel="stylesheet">
<link  href="/resources/css/responsive.css" type="text/css" rel="stylesheet">

我在正文底部调用的 js 文件 :)

感谢您的回答,如果您需要更多信息,请告诉我.. 我会更新我的问题!

标签: javascriptjavacssspringspring-mvc

解决方案


终于解决了!

我一直在寻找很多..直到我找到解决方案。所以这里是我已经完成的步骤:

  1. 我已经将我所有的 css/js/image 内容移到了 WebContent/resources/...
  2. 我已经改变了 mvc servlet xml
  3. 最后..我改变了所有的链接和来源

所以这里是xml:

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property value="/" name="prefix" />
    <property value=".jsp" name="suffix" />
</bean>

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

和参考资料

<script src="${pageContext.servletContext.contextPath}/resources/libraries/jquery/jquery.min.js"></script>

所以,我希望这篇文章能帮助像我一样绝望的人。


推荐阅读