Return type of Spring MVC
In fact, Spring's return type can support a variety of
1. ModelAndView
2. String
3. void
4. Map
5. Model
6. object (this is often used in app back-end development)
code show as below
package com.wangbiao.springMVC; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; 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.servlet.ModelAndView; import org.springframework.web.servlet.View; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; @Controller @RequestMapping("/return") public class HelloWorld extends MultiActionController{ /* * The return value is of type ModelAndView */ public ModelAndView sayHelloWorld(HttpServletRequest request, HttpServletResponse response) { String param = request.getParameter("param"); System.out.println("springMVC测试:helloWorld;"+param); ModelAndView mv = new ModelAndView(); User user = (User) this.getApplicationContext().getBean("user"); mv.addObject("content", "springMVC HelloWorld:"+param); mv.setViewName("springMVC/helloWorld"); ServletContext ctx = this.getServletContext(); return mv; } /* * The return value is of type String, and the view name is the return value */ @RequestMapping(value="/returnString",method=RequestMethod.GET) public String returnString(Model model) { model.addAttribute("test", "return string!!!"); System.out.println("springMVC测试:helloWorld;"); return "return/returnString"; } /* * The return value is of type void, since there is no return value * Its default display view name is the same as a segment in the url, namely returnVoid.jsp */ @RequestMapping("/returnVoid") public void returnVoid(Model model){ model.addAttribute("test", "return void!!!"); } /* * The return value is of type void, since the view name is not specified * Its default display view name is the same as a segment in the url, namely returnVoid.jsp */ @RequestMapping("/returnModel") public Model returnModel(Model model){ model.addAttribute("test", "return Model!!!"); return model; } /* * The return value is of type void, since the view name is not specified * Its default display view name is the same as a segment in the url, namely returnVoid.jsp */ @RequestMapping("/returnMap") public Map returnMap(Model model){ Map map = new HashMap<String,String>(); map.put("test", "return map!!!"); return map; } }
If it is to return Object, you can use @ResponseBody like this
@Controller public class LoginController { @Autowired private CmsUserSessionService cmsUserSessionService; @ResponseBody @RequestMapping(value = "/login", method = RequestMethod.POST) public ResultVO loginUser(final HttpServletRequest request, final UserLoginParam param) throws Exception { AssertUtil.notBlank(param.getUserName(), "userName is required"); AssertUtil.notBlank(param.getPassword(), "password is required"); final String userAgent = request.getHeader("User-Agent"); final String ip = RequestUtil.getRealIp(request); final ResultVO result = new ResultVO(); result.setData(cmsUserSessionService.login(param, userAgent, ip)); result.setMessage("Login success"); return result; } }
Or use @RestController, no need for @ResponseBody
@RestController public class UserSessionController extends BaseController { @Autowired private UserSessionService userSessionService; @Autowired private UserDao userDao; @RequestMapping(value = "/user/session", method = RequestMethod.POST) public ResultVO logoutUser(final Long userId, final Long logoutUserId) { final UserInfoDTO userInfo = userDao.findUserInfoByUserId(logoutUserId); Constants.sActivitiesLogger.info(" Action:[" + Constants.LOG_USER_LOGOUT + "] Payme ID:["+userInfo.getMemberId()+"]"); userSessionService.logoutAllSessions(logoutUserId, LogoutSessionRemark.CS_CASE.toValue()); return new SuccessVO(); } }