日期:2014-05-16 浏览次数:20409 次
package com.microblog.web; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * Controller类 * @author 晔 * */ @Controller public class TestController { @RequestMapping(value="/test.json",method=RequestMethod.GET) public String getUsers(@PathVariable("namePrefix") String prefix, Model model) { model.addAttribute("json", "[{'id':'1','name':'messi'},{'id':'2','name':'pedro'}]"); System.out.println("hello 你好周杰伦"); return "jsonView"; } }
<context:component-scan base-package="**/web/**" />
package com.microblog.web; import java.io.PrintWriter; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.view.AbstractView; /** * Viewer类 * @author 晔 * */ public class TestView extends AbstractView{ protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { PrintWriter out=response.getWriter(); out.println((String)model.get("json"));//json字符串从这里走向世界 } }
<bean id="test" class="com.microblog.web.TestController"/> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="1" /> </bean> <bean id="jsonView" class="com.microblog.web.TestView" />