java8 idea2016 spring mvc4.3 maven3.3 tomcat8 ubuntu
訪問localhost:8080/HelloWeb/hello
或者localhost:8080/hello
都會返回404的狀態碼:
Web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Form Handling</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloWeb-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.neo.sml"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
HelloController
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(value="/hello")
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
我猜測原因是spring mvc沒有識別@Controller
注解,但是我在HelloWeb-servlet.xml
中已經聲明了<context:component-scan base-package="org.neo.sml"/>
,請問發生錯誤的原因可能是什麼?
按照@傅易君的提示,參考so上麵,我加入了<mvc:annotation-driven />
,但是還是不行:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="org.ziwenxie.sml"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
把HelloWeb-servlet.xml放到resources裡面,web.xml裡面改為
設定HelloWeb-servlet.xml中宣告了
存取路徑寫http://localhost:8080/sml/hello就可以了
controller打個斷電,看看是沒進controller嗎?
試試 localhost:8080/sml
先開啟註解支援:
主要有以下幾個面向要注意:
1.在web.xml檔中是否設定xml檔的位置,保證HelloWeb-servlet.xml在src目錄下,設定如下:
2.在HelloWeb-servlet.xml檔案中是否配置,如下所示:
3.專案啟動的目錄是否正確,是否需要新增專案名稱;
4.啟動之後,目錄是否正確;
這個路徑我想你沒輸對吧?為什麼是
HelloWeb
而不是sml
?一般不是项目的名称么,默认是。或者,你找到
tomcat的server.xml
,找到如下配置。