1

在 Spring MVC 3.1 应用程序中,我应该如何使用请求的语言环境来控制 HttpMessageConverter 的字符串格式?

我有这个代表土壤类型的枚举(摘录):

public enum Soil {

    SAND("0.35"),
    PEAT("0.70");

    private Double porosity;

    Soil(String porosity) {
        this.porosity = new Double(porosity);
    }

    @NumberFormat(pattern = "0.000")
    public Double getPorosity() {
        return porosity;
    }

}

我还有一个用于访问土壤特性的控制器。这是在选择不同的土壤类型时触发的 ajax 请求。

@Controller
@RequestMapping("/soil/*")
public class SoilController {

    @RequestMapping(value="{id}/porosity")
    public @ResponseBody Double getPorosity(@PathVariable String id) {
        Soil soil = Soil.valueOf(id);
        return soil.getPorosity();
    }

}

据我了解,通过使用@RequestBody,Spring 使用 HttpMessageConverter 将返回的 Double 转换为响应正文。这工作正常,但响应字符串始终使用默认语言环境进行格式化。例如,对 /soil/PEAT/porosity 的 GET 请求将导致将“0.7”写入响应正文。

然而,大多数访问者将“nl”作为他们的首选语言环境,期望使用小数点逗号而不是小数点。在这种情况下,我希望将响应字符串格式化为“0,7”。

我可以将 HttpServletRequest 参数添加到处理程序方法的签名中以确定请求的语言环境,但不知道如何将此信息传递给 HttpMessageConverter - 在这种情况下为 MappingJacksonHttpMessageConverter (*)。我知道可以通过覆盖标记中的默认值来配置 MappingJacksonHttpMessageConverter bean,但是如何让它知道请求区域设置?

(*) 调试:org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor - 使用 [org.springframework.http.converter.json 将 [0.7] 写为“application/json;charset=UTF-8”。映射JacksonHttpMessageConverter@145240a]

工作解决方案

正如@CodeChimp 所说(https://stackoverflow.com/a/15074077/1343409),i18n格式最好由视图处理。

我将处理程序方法更改为

@RequestMapping(value="{id}/porosity")
public String getPorosity(@PathVariable String id, HttpServletRequest request, Model model) {
    Soil soil = Soil.valueOf(id);
    model.addAttribute("doubleValue", soil.getPorosity());
    return "basevalues/double";         
}

并添加了一个简单的 JSTL 视图 double.jsp

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/plain;charset=UTF-8" %>
<fmt:formatNumber type="number" value="${doubleValue}"/>

这对我有用。

4

1 回答 1

0

ResponseBody 注释指示 Spring 获取您返回的任何内容,并将其放入响应正文中。它是一对一的,没有转换。例如,我使用该注释来返回二进制图像数据、CSV 文件等。如果您想返回针对特定语言环境格式化的数字,那么您需要在 getPorosity() 方法中进行检查并制作那里适当的修改。

编辑以添加信息

My understanding of the HttpMessageConverter is that it is used to convert to/from HTTP request/responses. HttpMessageConverter is, itself, just an interface with many implementations. The proper implementation is chosen based on the request/response headers and the type of return you are using. For instance, if the request specifies it accepts application/xml or text/xml, then the MarshallingHttpMessagingConverter is used to convert the return to the proper XML format.

However, the @ResponseBody annotation allows one to circumvent the conversion. This annotation is basically indicating to Spring "don't worry about converting the return type, just take what I am returning and shove it into the response body."

I do not believe that any of the classes that implement HttpMessageConverter are concerned with locale at all. Their only concern is to take the return type and convert it to the proper form for transport. Handling i18n-related items, which would be those reliant on the locale, are view-related concerns, and in my experience best handled in the view.

于 2013-02-25T18:43:59.413 回答