1,转发
@Controller
@RequestMapping("/hello")
public class Demo1 {
@RequestMapping("test1")
public String test1() {
System.out.println("你好mvc1");
return "forward:/hello2/demo1.jsp";
}
@RequestMapping("test2")
public String test2() {
System.out.println("你好mvc2");
return "/hello2/demo1.jsp";
}
}
2,重定向
@RequestMapping("test1")
public String test1() {
System.out.println("你好mvc1");
return "redirect:/go1.jsp";
}
@RequestMapping("test1")
public String test1() {
System.out.println("你好mvc1");
return "redirect:test1"; // 重定向到test1方法上
}
3,补充细节
- 增删改用重定向
- 查询用转发
4,视图解析器
mxv.xml配置
作用:获取到后端的返回值后,在前后拼接,/xxx.jsp
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
java测试
@Controller
@RequestMapping("/hello")
public class Demo1 {
@RequestMapping("/test1")
public String test1() {
return "go";
}
}