首页 > 解决方案 > css文件不适用于我的jsp(maven/spring/hibernate项目)

问题描述

我创建了一个小项目来填写表格(姓名+国家),并在验证后列出数据库中的表格。该项目是一个带有 maven 的 Web 应用程序。我集成了 spring + hibernate 来管理我的数据库。但是,我无法将我的 css 文件应用于 jsp,也无法显示图像。我不知道如何解决我的问题。

项目结构:

在此处输入图像描述 以下是项目文件:

春天.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/db_person?useUnicode=true&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC" />
        <property name="username" value="root" />
        <property name="password" value="0324" />
    </bean>

    <!-- Hibernate 5 XML SessionFactory Bean definition-->
        <bean id="hibernate5SessionFactory"
            class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="mappingResources">
                <list>
                    <value>person.hbm.xml</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <value>
                    hibernate.dialect=org.hibernate.dialect.MySQLDialect
                </value>
            </property>
        </bean>

    <bean id="personDAO" class="com.oc.dao.PersonDAOImpl">
        <property name="sessionFactory" ref="hibernate5SessionFactory" />
    </bean>
</beans>

头.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<head>
    <link rel="stylesheet"
          href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
          integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
          crossorigin="anonymous">

    <!-- Test to include css -->

    <link rel="stylesheet" type="text/css" href="style/style.css">
    <link href="/resources/style/style.css" />
    <script src="<c:url value='/resources/style/style.css'/>"></script>
    <link rel="stylesheet" type="text/css" href="<c:url value='/resources/style/style.css'/>">

    <title>Annuaire</title>
</head>

表单.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<%@include file="include/head.jsp"%>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<ul class="nav nav-tabs">
    <li class="nav-item">
        <a class="nav-link active" href="form">Inscription</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="register">Listing</a>
    </li>

    <li class="nav-item">
        <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">exemple</a>
    </li>
</ul>
    <form method="post" action="register" class="col-lg-9">
        <div class="form-group">
            <label for="nom">Nom :</label>
            <input type="text" class="form-control" id="nom" name="nom" placeholder="ex: Jean-edouard">
        </div>
        <div class="form-group">
            <label for="pays">Pays :</label>
            <input type="text" class="form-control" id="pays" name="pays" placeholder="ex: France">
        </div>
        <button type="submit" class="btn btn-primary mb-2">Validation</button>
    </form>
<p><img src="../resources/img/a.jpg" alt="img"></p>
</body>
</html>

网页.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>form</servlet-name>
    <servlet-class>com.oc.servlets.Form</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>register</servlet-name>
    <servlet-class>com.oc.servlets.Register</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>form</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>register</servlet-name>
    <url-pattern>/register</url-pattern>
  </servlet-mapping>

</web-app>

样式.css:

body{
    background: #0c5460;
}
head{
    background: #721c24;
}
form{
    background: #066000;
}

编辑 :

添加:在spring.xml中

表单验证后:

exception

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0': Invocation of init method failed; nested exception is java.lang.IllegalStateException: WebApplicationObjectSupport instance [ResourceHttpRequestHandler ["/resources/"]] does not run in a WebApplicationContext but in: org.springframework.context.support.ClassPathXmlApplicationContext@29e8770d, started on Thu Feb 27 17:03:14 CET 2020
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1803)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
    org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
    com.oc.servlets.Register.doPost(Register.java:24)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
cause mère

java.lang.IllegalStateException: WebApplicationObjectSupport instance [ResourceHttpRequestHandler ["/resources/"]] does not run in a WebApplicationContext but in: org.springframework.context.support.ClassPathXmlApplicationContext@29e8770d, started on Thu Feb 27 17:03:14 CET 2020
    org.springframework.web.context.support.WebApplicationObjectSupport.getWebApplicationContext(WebApplicationObjectSupport.java:115)
    org.springframework.web.context.support.WebApplicationObjectSupport.getServletContext(WebApplicationObjectSupport.java:134)
    org.springframework.web.servlet.resource.ResourceHttpRequestHandler.initContentNegotiationStrategy(ResourceHttpRequestHandler.java:429)
    org.springframework.web.servlet.resource.ResourceHttpRequestHandler.afterPropertiesSet(ResourceHttpRequestHandler.java:347)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1862)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1799)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
    org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
    com.oc.servlets.Register.doPost(Register.java:24)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

在 WEB-INF 中添加 spring-web-config.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.oc" />

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

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

</beans>

标签: javacssspringmavenjsp

解决方案


我在您的代码中看不到任何资源映射配置。

您可以spring-web-config.xml在 WEB-INF 中添加以下内容

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

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


</beans>

来自这个 URL 模式的任何请求,Spring 都会从路径中/resources/**寻找资源。/resources/

在 JSP 中使用

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

or

<script src="<c:url value="/resources/js/main.js" />"></script>

请参阅此处了解更多详细信息

选项 2:

您可以尝试mvc:resource在现有的spring.xml


推荐阅读