0

Status: 解决了。

这是我的目录结构。

在此处输入图像描述

以及右侧的.war文件的其他目录结构和结构。

在此处输入图像描述


编辑 1 开始

我刚刚研究并看到了我的其他项目。它不是tomcat或jsp的问题。我在早期的项目中也做过类似的事情。唯一的区别是我在目录之外包含springmavenjsps、图像、脚本WEB-INF。但在web目录中

(但我可能错了)

我也得到WARNING: No mapping found for HTTP request with URI [/ttmaven/resources/images/person.png] in DispatcherServlet with name 'springDispatcher'

我认为这可能是过滤器或 url 模式的问题。 http://localhost:8084/ttmaven/resources/images/person.png 将请求传递给 servlet 并且该 servlet 没有正确处理它?

编辑 1 结束


我正在使用弹簧和控制器。

例如localhost/appName/login给我WEB-INF/view/jsp/login/login.jsp

prefix/view/jsp

我真的尝试了所有可能的组合来链接我的 jsps 中的图像和 .css 文件。例如在 login.jsp 中。

我总是得到404 error

即使是直接链接,例如ttmaven我的应用程序名。http://localhost:8084/ttmaven/resources/images/person.png不工作

链接的正确方法是什么?如果我也使用了spring security,如何允许所有人访问资源/**。

我的相关问题

pageContext.request.contextPath 和通用链接

jsp中的通用链接,变量和路径

web.xml(我注释掉了 spring 安全性的东西,以确保它不是 spring 安全权限,这是造成问题的)

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

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>springDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>



<!--    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>-->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
<!--            /WEB-INF/spring-security.xml-->
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
</web-app>

applicationContext.xml 主要只有:

 <import resource="springDispatcher-servlet.xml" />

springDispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
>


    <context:component-scan base-package="web" >
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"       />
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"    />
    </context:component-scan> 
  <mvc:annotation-driven />
<!--  <context:annotation-config />-->
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--          class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->

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

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController">
          <property name="viewName" value="index" />
    </bean>



     <!---
     ##########################################################################
    Hibernate

    -->
<!--    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="${jdbc.url}jdbc:postgresql://localhost:5432/postgres" />
        <property name="username" value="postgres" />
        <property name="password" value="abc" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="web.entity" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</prop>
            </props>

        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    ###########################################################
    -->


<!--

    JPA based instead of hibernate

-->
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<!-- 
        This configures the EntityManagerFactory object used for JPA/Spring managed persistent objects. 
     -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="persistence-unit-demo" /> 
        <property name="dataSource" ref="dataSource" />
                <property name="packagesToScan" value="web.entity" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                                <property name="database" value="POSTGRESQL" />
<!--    giving errors       <property name="databasePlatorm" value="org.hibernate.dialect.PostgreSQLDialect"/>-->
<!--                                <property name="database" value="HSQL" />-->  
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />                        
            </bean>
        </property>   
                <property name="jpaProperties">
                    <props>
<!--                        <prop key="hibernate.hbm2ddl.auto">create-drop</prop>-->
                            <prop key="hibernate.hbm2ddl.auto">update</prop>
                    </props>
                </property>                         
    </bean> 
    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    <!-- Pulls database connection from the tomcat container's context database pool via JNDI -->
<!--    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/mssqlserver" resource-ref="true"/>-->

        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
            <property name="driverClassName" value="org.postgresql.Driver" />
            <property name="url" value="jdbc:postgresql://localhost:5432/postgres" />
            <property name="username" value="postgres" />
            <property name="password" value="abc" />
        </bean>



    <!--
        Sets up our transaction manager. 
     -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="jpaDialect" ref="jpaDialect" />
        <property name="dataSource" ref="dataSource" />
<!-- giving errors               <property name="loadTimeWeaver">
                    <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
                </property>-->
    </bean>

        <!--
        Defines our transaction manager for Transactional annotations.
     -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory" />




        <!--Internationalization ########################## -->

        <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="en" />
    </bean>

    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="language" />
    </bean>

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
        <property name="interceptors">
           <list>
            <ref bean="localeChangeInterceptor" />
           </list>
        </property>
    </bean>



    <!-- Register the welcome.properties -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages" />
    </bean>


</beans>
4

2 回答 2

4

浏览器将直接请求的资源,例如图像、CSS 和 JavaScript,不能位于该/WEB-INF文件夹下。无法通过 Web 浏览器访问该文件夹。将您的 Web 资源上移到 webapp 文件夹。然后,使用 链接到它们${pageContext.request.contextPath}/images/person.png

您的结构应如下所示:

/webapp
  /css
  /images
    /person.png
  /scripts
  /WEB-INF
    /view
      /jsp

等等等等……

于 2013-08-04T05:37:11.673 回答
3

<mvc:resources mapping="/resources/**" location="/resources/" />找到解决方案:只需要添加springDispatcher xml

有用的链接:

不在 Spring MVC 中显示图像 http://www.coderanch.com/t/595112/Spring/find-css-images-js-static http://forum.springsource.org/showthread.php?97061-The-lt -mvc-resources-gt-does-not-work

间接帮助:

找不到带有 URI [/WEB-INF/pages/apiForm.jsp] 的 HTTP 请求的映射

于 2013-08-04T10:23:46.977 回答