SpringMVC請求處理方法的返回值:
ModelAndView
Model
Map包含模型和屬性
View
代表邏輯視圖名的String
void
提供對Servlet的訪問,以響應Http頭部和內容的HttpEntity或ResponseEntity
Callable
DeferredResult
其他任意類型,Spring將其視作輸出給View的對象模型
返回Json:
package com.fieldsignserver.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
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 org.springframework.web.servlet.view.json.MappingJacksonJsonView;
@Controller
@RequestMapping(value="/admin")
public class LoginController {
@ResponseBody
@RequestMapping(value="/login",method=RequestMethod.GET)
public ModelAndView login(@RequestParam("username") String username,@RequestParam("password") String password){
Map result = new HashMap<String,String>();
System.out.println("Username : "+username+", Password :"+password);
result.put("result", "success");
return new ModelAndView(new MappingJacksonJsonView(),result);
}
}