2014-01-12 50 views
2

當我想在我的網站訪問,我有以下錯誤:春 - JSTL - 錯誤404 - 請求的資源不可用

The requested resource is not available. 

我注意到這個問題是在MVC-調度 - 行<mvc:resources mapping="/resources/**" location="../../resources/normalMode/"/> -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"); 
    } 
} 
+0

對於那些要求你得到這個錯誤?什麼網址? –

+0

http:// localhost:8080/lbagno/ – user2274060

+0

lbagno是您的上下文路徑嗎?我們來看看你的web.xml。 –

回答

1

你需要

<mvc:annotation-driven/> 

添加到您的mvc-dispatcher-servlet.xml。通過此配置(和組件掃描),Spring將掃描並註冊您的@Controller方法,並使用@RequestMapping進行註釋。

使用您當前的配置,沒有這樣的處理程序正在註冊,因此沒有任何處理您的請求/

+0

我有一個問題。 ''和'' – user2274060

+0

@ user2274060''設置有什麼區別DispatcherServlet需要的bean。它會在你的上下文中找到任何具有「@ Controller」註釋併爲你的@ RequestMapping方法生成處理程序方法的bean。 ''掃描指定的軟件包中用@ Component(或其子類型'@ Repository','@ Service','@ Controller'等註解的類)並創建bean爲他們。所以,你需要'component-scan'來生成bean,並且你需要'annotation-driven'來處理它們。 –

+1

@ user2274060我看到您之前已經提出過幾個問題,但尚未接受任何答案。考慮upvoting /接受幫助你的答案。 –

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> 

現在你需要做以下使用註釋

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

您需要啓用註釋和組件掃描以掃描項目中的所有註釋。

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

相關問題