首页 > 代码库 > Spring控制器响应(action)请求的几种处理方式,如返回JSON,MODEL MODELVIEW,STRING
Spring控制器响应(action)请求的几种处理方式,如返回JSON,MODEL MODELVIEW,STRING
Controller:
package **.web.controller; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.google.gson.JsonObject; import com.hunantv.ott.loganalysis.persistence.beans.TestBean; /** * this class just for example ,please read spring reference in detail. * * just for controller use case demo by xiaohelong */ import dwz.business.info.News; import dwz.business.info.NewsType; import dwz.persistence.BaseConditionVO; import dwz.web.BaseController; @Controller public class OttControlerTest extends BaseController{ static Logger log = Logger.getLogger( OttControlerTest.class.getName()); @RequestMapping("/Test") @ResponseBody public TestBean test (){ log.debug("Test function"); TestBean testBean=new TestBean(); testBean.setName("haha"); return testBean; } @RequestMapping("/TestList") @ResponseBody public List<TestBean> testList (){ log.debug("TestList function"); List<TestBean> testBeanList=new ArrayList<TestBean>(); TestBean testBean1=new TestBean(); testBean1.setName("haha"); TestBean testBean2=new TestBean(); testBean2.setName("hehe"); testBeanList.add(testBean1); testBeanList.add(testBean2); return testBeanList; } @RequestMapping("/TestJSONObject") public void testReturrnJSONWithoutBean(HttpServletRequest request,HttpServletResponse response) throws IOException{ log.debug("TestJSONObject function"); PrintWriter writer=response.getWriter(); JsonObject jsonObject=new JsonObject(); jsonObject.addProperty("name","hehe jsonobject"); writer.println(jsonObject.toString()); writer.close(); } @RequestMapping("/TestReturnPage") @ResponseBody public String testReturrnPage(){ log.debug("TestReturnPage"); return "/testpage"; } /** * The difference between model and modelandview is just sematic * */ @RequestMapping("/TestReturnModel") public String testReturnModel(Model model){ log.debug("testReturrnModel"); model.addAttribute("testmodel", "hello model"); return "testpage"; } /** * The difference between model and modelandview is just sematic * */ @RequestMapping("/TestReturnModelAndView") public ModelAndView testReturnModelAndView(){ log.debug("testReturrnModel"); ModelAndView mav = new ModelAndView("testpage"); mav.addObject("testmodel", "hello test model"); return mav; } /* @RequestMapping("") public String list(BaseConditionVO vo, Model model) { List<News> newsList = newsMgr.searchNews(vo); int totalCount = newsMgr.searchNewsNum(vo); vo.setTotalCount(totalCount); model.addAttribute("newsTypes", NewsType.values()); model.addAttribute("newsList", newsList); model.addAttribute("vo", vo); return "/management/news/list"; }*/ }
Class TestBean:
package **.persistence.beans; public class TestBean { public String getName() { return name; } public void setName(String name) { this.name = name; } String name; }
Spring控制器响应(action)请求的几种处理方式,如返回JSON,MODEL MODELVIEW,STRING
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。