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"); return modelAndView; } //returns a ModelAndView object containing a model and a view; /** * Model a model object, * Mainly includes spring-packaged model and modelMap, as well as java.util.Map, * When no view is returned, the view name will be determined by requestToViewNameTranslator; * @return */ @RequestMapping(value="/index3",method=RequestMethod.GET) public Map<String, String> index3(){ Map<String, String> map = new HashMap<String, String>(); map.put("1", "1"); //map.put is equivalent to request.setAttribute method return map; } //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; } //By cooperating with @ResponseBody, the content or object is returned as the HTTP response body (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 generally 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. }Reprinted from: http://blog.csdn.net/sunhuwh/article/details/41727257
Explanation of several return forms of springMVC
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325658551&siteId=291194637
Recommended
Ranking