Introduction to the return value of the processor
- (1) Servlet returns the result to the browser
Forwarding: with data
Redirection: without data
Asynchronous: json data is sent to the js of the page, js organization label display
Servlet里面编写响应浏览器的代码都要调用response
- (2) The springmvc method has designed a variety of return value types
"ModelAndView
" void
"String: request forwarding, redirection, json data
The return value of the processor-ModelAndView
- (1) ModelAndView is a model view provided by SPringMVC
- (2) Function
Set data addObject (key, value)
setting page setViewName (logical view) is the name of the file
@Controller
public class Demo02ReturnController {
//public 返回值类型 方法名(参数类型 参数名){}
@RequestMapping(path = "demo01.action",method = {
RequestMethod.POST,RequestMethod.GET})//回显页面
public ModelAndView test01(){
//
Person p1 = new Person(1,"jack","1234");
Person p2 = new Person(2,"rose","1234");
List<Person> list = new ArrayList<Person>();
list.add(p1);
list.add(p2);
//使用ModelAndView实现请求转发
ModelAndView mv = new ModelAndView();
mv.addObject("list",list);
mv.setViewName("demo04_update_persons");//前缀+逻辑视图+后缀
//物理视图 真正的文件地址 /WEB-INF/jsp/demo04_update_persons.jsp
//物理视图 = 前缀+逻辑视图+后缀
return mv;
}
}
The return value of the processor-void
- (1) Write void as the return value type of the method.
At this time, write request as the parameter, call request with setting parameters and request forwarding or redirection - (2) Background code
- (3) Does not reflect the advantages of springmvc
Demo02ReturnController
@RequestMapping(path="demo02.action",method = {
RequestMethod.GET,RequestMethod.POST})
public void test02(HttpServletRequest req, HttpServletResponse resp)throws Exception{
//请求转发
req.setAttribute("name","请求转发");
req.getRequestDispatcher("demo05.jsp").forward(req,resp);
}
The return value of the processor-String
- (1) The method return value type is written on the String
method to return the page name - (2) What if you carry data?
Method parameters are written on Model, which is less View than ModelAndView
req.setAttriibute(key,val)
req....forward(req,resp)
》Model is a model class provided by SpingMVC, this class does not need to create objects by itself, the system automatically creates
- (3) Model role
Model can be used to pass parameters to the page - (4) Background code
model.addAttribute("item", item);
@RequestMapping(path="demo03.action",method = {
RequestMethod.GET,RequestMethod.POST})
public String test03(Model model){
//Model 可以设置数据,自动由视图解析器带到页面
model.addAttribute("data","wHelloWord");
return "success"; //逻辑视图 文件名
}
The return value of the processor-String-forwarding and redirection
- (1) What is a logical view and a physical view. The
page name is called a logical view, not a real page address,
but it can be spliced by a view resolver to get the real page address, that is, the physical view - (2) The return value can add instructions
"1: forward request forwarding
forward instruction: physical view
" 2: redirect redirection
redirect instruction: project access path + physical view
特点:视图解析器不对指令后的内容拼接前缀与后缀
- (3) Instructions can also access controller methods
@RequestMapping(path="demo04.action",method = {
RequestMethod.GET,RequestMethod.POST})
public String test04(Model model){
//Model 可以设置数据,自动由视图解析器带到页面
model.addAttribute("data","wHelloWord");
//return "forward:/WEB-INF/jsp/success.jsp"; //
//return "redirect:http://www.baidu.com"; //外网
//return "redirect:demo05.jsp"; //
return "redirect:demo01.action"; //
}