费马小定理
2017-05-06 02:42
采纳率: 100%
浏览 1.9k
已采纳

ssm框架静态文件引入报404,求大神指点

这是我的SpringMVC配置文件的一些信息:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<!-- 使spring扫描包下的所有类,让标注spring注解的类生效 -->
<context:component-scan base-package="cn.beike"/>

<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>



<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/" />

<mvc:default-servlet-handler />

这是控制台输出的一些信息:

  • (1440675 ms) - [DEBUG] 2017-05-06 10:32:29,019 org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'spring' processing GET request for [/Real_estate/login.action]
  • (1440677 ms) - [DEBUG] 2017-05-06 10:32:29,021 org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Matching patterns for request [/login.action] are [/**]
  • (1440678 ms) - [DEBUG] 2017-05-06 10:32:29,022 org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - URI Template variables for request [/login.action] are {}
  • (1440679 ms) - [DEBUG] 2017-05-06 10:32:29,023 org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/login.action] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@69d64cf4] and 1 interceptor
  • (1440680 ms) - [DEBUG] 2017-05-06 10:32:29,024 org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/Real_estate/login.action] is: -1
  • (1440681 ms) - [DEBUG] 2017-05-06 10:32:29,025 org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling
  • (1440682 ms) - [DEBUG] 2017-05-06 10:32:29,026 org.springframework.web.servlet.DispatcherServlet - Successfully completed request

这是web.xml的配置信息:

<?xml version="1.0" encoding="UTF-8"?>
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">

<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->

contextConfigLocation
classpath:applicationContext-*.xml

<!-- spring字符编码过滤器start-->
<filter>
    <!--① Spring 编码过滤器 -->
    <filter-name>encodingFilter</filter-name>
    <filter-class>
        org.springframework.web.filter.CharacterEncodingFilter
    </filter-class>
    <!--② 编码方式  -->
    <init-param> 
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <!--③ 强制进行编码转换 -->
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<!-- ② 过滤器的匹配 URL -->
<filter-mapping> 
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring字符编码过滤器end-->


<!-- Spring MVC配置 -->
<!-- ====================================== -->
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>



<!-- Spring配置 -->
<!-- ====================================== -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- log4j配置start -->
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- Spring 加载 Log4j 的监听 -->
<listener>
    <listener-class>
        org.springframework.web.util.Log4jConfigListener
    </listener-class>
</listener>
<!-- log4j配置end -->



<!--  


default
.jpg


default
.gif


default
.png


default
.js


default
*.css

-->


index.jsp


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • 坎特 2017-05-06 05:44
    已采纳

    web.xml 里加这些标签


    default
    .css


    default
    .jpg


    default
    .jpeg


    default
    .png


    default
    .js


    default
    .woff


    default
    *.html

    已采纳该答案
    打赏 评论
  • 费马小定理 2017-05-06 02:57

    当我注释掉



    网页可以正常显示

    打赏 评论
  • 毕小宝 2017-05-06 04:22

    首先,看了你的配置,对于静态资源不会走Spring的Servlet的,所以静态资源文件不会被拦截的。
    其次,报404错误,有可能是你的静态文件路径设置的不正确。浏览器的F12看下请求路径是什么,跟你项目的路径是否一致。

    打赏 评论
  • 费马小定理 2017-05-06 05:10

    图片说明
    图片说明

    这是controller:
    @RequestMapping(value="/login.action")
    private String login() {
    System.out.println("%%%%%%%%%%%%%%%%%%%%%%%");
    return "login";
    }

    还有就是执行之后控制台无法输出“%%%%%%%%%%%%%%%%%%%%%%%”也就是说无法进入controller中的login,但是当我注释掉配置文件中的
    <mvc:resources mapping="/images/**" location="/images/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:resources mapping="/css/**" location="/css/" />
    网页可以显示但是没有样式加入
    
    打赏 评论

相关推荐 更多相似问题