spring - jstl - 错误404 - 请求的资源不可用

时间:2014-01-12 21:36:46

标签: java spring spring-mvc servlets

当我想访问我的网站时,我遇到以下错误:

The requested resource is not available.

我注意到问题出在<mvc:resources mapping="/resources/**" location="../../resources/normalMode/"/>行的mvc-dispatcher-servlet.xml文件中,但我不知道为什么?

你有解决方案吗?

我的项目结构:

- src
  - main
    - java
      - com
        - myblog
          - controller
             -//all java files as controller
    - resources
       - normalMode
          - css
             - header.css
    - webapp
       - /WEB-INF
         - /pages 
           - mvc-dispatcher.xml
           - web.xml

MVC-调度-servlet.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">

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

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

    <property value="org.springframework.web.servlet.view.JstlView"
              name="viewClass" />

    <property name="prefix">
      <value>/WEB-INF/pages/</value>
    </property>

    <property name="suffix">
      <value>.jsp</value>
    </property>
  </bean>

  <mvc:resources mapping="/resources/**" location="../../resources/normalMode/"/>
</beans>

的web.xml

<!DOCTYPE web-app PUBLIC
   "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
   "http://java.sun.com/dtd/web-app_2_3.dtd" >

  <web-app>
     <display-name>Archetype Created Web Application</display-name>

     <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
           org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
     </servlet>

     <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
     </servlet-mapping>
</web-app>

IndexController.java

package com.myblog.controller;

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
@RequestMapping(value={"","/","home"})
public class IndexController {

    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView init(){
         return new ModelAndView("index");
    }
}

2 个答案:

答案 0 :(得分:1)

您需要添加

<mvc:annotation-driven/>

mvc-dispatcher-servlet.xml。通过此配置(以及组件扫描),Spring将扫描并注册使用@Controller注释的@RequestMapping方法。

使用您当前的配置,没有注册此类处理程序,因此无法处理您对/的请求。

答案 1 :(得分:0)

首先,如果你没有在类路径上加载.xml文件,你需要加载它们

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   classpath:spring/application-config.xml,
   /WEB-INF/conf/mvc-config.xml 
  </param-value>
 </context-param>

现在您需要执行以下操作以使用注释启用spring mvc

 <context:component-scan base-package="com.ekiras" />
 <mvc:annotation-driven />
 <tx:annotation-driven />

您需要启用注释和组件扫描以扫描项目中的所有注释。

See a demo project setup with SpringMVC + Maven + Hibernate + Sitemesh