Spring MVC 之返回类型
其实Spring的返回类型可以支持多种
1. ModelAndView
2. String
3. void
4. Map
5. Model
6. object(app后端开发经常会这么用)
代码如下
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{
/*
* 返回值为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;
}
/*
* 返回值为String类型的,视图名就是返回值
*/
@RequestMapping(value="/returnString",method=RequestMethod.GET)
public String returnString(Model model) {
model.addAttribute("test", "return string!!!");
System.out.println("springMVC测试:helloWorld;");
return "return/returnString";
}
/*
* 返回值是void类型的,由于没有返回值
* 它默认的展示视图名和url中的一段是一样的即returnVoid.jsp
*/
@RequestMapping("/returnVoid")
public void returnVoid(Model model){
model.addAttribute("test", "return void!!!");
}
/*
* 返回值是void类型的,由于没有指定视图名
* 它默认的展示视图名和url中的一段是一样的即returnVoid.jsp
*/
@RequestMapping("/returnModel")
public Model returnModel(Model model){
model.addAttribute("test", "return Model!!!");
return model;
}
/*
* 返回值是void类型的,由于没有指定视图名
* 它默认的展示视图名和url中的一段是一样的即returnVoid.jsp
*/
@RequestMapping("/returnMap")
public Map returnMap(Model model){
Map map = new HashMap<String,String>();
map.put("test", "return map!!!");
return map;
}
}
如果是返回Object,可以这么用@ResponseBody
@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;
}
}
或者用@RestController,就不需要@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();
}
}