码迷,mamicode.com
首页 > 编程语言 > 详细

Spring控制器响应(action)请求的几种处理方式,如返回JSON,MODEL MODELVIEW,STRING

时间:2014-09-22 18:03:23      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   os   java   ar   for   2014   sp   

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

标签:blog   http   io   os   java   ar   for   2014   sp   

原文地址:http://blog.csdn.net/xiaohelong2005/article/details/39477727

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!