Spring MVC 视图解析 VIEW
转发和重定向:
/** * 转发 forward:资源路径 * 重定向 redirect:资源路径 */ @RequestMapping("demo8") public String demo8() { return "redirect:/login.jsp"; }
视图解析器:
/** * 如果在前天加了forward或redirect则会走默认解析器 * @return */ @RequestMapping("demo9") public String demo9() { return "forward:demo10"; } @RequestMapping("demo10") public String demo10() { return "login"; }
在SpringMVC.xml中配置:
<bean id="viewResole" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean>
@ResponseBody
如果方法上只有@Controller,会根据返回值进行跳转,
当加上@ResponseBody之后则不会进行跳转
/** * RequestMapping()无论返回值是什么都进行跳转 * 加ResponseBody则不会进行跳转 * 1.如果返回值是键值对(对象,map)会自动转化为json,要导入jsckson三个包 * 2.如果返回值不是健值对,则返回字符串 */ @RequestMapping(value="demo11",produces="text/html;charset=utf-8") @ResponseBody public String demo11() { return "中文11 "; } /** * 返回对象则自动转化为json */ @RequestMapping(value="demo12") @ResponseBody public Teacher demo12() { Teacher teacher = new Teacher(); teacher.setName("teacherName"); return teacher; }
<mvc:annotation-driven></mvc:annotation-driven>