这是我遇到的问题:
启动服务器访问/testArea路径时,无法将map直接响应至页面,而是在视图解析器指定的路径中寻找以testArea为名称的视图页面。
此时@ResponseBody不起作用。
错误代码如下:
@Controller
public class AreaController {
@Autowired
private AreaService areaService;
@RequestMapping(value = "/testArea",method = RequestMethod.GET)
@ResponseBody
public Map testArea(Map<String, Object> map){
System.out.println("testArea");
List<Area> areas = areaService.findAll();
try{
map.put("areaList",areas);
map.put("areaSize",areas.size());
}catch (Exception e){
map.put("error",e.getMessage());
}
System.out.println("数据库已查询。。。");
return map;
}
}
原因:Map对象不能放在参数里创建,只能放在方法里new出来即可。
想想也是,直接放在参数无法指定是HashMap还是什么其他的Map,自然无法识别,最后只能访问默认的页面。
解决:(部分代码与上相同已省略)
@RequestMapping(value = "/testArea",method = RequestMethod.GET)
@ResponseBody
public Map testArea(){
Map<String, Object> map = new HashMap<String, Object>();
System.out.println("testArea");
//关键部分