我遇到的問題已解決,方法如下:
原連結位址 https://blog.csdn.net/panguang9713/article/details/76184927
遇到這個問題總結一下:
1. springmvc的配置檔案沒有被加載,是以這裡特别說明一下,如果您使用的是maven項目,并且在web.xml檔案中配置的springmvc配置檔案在classpath下,務必将配置檔案放在resource目錄而不是java目錄下,您不妨打開target目錄或者tomcat部署的目錄看classes目錄下是否有該配置檔案。
2. 開啟spring的注解功能,直接在springmvc的配置檔案的beans節點中加入(mvc命名空間後面源碼有有)用來開啟注解
3. 确認掃描的包名沒有錯
4. 如果你發現通路一個url後控制器執行了但是還是404并且還是如題所示的錯誤,那麼你就要注意了,原因是控制器傳回的視圖又被dispatcherservlet當做控制器通路了,當他發現沒有找到對應的控制器就會傳回404,而你要傳回的視圖是個靜态資源,此時在springmvc的配置檔案中添加
<mvc:default-servlet-handler/>
或者resouce節點即可解決問題
這裡貼上springmvc配置檔案的源碼
<?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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--開啟注解-->
<mvc:annotation-driven/>
<!--通路靜态資源(假如沒找到相應的控制器就去找靜态資源,如果都沒有則傳回404)-->
<mvc:default-servlet-handler/>
<!--配置要自動掃描的包-->
<context:component-scan base-package="com.jhinwins.*"></context:component-scan>
<!-- 視圖解析(不必須) -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"></property>
<property name="suffix" value=".html"></property>
</bean>
</beans>
下文連結位址 https://blog.csdn.net/u010738061/article/details/48136747
1、控制層沒有被spring掃描到,控制層沒有執行個體化,檢查自己的控制層是否被spring掃描到,檢查以下配置:<context:component-scan base-package=""/>
2、web.xml配置檔案的<url-pattern>标簽配置成<url-pattern>/*</url-pattern>,正确的配置應該是<url-pattern>/</url-pattern>。學了structs之後再學spring mvc架構的 就會很容易犯這個錯誤。
3、使用的是注解執行個體化控制層,且web.xml的路徑配置為<url-pattern>/</url-pattern>,卻沒有在spring的配置檔案使用<mvc:annotation-driven />标簽,導緻URL解析出錯,檢查是否加上<mvc:annotation-driven />。