SpringMVC-- return type

Type as the return value 1.void

  If you approach Void just wrote the original meaning is almost the Servlet

@RequestMapping("/index*")
    public void firstRequest(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws ServletException, IOException {
        UserInfo info=new UserInfo();
        info.setUser_id(1);
        info.setUser_name ( "Joe Smith" );

        / * Request.setAttribute ( "the User", "Joe Smith");
         request.getSession().setAttribute("user",info);
        request.getRequestDispatcher("/jsp/index.jsp").forward(request,response);*/

        /**
         * Json format transfer
         */
        response.setCharacterEncoding("UTF-8");
        String value = JSON.toJSONString(info);
        response.getWriter().write(value);
    }

Type as the return value type 2.String

  The return value of type String, the general name for returning the view
  1. When the method returns the value Null , the default path as the request if there is no attempt to view /jsp/thread/secondRequest.jsp parser, if the return value is carried Null data only in JSON
  return method 2. when a character string of string , when the string is returned only view the logical view name, if the data is to be carried request, session, or Json
     if use Model ModelMap or transfer data, Model or ModelMap definitely a method to the Senate
  3. when the method returns a value added forward when the representatives forwarded, if written as redirect: xxxx represents the redirect, not a return to the view , but do not do it! ! ! ! ! !

@RequestMapping()
    public String secondRequest(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException {

       // return "index"

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

 Type as the return value 3.ModelAndView

  ModelAndView model is to carry data View page is the View

 

 @RequestMapping("/threadRequest*")
    public ModelAndView threadRequest(){
        ModelAndView mv = new ModelAndView ();
        mv.setViewName("index");
        mv.addObject("user","王五");
        return mv;
    }

 

Type as the return value 4.Object

   1. When the method returns Null value, the request path as the default view /jsp/thread/secondRequest.jsp If the parser does not attempt, if the return value Null carrying data only in JSON

   2. When the method returns a value of type String String is the logical name of the view

   3. When the return or collection of data objects , to use Json format string, optional fastJson manual conversion, may be used automatically converted jackson

 @RequestMapping("/fourthRequest")
    @ResponseBody    // response element return data, in addition to manually installed for use JSON format string Jackson 
    public Object fourthRequest () {
        List<UserInfo> userList=new ArrayList<>();
        UserInfo info=new UserInfo();
        info.setUser_id(1);
        info.setUser_name ( "Joe Smith" );
        UserInfo info2=new UserInfo();
        info2.setUser_id(2);
        info2.setUser_name ( "John Doe" );
        userList.add(info);
        userList.add(info2);
        return userList;
    }
}

  As used herein, the need to import dependent jackson:

<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.8</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.8</version>
    </dependency>

 

 

Guess you like

Origin www.cnblogs.com/xiao-ran/p/11826978.html