当只是返回到一个值是,就用@ResponsBody,当返回到一个页面时,就不用@ResponsBody了。
package com.example.control; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; @Controller public class MyController { //返回一个对象。 @RequestMapping("/hehe") public @ResponseBody Object hehe(){ Map<String,Object> retMap=new HashMap<String,Object>(); retMap.put("id",1002); retMap.put("username","lili"); return retMap; } //转向到一个存在的jsp页面 @RequestMapping("/show") public String show(Model model){ model.addAttribute("id",1002); model.addAttribute("username","aabb"); return "show"; } }
浙公网安备 33010602011771号