搜尋

首頁  >  問答  >  主體

java - springmvc 请求无法到达controller,出现404错误

springmvc 请求无法到达controller,出现404错误

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>testmvc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>    
        <param-name>contextConfigLocation</param-name>    
        <param-value>classpath:springmvc-servlet.xml</param-value>    
    </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
  
    <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

springmvc-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: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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <mvc:annotation-driven />
    <context:component-scan base-package="com.iszhmj.controller" />
    
     
    <bean id="viewResolver" 
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/">
            </property>
            <property name="suffix" value=".jsp">
            </property>
    </bean>
    

</beans>

WelcomeController.java

package com.iszhmj.controller;

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

@Controller
@RequestMapping("/welcome")
public class WelcomeController {
    
    @RequestMapping("/test")
    public String index()
    {
        System.out.println("index");
        return "index";
    }

}

巴扎黑巴扎黑2802 天前591

全部回覆(8)我來回復

  • ringa_lee

    ringa_lee2017-04-18 10:52:19

    把Spring-MVC的debug日誌輸入來看看!

    回覆
    0
  • PHPz

    PHPz2017-04-18 10:52:19

    所以你訪問的url是怎麼寫的呢?

    回覆
    0
  • 高洛峰

    高洛峰2017-04-18 10:52:19

    推薦所有web開發者通讀一下http協定

    回覆
    0
  • ringa_lee

    ringa_lee2017-04-18 10:52:19

    springmvc-servlet.xml要放在src下吧?

    回覆
    0
  • 阿神

    阿神2017-04-18 10:52:19

    看下我的部落格吧 http://blog.csdn.net/yege2006...

    回覆
    0
  • PHP中文网

    PHP中文网2017-04-18 10:52:19

    設定檔裡新增<context:annotation-config/>試試

    回覆
    0
  • 阿神

    阿神2017-04-18 10:52:19

    依照這個設定 classpath:springmvc-servlet.xml springmvc-servlet.xml檔案需要放在src目錄下。 然後你確定你的專案可以啟動成功? ?

    回覆
    0
  • 迷茫

    迷茫2017-04-18 10:52:19

    應該是放在src下面的

    回覆
    0
  • 取消回覆