javaeye 阅读(43) 评论(0)
@Controller
@RequestMapping("/test")
public class TestController {
    @Autowired
    private UserService userService;
    /**
     * 返回普通字符串
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping("/demo0")
    public String demo0(Model model)
    {
        //model.addAttribute("name", "我是老张");
        return "我是老张";
    }

    /**
     * 返回JSON字符串
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping("/demo1")
    public User demo1(@PathVariable String userName) {
        User user = userService.getUser(userName);
        return user;
    }

    /**
     * 返回模板名
     * @param model
     * @return
     */
    @RequestMapping("/demo2")
    public String demo2(Model model)
    {
        model.addAttribute("name", "小张");
        return "test/demo2";
    }

    /**
     * 返回ModelAndView对象
     * @param mv
     * @return
     */
    @RequestMapping("/demo3")
    public ModelAndView demo3(ModelAndView mv)
    {
        mv.setViewName("test/demo3");
        mv.addObject("name", "小王");
        return mv;
    }

    /**
     * 直接跳转
     * @param mv
     * @return
     */
    @RequestMapping("/demo4")
    public ModelAndView demo4(ModelAndView mv)
    {
        mv.setViewName("redirect:/test/demo1");
        return mv;
    }
}

发表评论
切换编辑模式