Spring MVC控制器返回HTML [英] Spring MVC controller return HTML

查看:347
本文介绍了Spring MVC控制器返回HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试将HTML返回到我的Spring MVC控制器时遇到问题。



它看起来像这样:

  @RequestMapping(value = QUESTION_GROUP_CREATE_URL,method = RequestMethod.POST)
public
@ResponseBody
String createQuestionGroup(@RequestBody JsonQuestionGroup questionGroup, HttpServletResponse response){

//问题组 - 这就OK了。

response.setContentType(text / html);
response.setCharacterEncoding(UTF-8);
return< div>< / div>;
}

我的Spring配置:

 < bean id =contentNegotiationManagerclass =org.springframework.web.accept.ContentNegotiationManagerFactoryBean> 
< property name =favorPathExtensionvalue =false/>
< property name =favorParametervalue =true/>
< property name =mediaTypes>
< value>
json = application / json
xml = application / xml
html = application / html
< / value>
< / property>
< / bean>

我看到firebug的响应来了: {String: < div>< / div>} 如何告诉这个方法发送纯HTML作为响应?

更改您的Spring配置像这样: html = text / html 并添加 produce = MediaType.TEXT_HTML_VALUE 到您的 @RequestMapping 注释。


I am having an issue trying when trying to return HTML to my Spring MVC controller.

It looks like this:

@RequestMapping(value = QUESTION_GROUP_CREATE_URL, method = RequestMethod.POST)
public
@ResponseBody
String createQuestionGroup(@RequestBody JsonQuestionGroup questionGroup, HttpServletResponse response) {

    // questionGroup - this comes OK.

    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8");
    return "<div></div>";
}

My Spring config:

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false"/>
    <property name="favorParameter" value="true"/>
    <property name="mediaTypes">
        <value>
            json=application/json
            xml=application/xml
            html=application/html
        </value>
    </property>
</bean>

I am seeing firebug that response is coming like: {"String":"<div></div>"} how can I tell this method to send me plain HTML as the response?

解决方案

Change your Spring config like this: html=text/html and add produces = MediaType.TEXT_HTML_VALUE to your's @RequestMapping annotation.

这篇关于Spring MVC控制器返回HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆