相关疑难解决方法(0)

谁在S​​pring MVC中设置响应内容类型(@ResponseBody)

我在我的Annotation驱动的Spring MVC Java Web应用程序中运行在jetty Web服务器上(目前在maven jetty插件中).

我试图用一个控制器方法做一些AJAX支持,只返回String帮助文本.资源采用UTF-8编码,字符串也是如此,但我的服务器响应是随附的

content-encoding: text/plain;charset=ISO-8859-1 
Run Code Online (Sandbox Code Playgroud)

即使我的浏览器发送

Accept-Charset  windows-1250,utf-8;q=0.7,*;q=0.7
Run Code Online (Sandbox Code Playgroud)

我正在以某种方式使用弹簧的默认配置

我发现了一个提示将这个bean添加到配置中,但我认为它只是没有使用,因为它说它不支持编码,而是使用默认编码.

<bean class="org.springframework.http.converter.StringHttpMessageConverter">
    <property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
</bean>
Run Code Online (Sandbox Code Playgroud)

我的控制器代码是(请注意,此响应类型的更改对我不起作用):

@RequestMapping(value = "ajax/gethelp")
public @ResponseBody String handleGetHelp(Locale loc, String code, HttpServletResponse response) {
    log.debug("Getting help for code: " + code);
    response.setContentType("text/plain;charset=UTF-8");
    String help = messageSource.getMessage(code, null, loc);
    log.debug("Help is: " + help);
    return help;
}
Run Code Online (Sandbox Code Playgroud)

java web-applications spring-mvc character-encoding

122
推荐指数
7
解决办法
24万
查看次数

Spring MVC 3返回内容类型:text/plain

我想在页面上显示简单的文本,因此我想返回Content-Typeas text/plain.

使用下面的代码,我在页​​面上看到纯文本,但返回Content-Type仍然是text/html.

我怎样才能解决这个问题?

注意:我正在使用带有Spring MVC的Tiles.返回的"m.health"指向一个tile def,它映射到一个health.jsp,它只包含下面的1行.

更新说明:我无法控制HTTP标头请求中的Content-TypeAccept值.text/plain无论发出什么样的请求,我都希望我的回复能够回复.

控制器:

@RequestMapping(value = "/m/health", method = RequestMethod.GET, headers = "Accept=*")
public String runHealthCheck(HttpServletResponse response, HttpServletRequest request, Model model) throws Exception {
    model = executeCheck(request, response, TEMPLATE, false, model);
    model.addAttribute("accept", "text/plain");
    response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");
    return "m.health";
}
Run Code Online (Sandbox Code Playgroud)

JSP:

$ {}状态

java content-type spring-mvc apache-tiles

21
推荐指数
2
解决办法
6万
查看次数