我遵循書中的指示「春天行動」對付用SpringMVC控制器無法與在SprinvMVC章請求
當我運行Tomcat服務器,並嘗試演示它返回錯誤,如:
HTTP狀態404 - 找不到
類型狀態報告
說明原始服務器沒有找到噸當前表示他瞄準資源或不願意透露存在的資源。
- 我使用的MacOS,tomcat9,的Intellij 2017.1,JDK 1.9 這裏是我的代碼。
web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spitter</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spitter</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
spitter.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">
<!--static resources getting-->
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources"/>
<!--use annotations to create the mapping between-->
<!-- url and class deal with request(Controller) -->
<mvc:annotation-driven/>
<!--scan the component and auto regist as bean-->
<context:component-scan base-package="com.springmvc"/>
<!--Use this bean to map the jsp file according to the name return by Controller-->
<!--It will automatically add the prefix and suffix to the name string-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
控制器:
package com.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by xwh on 29/3/2017.
*/
@Controller
public class HomeController {
// public static final int DEAFAULT_SPITTLES_PER_PAGE = 25;
public HomeController() {
System.out.println("-------HomeController init-------");
}
@RequestMapping("/")
public String showHomePage() {
System.out.println("-------showHomePage Method show-------");
return "home";
}
}
這裏是我的目錄圖片。 enter image description here
嘗試@RequestMapping(「/」)=> @RequestMapping(「/ Spring_no_2 /」) – Dongqing
嘗試url localhost:8080 /沒有「Spring_no_2」 – Simon