Several return methods of spring

package com.boventech.learning.controller; 
 
import java.util.HashMap; 
import java.util.Map; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.servlet.ModelAndView; 
 
import com.boventech.learning.entity.User; 
 
/**
* MVCReturn
* @author peng.xia
*
*/ 
@Controller 
@RequestMapping("/MVCReturn") 
public class SpringMVCReturnController { 
     
    @RequestMapping(value="/index1",method=RequestMethod.GET) 
    public ModelAndView index(){ 
        ModelAndView modelAndView = new ModelAndView("/user/index"); 
        modelAndView. addObject("name", "xxx"); 
        return modelAndView; 
    } 
    //For the ModelAndView constructor, you can specify the name of the returned page, or you can use the setViewName method to set the page you want to jump to; 
     
    @RequestMapping(value="/index2 ",method=RequestMethod.GET) 
    public ModelAndView index2(){ 
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.addObject("name", "xxx");   
        modelAndView.setViewName("/user/index"); 
    }      //The view of the response should also be the view of the request. Equivalent to void return.      //Return String      //Use through model 


     














     


    @RequestMapping(value="/index4",method = RequestMethod.GET) 
    public String index(Model model) { 
        String retVal = "user/index"; 
        User user = new User(); 
        user.setName("XXX"); 
        model.addAttribute("user", user); 
        return retVal; 
    } 
     
    //Return the content or object as the HTTP response body by cooperating with @ResponseBody (suitable for instant verification); 
    @RequestMapping(value = "/valid", method = RequestMethod.GET) 
    @ResponseBody 
    public String valid(@RequestParam(value = "userId", required = false) Integer userId, 
            @RequestParam(value = "name") String name) { 
        return String.valueOf(true);   
    } 
    //The return string represents a view name. At this time, if you need a model in the process of rendering the view, you can add a model parameter to the processor, and then add a value to the model in the method body, 
     
      
    @RequestMapping(method =RequestMethod.GET) 
    public void index5(){ 
        ModelAndView modelAndView = new ModelAndView(); 
        modelAndView.addObject("xxx", "xxx"); 
    } 
    //The returned result page is still: /type 
    //At this time, we usually are Write the return result in HttpServletResponse, if not, 
    //spring will use RequestToViewNameTranslator to return a corresponding view name. If a model is needed at this time, the processing method is the same as the case of returning a string. 
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326801162&siteId=291194637