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

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

问题描述

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

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

看起来像这样:

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

我的 Spring 配置:

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>

我看到萤火虫的响应是这样的:{"String":"<div></div>"} 我怎么能告诉这个方法向我发送纯 HTML 作为回应?

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?

推荐答案

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

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天全站免登陆