页面跳转
ServletAPI
通过设置ServletAPI , 不需要视图解析器 .
1、通过HttpServletResponse进行输出
2、通过HttpServletResponse实现重定向
3、通过HttpServletResponse实现转发
@Controller
public class ResultGo {
@RequestMapping("/result/t1")
public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
rsp.getWriter().println("Hello,Spring BY servlet API");
}
@RequestMapping("/result/t2")
public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
rsp.sendRedirect("/index.jsp");
}
@RequestMapping("/result/t3")
public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {
//转发
req.setAttribute("msg","/result/t3");
req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);
}
}
通过SpringMVC,无视图解析器
视图解析器的作用就是简化路径,没有视图解析器的时候就需要写路径的全称
@Controller
public class ResultSpringMVC {
@RequestMapping("/rsm/t1")
public String test1(){
//转发 无视图解析器
return "/index.jsp";
}
@RequestMapping("/rsm/t2")
public String test2(){
//转发二
return "forward:/index.jsp";
}
@RequestMapping("/rsm/t3")
public String test3(){
//重定向
return "redirect:/index.jsp";
}
}
通过SpringMVC,有视图解析器
有视图解析器,处理重定向,其他的页面可以省略前缀和后缀。
@Controller
public class ResultSpringMVC2 {
@RequestMapping("/rsm2/t1")
public String test1(){
//转发
return "test";
}
@RequestMapping("/rsm2/t2")
public String test2(){
//重定向
return "redirect:/index.jsp";
//return "redirect:hello.do"; //hello.do为另一个请求/
}
@RequestMapping("/hello")
public String hello(String name){
System.out.println(name);
return "hello";
}
}
数据处理
处理提交数据
- 提交的域名称和处理方法的参数名一致
@RequestMapping("/hello")
public String hello(String name){
System.out.println(name);
return "hello";
}
- 提交的域名称和处理方法的参数名不一致
@RequestMapping("/hello1")
public String hello1(@RequestParam("username") String name){
System.out.println(name);
return "hello";
}
3.提交的是一个对象
@RequestMapping("/user")
public String user(User user){
System.out.println(user);
return "hello";
}
数据显示到前端
- ModelAndView
通过此方法显示数据需要注册bean
<bean class="com.zhang.controller.ControllerTest1" id="/controllerTest1" />
public class ControllerTest1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//ModelAndView 模型和视图
ModelAndView mv = new ModelAndView();
//封装对象,放在ModelAndView中。Model
String aa = "HelloSpringMVC!";
mv.addObject("msg",aa);
//封装要跳转的视图,放在ModelAndView中
mv.setViewName("controllerTest"); //: /WEB-INF/jsp/controllerTest.jsp
return mv;
}
}
- ModelMap
通过http://localhost:8080/ControllerTest2/hello?username=zhangsan 访问
@RequestMapping("/ControllerTest2/hello")
public String hello(@RequestParam("username") String name, ModelMap model){
//封装要显示到视图中的数据
//相当于req.setAttribute("name",name);
model.addAttribute("name",name);
System.out.println(name);
return "hello";
}
- Model
通过http://localhost:8080/ControllerTest2/hello1?username=zhangsan访问
@RequestMapping("/ControllerTest2/hello1")
public String hello(@RequestParam("username") String name, Model model){
//封装要显示到视图中的数据
//相当于req.setAttribute("name",name);
model.addAttribute("msg",name);
System.out.println(name);
return "hello";
}