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;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。