今天解決一個java中的springmvc的問題,所有配置都是對的,主頁面也能打得開,唯獨Controller層的方法打不開,一直報http404錯誤
package com.gold.controller; import java.util.HashMap; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/") public class PersonController { // @Autowired // Idepart departservice; // @RequestMapping("getDeparts") // public Object getDeparts(){ // // //獲取所有的部門 // List<depart> departs=departservice.getAll(); // // // } @RequestMapping("test") @ResponseBody public Map<String,Object> test(){ Map<String,Object> map = new HashMap<String,Object>(); map.put("code", true); return map; } }
打入網址:http://localhost:8080/GoldMailList/test就一直報http404錯誤
后來在一同事提醒下,檢查了web.xml
<servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
所以應該打入http://localhost:8080/GoldMailList/test.do
OK搞定