java - Spring MVC無(wú)法識(shí)別Controller導(dǎo)致返回的結(jié)果是404?
問(wèn)題描述
運(yùn)行環(huán)境java8 + idea2016 + spring mvc4.3 + maven3.3 + tomcat8 + ubuntu
報(bào)錯(cuò)信息訪(fǎng)問(wèn)localhost:8080/HelloWeb/hello或者localhost:8080/hello都會(huì)返回404的狀態(tài)碼:
Web.xml
<web-app 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 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;@Controllerpublic 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>補(bǔ)充
我猜測(cè)原因是spring mvc沒(méi)有識(shí)別@Controller注解,但是我在HelloWeb-servlet.xml中已經(jīng)聲明了<context:component-scan base-package='org.neo.sml'/>,請(qǐng)問(wèn)發(fā)生錯(cuò)誤的原因可能是什么?
按照@傅易君的提示,參考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 id='internalResourceViewResolver'><property name='prefix' value='/WEB-INF/jsp/'/><property name='suffix' value='.jsp'/> </bean></beans>
問(wèn)題解答
回答1:把HelloWeb-servlet.xml放到resources里面,web.xml里面改為<servlet>
<servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:HelloWeb-servlet.xml</param-value> </init-param> </servlet>
配置HelloWeb-servlet.xml中聲明了<context:component-scan base-package='org.neo.sml'/>訪(fǎng)問(wèn)路徑寫(xiě)http://localhost:8080/sml/hello就可以了
回答2:controller打個(gè)斷電,看看是沒(méi)進(jìn)controller嗎?
回答3:試試 localhost:8080/sml
回答4:先開(kāi)啟注解支持:
<mvc:annotation-driven />回答5:
主要有以下幾個(gè)方面需要注意:1.在web.xml文件中是否配置xml文件的位置,保證HelloWeb-servlet.xml在src目錄下,配置如下:
<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:HelloWeb-servlet.xml</param-value> </init-param> </servlet>
2.在HelloWeb-servlet.xml文件中是否配置,如下所示:
<mvc:annotation-driven></mvc:annotation-driven> <context:component-scan base-package='cn'></context:component-scan> <bean class='org.springframework.web.servlet.view.InternalResourceViewResolver'><property name='prefix' value='/WEB-INF/jsps/'></property><property name='suffix' value='.jsp'></property> </bean>
3.項(xiàng)目啟動(dòng)的目錄是否正確,是否需要添加項(xiàng)目名稱(chēng);4.啟動(dòng)之后,目錄是否正確;
回答6:訪(fǎng)問(wèn)localhost:8080/HelloWeb/hello或者localhost:8080/hello都會(huì)返回404的狀態(tài)碼
這個(gè)路徑我想你沒(méi)輸對(duì)吧?為什么是HelloWeb而不是sml?一般不是項(xiàng)目的名稱(chēng)么,默認(rèn)是。或者,你找到tomcat的server.xml,找到如下配置。
<Context docBase='test' path='/test' reloadable='true' source='org.eclipse.jst.jee.server:test'/>
相關(guān)文章:
1. mysql - 在不允許改動(dòng)數(shù)據(jù)表的情況下,如何優(yōu)化以varchar格式存儲(chǔ)的時(shí)間的比較?2. javascript - Img.complete和img.onload判斷圖片加載完成有什么區(qū)別?3. java中返回一個(gè)對(duì)象,和輸出對(duì)像的值,意義在哪兒4. mysql 為什么主鍵 id 和 pid 都市索引, id > 10 走索引 time > 10 不走索引?5. css3 - 純css實(shí)現(xiàn)點(diǎn)擊特效6. docker網(wǎng)絡(luò)端口映射,沒(méi)有方便點(diǎn)的操作方法么?7. css - 網(wǎng)頁(yè)div區(qū)塊 像蘋(píng)果一樣可左右滑動(dòng) 手機(jī)與電腦8. 安全性測(cè)試 - nodejs中如何防m(xù)ySQL注入9. javascript - 關(guān)于apply()與call()的問(wèn)題10. python - 在sqlalchemy中獲取剛插入的數(shù)據(jù)id?
