Data processing and jump

Time:2021-11-23

Result jump mode

ModelAndView

Set the modelandview object, and jump to the specified page according to the name of the view and the view parser.
Page: {view parser prefix} + VIEWNAME + {view parser suffix}

<!--  View parser -- >
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
     id="internalResourceViewResolver">
   <!--  Prefix -- >
   <property name="prefix" value="/WEB-INF/jsp/" />
   <!--  Suffix -- >
   <property name="suffix" value=".jsp" />
</bean>

Corresponding controller class

public class ControllerTest1 implements Controller {  public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
       //Returns a model view object
       ModelAndView mv = new ModelAndView();
       mv.addObject("msg","ControllerTest1");
       mv.setViewName("test");
       return mv;
  }
}

ServletAPI

By setting up the servlet API, you do not need a view parser.
1. Output through httpservletresponse
2. Redirection via httpservletresponse
3. Forwarding through httpservletresponse

@Controller
public class ControllerTest1 {

    @RequestMapping("/result/t1")
    public void test1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.getWriter().println("Hello!Spring by servlet API");
    }

    @RequestMapping("/result/t2")
    public void test2(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        resp.sendRedirect("/index.jsp");
    }

    @RequestMapping("result/t3")
    public void test3(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        //Forward
        req.setAttribute("msg","/result/t3");
        req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,resp);
    }

}

SpringMVC

Forward and redirect through spring MVC – no view parser is required;

Before testing, you need to comment out the view parser

//Forwarding and redirection via spring MVC - no view parser required
@Controller
public class ControllerTest2 {

    @RequestMapping("/rsm/t1")
    public String test1() {
        //Forward
        return "/index.jsp";
    }

    @RequestMapping("/rsm/t2")
    public String test2() {

        //Forward
        return "forward:/index.jsp";
    }

    @RequestMapping("/rsm/t3")
    public String test3() {
        return "redirect:/index.jsp";
    }

}

data processing

Process submitted data

1. The submitted domain name is consistent with the parameter name of the processing method

Submit data: http://localhost:8080/hello?name=hudu

Treatment method:

public class ControllerTest3 {
    @RequestMapping("/hello")
    public String test1(String name, Model model) {
//        System.out.println(name);
        model.addAttribute("msg",name);
        return "test";
    }
}

2. The submitted domain name is inconsistent with the parameter name of the processing method

Submit data: http://localhost:8080/hello?username=hudu

Treatment method:

@RequestMapping("/hello2")
    public String test2(@RequestParam("username") String name, Model model) {

        model.addAttribute("msg",name);
        return "test";
    }

3. Submitted is an object

It is required that the submitted form field and the attribute name of the object are consistent, and the parameter can use the object

1. Entity class

public class User {
   private int id;
   private String name;
   private int age;
   //Structure
   //get/set
   //tostring()
}

2. Submit data: http://localhost:8080/mvc04/user?name=hudu&id=1&age=15

3. Treatment method:

@RequestMapping("/user")
public String test3(User user){
   System.out.println(user);
   return "hello";
}

If an object is used, the parameter name passed by the front end must be consistent with the object name, otherwise it is null.

Data display to front end

First: through modelandview

public class ControllerTest1 implements Controller {  public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
       //Returns a model view object
       ModelAndView mv = new ModelAndView();
       mv.addObject("msg","ControllerTest1");
       mv.setViewName("test");
       return mv;
  }
}

The second is through modelmap

ModelMap

@RequestMapping("/hello")
public String hello(@RequestParam("username") String name, ModelMap model){
   //Encapsulates the data to be displayed in the view
   //Equivalent to req.setattribute ("name", name);
   model.addAttribute("name",name);
   System.out.println(name);
   return "hello";
}

Third: through model

Model

@RequestMapping("/ct2/hello")
public String hello(@RequestParam("username") String name, Model model){
   //Encapsulates the data to be displayed in the view
   //Equivalent to req.setattribute ("name", name);
   model.addAttribute("msg",name);
   System.out.println(name);
   return "test";
}

contrast

In short:
Model has only a few methods, which are only suitable for storing data, simplifying the operation and understanding of model objects;

Modelmap inherits linkedmap. In addition to implementing some of its own methods, modelmap also inherits the methods and features of linkedmap;

Modelandview can store data, set the returned logical view, and control the jump of the display layer.

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

Apache sqoop

Source: dark horse big data 1.png From the standpoint of Apache, data flow can be divided into data import and export: Import: data import. RDBMS—–>Hadoop Export: data export. Hadoop—->RDBMS 1.2 sqoop installation The prerequisite for installing sqoop is that you already have a Java and Hadoop environment. Latest stable version: 1.4.6 Download the sqoop installation […]