您好,登錄后才能下訂單哦!
下面是官方文檔對于@ResponseBody注解的解釋:
Mapping the response body with the @ResponseBody annotation The @ResponseBody annotation is similar to @RequestBody. This annotation can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name). For example: @RequestMapping(path = "/something", method = RequestMethod.PUT) @ResponseBody public String helloWorld() { return "Hello World"; } The above example will result in the text Hello World being written to the HTTP response stream. As with @RequestBody, Spring converts the returned object to a response body by using an HttpMessageConverter. For more information on these converters, see the previous section and Message Converters.
@ResopnseBody注解能夠 直接把 控制器返回變量(String)直接 返回給瀏覽器,也可以通過配置 后,把 對象 序列化成Json數據返回給瀏覽器!如果為 null 就會返回空白。
怎么配置呢 ?需要配置MessageConverter:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJackson2HttpMessageConverter" /> </list> </property> </bean> <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>text/json;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean>
下面貼出在官方文檔中的位置:
這個需要jackson jar包支持,需要 jackson-annotations,jackson-core,jackson-databind三個包,:
控制器代碼:
@RequestMapping("House/ClassManager/addByAjax") @ResponseBody public HanBlog_Class ClassManager_addByAjax(HttpServletRequest request){ if(request.getSession().getAttribute("hanblog_uid")==null) return null; HanBlog_Class objClass=new HanBlog_Class(); return objClass; }
jquery代碼:
//|增加 $("#hanblog_add_btn").click(function(){ var classname=$("#add_input_name").val(); var classintroduction=$("#add_input_introduction").val(); alert("分類名稱:"+classname+"分類介紹:"+classintroduction); $.get("<c:url value="/House/ClassManager/addByAjax.do" />",function(result){ alert(result); }); });
運行返回例子:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。