亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

java - Spring MVC無法識別Controller導致返回的結果是404?

瀏覽:59日期:2023-12-26 08:10:59

問題描述

運行環境

java8 + idea2016 + spring mvc4.3 + maven3.3 + tomcat8 + ubuntu

報錯信息

訪問localhost:8080/HelloWeb/hello或者localhost:8080/hello都會返回404的狀態碼:

java - Spring MVC無法識別Controller導致返回的結果是404?

具體配置項目結構

java - Spring MVC無法識別Controller導致返回的結果是404?

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>補充

我猜測原因是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 id='internalResourceViewResolver'><property name='prefix' value='/WEB-INF/jsp/'/><property name='suffix' value='.jsp'/> </bean></beans>

問題解答

回答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'/>訪問路徑寫http://localhost:8080/sml/hello就可以了

回答2:

controller打個斷電,看看是沒進controller嗎?

回答3:

試試 localhost:8080/sml

回答4:

先開啟注解支持:

<mvc:annotation-driven />回答5:

主要有以下幾個方面需要注意: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.項目啟動的目錄是否正確,是否需要添加項目名稱;4.啟動之后,目錄是否正確;

回答6:

訪問localhost:8080/HelloWeb/hello或者localhost:8080/hello都會返回404的狀態碼

這個路徑我想你沒輸對吧?為什么是HelloWeb而不是sml?一般不是項目的名稱么,默認是。或者,你找到tomcat的server.xml,找到如下配置。

<Context docBase='test' path='/test' reloadable='true' source='org.eclipse.jst.jee.server:test'/>

標簽: java
相關文章:
主站蜘蛛池模板: 日韩 欧美 中文字幕 不卡 | 日韩亚洲欧美一区二区三区 | 久久青青草原综合伊人 | 国产成人v片视频在线观看 国产成人v视频在线观看 | 国产精品中文 | 欧美黄免在线播放 | 影音先锋男人在线资源 | 成人理论 | 亚洲精品综合一区二区 | 国产v精品成人免费视频400条 | 伊人丁香婷婷综合一区二区 | 日韩国产欧美一区二区三区在线 | 亚洲色图综合网 | 国产尤物在线播放 | 婷婷色香五月激情综合2020 | 黄色网欧美 | 亚洲欧美综合色区小说 | 热99re久久精品精品免费 | 国产成人久久一区二区三区 | 美女制服一二三区 | 国产成人在线播放 | 亚洲涩涩涩 | 欧美日韩国产在线人成dvd | 五月国产综合视频在线观看 | 久色tv| 国产女人的一级毛片视频 | www成人国产在线观看网站 | 妖精视频一区二区三区 | 一区二区三区国产精品 | 亚洲精品高清在线 | 一级做片爱性视频免费 | 在线观看日韩视频 | 欧美一区二区三区在观看 | 欧美亚洲中日韩中文字幕在线 | 亚洲欧美一二三区 | 国产在线精品一区二区高清不卡 | 海外毛片 | 国产亚洲精品一区二区三区 | 欧美日韩中文国产一区 | 日韩毛片网 | 任你敢不一样精品的视频 |