2016-11-10 120 views
0

這是本web.xml文件servlet映射總是404錯誤

<!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> 

    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 

    <servlet> 
    <servlet-name>myservlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>myservlet</servlet-name> 
    <url-pattern>*.html</url-pattern> 
    </servlet-mapping> 
</web-ap> 

的applicationContext.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:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> 


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

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

</beans> 

,這是控制器

package com.tutorial.ejemplospring; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
public class MainController { 

    @RequestMapping("/main.html") 
    public ModelAndView mainPage() { 

     return new ModelAndView("main"); 
    } 

} 

我不知道爲什麼'http:localhost:8080/ejemplospring/mail.html'與網址格式不匹配.html ?,我收到404錯誤,我f我試着用/或/我得到同樣的錯誤。

+0

嘗試在你的'@RequestMapping(「/ main.html」)'中去掉「.html」。 –

+0

在web.xml中將 * .html標記更改爲/ pkoli

回答

0

嘗試取消註釋

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

似乎是沒有找到控制器,並且因此不登記的映射。

0

爲什麼你的ComponentScan註釋掉了?您需要啓用這個(去掉註釋),所以它發現你的@Controller

<!-- <context:component-scan base-package="com.tutorial.ejemplospring" />--> 

需求是:

<context:component-scan base-package="com.tutorial.ejemplospring" /> 
+0

雖然是未知的,但仍然存在相同的問題 – AFS

+0

您能否提供此路徑中的視圖?/WEB-INF/views/ – Mechkov

+0

main.jsp是我在/ WEB-INF/views中唯一的視圖 – AFS

0

第一個問題是,你必須啓用豆組件掃描被創建。

第二個是Spring MVC默認情況下會忽略URL中的擴張,因爲它是被用於特定目的,如.json/.xml將表明您在JSON or XML格式需要數據Spring MVC

所以你的網址應該是mail.html.someExtension即。 mail.html.do

如果你不想這樣做,你絕對必須擁有擴展名,那麼你可以使用下面的配置來啓用擴展映射。

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> 
    <property name="favorPathExtension" value="false" /> 
</bean> 

否則簡單的方法是隻使用一些簡單的URL/mail沒有任何擴展。