運行環境:jdk1.7.0_17 + tomcat 7 + spring 3.2.0 +mybatis 3.2.7+ eclipse,訪問路徑:http://localhost:8085/Springmvc_Mybits_store/queryItems.do
錯誤: 導致404 錯誤,一般是轉發路徑寫錯誤,還有是請求時候書寫錯誤找不到Handler仔細檢查路徑是否寫對,今天要講的錯誤,也可以說是比較粗心的犯的錯,但對於新手沒法找出來,在路徑對的情況下,我們訪問404錯誤,並且地址打印出來也是對的。
錯誤原因 :查看controller導入的包:原因就在這兩個包,由於現在還不能充分解釋,當時寫的時候是自動注入的包,注入是import org.springframework.web.portlet.ModelAndView;這是錯誤根本原因,找了資料大體解釋下這兩個的區別,在這兩個包里面的
ModelAndView 里面內容都是一樣的,這兩個是為適用不同的環境。
org.springframework.web.portlet.ModelAndView:是一個支持處理方法的返回類型:意味着spring有一個HandlerMethodReturnValueHandler
實現(ModelAndViewMethodReturnValueHandler
),它將接收類型的返回值ModelAndView
並處理它
org.springframework.web.servlet.ModelAndView:默認情況下沒有注冊的實現。
解決辦法:刪除原有的org.springframework.web.portlet.ModelAndView 導入org.springframework.web.servlet.ModelAndView
錯誤代碼:控制台無報錯,頁面404錯誤;
發送請求的日志文件:
1 DispatcherServlet with name 'springmvc' processing GET request for [/Springmvc_Mybits_store/queryItems.do] 2 Looking up handler method for path /queryItems.do 3 Returning handler method [public org.springframework.web.portlet.ModelAndView com.kjczwl.ssm.controller.ItemsController.queryItems() throws java.lang.Exception] 4 Returning cached instance of singleton bean 'itemsController' 5 Last-Modified value for [/Springmvc_Mybits_store/queryItems.do] is: -1 6 Creating a new SqlSession 7 Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7adc9dc9] 8 Fetching JDBC Connection from DataSource 9 Registering transaction synchronization for JDBC Connection 10 JDBC Connection [jdbc:mysql://localhost:3306/store, UserName=root@localhost, MySQL Connector Java] will be managed by Spring 11 ==> Preparing: SELECT * from items 12 ==> Parameters: 13 <== Total: 4 14 Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7adc9dc9] 15 Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7adc9dc9] 16 Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7adc9dc9] 17 Returning JDBC Connection to DataSource 18 44444444444444444444444444444 19 Rendering view [org.springframework.web.servlet.view.JstlView: name 'queryItems'; URL [/WEB-INF/pages/items/queryItems.jsp]] in DispatcherServlet with name 'springmvc' 20 Added model object 'modelAndView' of type [org.springframework.web.portlet.ModelAndView] to request in view with name 'queryItems' 21 Added model object 'org.springframework.validation.BindingResult.modelAndView' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'queryItems' 22 Forwarding to resource [/WEB-INF/pages/items/queryItems.jsp] in InternalResourceView 'queryItems' 23 Successfully completed request
相關代碼:
工程結構:
controller 代碼:
1 1 package com.kjczwl.ssm.controller; 2 2 3 3 import java.util.List; 4 4 5 5 import org.springframework.beans.factory.annotation.Autowired; 6 6 import org.springframework.stereotype.Controller; 7 7 import org.springframework.web.bind.annotation.RequestMapping; 8 8 import org.springframework.web.portlet.ModelAndView; 9 9 import com.kjczwl.ssm.po.ItemsCustom; 10 10 import com.kjczwl.ssm.service.ItemsService; 11 11 12 12 /** 13 13 *<p>package: com.kjczwl.ssm.controller</p> 14 14 *<p>Description:商品的controller(Handler) </p> 15 15 *<p>Company: Springmvc_Mybits_store</p> 16 16 *@author: 唐烈 17 17 * @date 2017下午5:30:58 18 18 */ 19 19 @Controller// 注解模式開發Controller 20 20 public class ItemsController { 21 21 // 注入 從service 中取得數據 22 22 @Autowired 23 23 ItemsService itemsService; 24 24 25 25 //注解掃描, 后面映射地址 可通過queryItems.action 訪問 26 26 @RequestMapping("/queryItems.do") 27 27 public ModelAndView queryItems()throws Exception{ 28 28 //得到數據 29 29 List<ItemsCustom> itemList = itemsService.findItemsList(null); 30 30 // 構造ModelAndView 31 31 ModelAndView modelAndView = new ModelAndView("itemsList"); 32 32 // 添加到內存區 外面直接“${} 條件表達式獲取” 33 33 modelAndView.addObject("itemList",itemList); 34 34 //轉發的路徑 modelAndView.setViewName("itemsList"); 35 35 return modelAndView; 36 36 } 37 37 }
springmvc 配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:mvc="http://www.springframework.org/schema/mvc" 8 xmlns:tx="http://www.springframework.org/schema/tx" 9 xmlns:aop="http://www.springframework.org/schema/aop" 10 xsi:schemaLocation="http://www.springframework.org/schema/beans 11 http://www.springframework.org/schema/beans/spring-beans.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context-3.2.xsd 14 http://www.springframework.org/schema/mvc 15 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 16 http://www.springframework.org/schema/tx 17 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 18 http://www.springframework.org/schema/aop 19 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> 20 <!-- =================================================================================== --> 21 <!-- 自動掃描注解 --> 22 <context:component-scan base-package="com.kjczwl.ssm.controller"> 23 <!-- 24 只掃描你規定的:<context:include-filter type="annotation" expression=""/> 25 不掃描你規定的:<context:exclude-filter type="annotation" expression=""/> 26 --> 27 </context:component-scan> 28 <!-- =================================================================================== --> 29 <!-- 注解驅動 30 代替: 31 處理器適配器 32 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> 33 處理器映射器 34 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> 35 --> 36 <mvc:annotation-driven/> 37 <!-- =================================================================================== --> 38 <!-- 視圖解析器 39 前綴:prefix 40 后綴:suffix 41 --> 42 <mvc:default-servlet-handler/> 43 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 44 <property name="prefix" value="/WEB-INF/pages/items/"/> 45 <property name="suffix" value=".jsp"/> 46 </bean> 47 </beans>