java - Spring国际化: locale change problem

标签 java spring-mvc internationalization

我有一个国际化的网络项目。它与 http://www.springbyexample.org/examples/basic-webapp-internationalization-jsp-example.html 非常相似。 装饰器中有用于切换区域设置的链接。他们将 lang 参数添加到当前 url:

<a href="?lang=en">En</a> | <a href="?lang=ru">Ru</a> </span></td>

第一次国际化效果很好。但后来我们发现某些表格存在问题。 形式:

<form:form action="${pageContext.request.contextPath}/branch/${branchId}/topic.html" modelAttribute="topicDto" method="POST" 
onsubmit="this.getAttribute('submitted')"> <!--Block multiple form submissions-->
<table border="2" width="100%">
    <tr>
        <td width="30%">
            <form:label path="topicName"><spring:message code="label.topic"/></form:label>
            <form:input path="topicName"/>
            <form:errors path="topicName"/>
        </td>
    </tr>
    <tr>
        <td height="200">
            <form:label path="bodyText"><spring:message code="label.text"/></form:label>
            <form:textarea path="bodyText"/>
            <form:errors path="bodyText"/>
        </td>
    </tr>
</table>
<input type="submit" value="<spring:message code="label.addtopic"/>"/>

Controller :

/**
 * @see Topic
 */
@Controller
public final class TopicController {
/**
 * Method handles newTopic.html GET request and display page for creation new topic
 *
 * @param branchId {@link org.jtalks.jcommune.model.entity.Branch} id
 * @return {@code ModelAndView} object with "newTopic" view, new {@link TopicDto} and branch id
 */
@RequestMapping(value = "/branch/{branchId}/topic/create", method = RequestMethod.GET)
public ModelAndView createPage(@PathVariable("branchId") Long branchId) {
    return new ModelAndView("newTopic")
            .addObject("topicDto", new TopicDto())
            .addObject("branchId", branchId);
}

/**
 * This method handles POST requests, it will be always activated when the user pressing "Submit topic"
 *
 * @param topicDto the object that provides communication between spring form and controller
 * @param result   {@link BindingResult} object for spring validation
 * @param branchId hold the current branchId
 * @return {@code ModelAndView} object which will be redirect to forum.html
 * @throws org.jtalks.jcommune.service.exceptions.NotFoundException
 *          when branch not found
 */
@RequestMapping(value = "/branch/{branchId}/topic", method = RequestMethod.POST)
public ModelAndView create(@Valid @ModelAttribute TopicDto topicDto,
                           BindingResult result,
                           @PathVariable("branchId") Long branchId) throws NotFoundException {
    if (result.hasErrors()) {
        return new ModelAndView("newTopic").addObject("branchId", branchId);
    } else {
        Topic createdTopic = topicService.createTopic(topicDto.getTopicName(), topicDto.getBodyText(),
                branchId);
        return new ModelAndView("redirect:/branch/" + branchId + "/topic/"
                + createdTopic.getId() + ".html");
    }
}

}

如果用户发布包含无效字段的表单,它将在字段之前看到验证消息。如果他当时切换页面语言,他将看到错误:

HTTP Status 405 - Request method 'GET' not supported
type Status report
message Request method 'GET' not supported
description The specified HTTP method is not allowed for the requested resource (Request method     'GET' not supported).
Apache Tomcat/7.0.11

您可以在我们的开发服务器上自行检查问题 http://deploy.jtalks.org/jcommune/index.html 例如在注册页面 http://deploy.jtalks.org/jcommune/registration.html 将表格留空并提交。您将看到验证消息。比更改语言并再次提交表单更能看到指定的错误。

您可以在这里找到我们所有的资源 http://fisheye.jtalks.org/

最佳答案

您将 lang 参数附加到将您带到该页面的任何 URL 中。因此,如果您通过/registration.html 访问表单,那么 En 和 Ru 链接也会指向/registration.html。据推测该 Controller 支持 GET,因此当您单击链接并生成 GET 请求时,一切正常。

一旦您发布了表单,En 和 Ru 链接就会指向/user.html,因为这就是您对页面进行编程的方式。单击这些链接,您将生成一个针对/user.html 的 GET 请求。大概这会失败,因为/user.html 的 Controller 支持 POST 但不支持 GET。

仅供引用,您可以准确地看到客户端和服务器端发生的情况。无论哪种情况,都可以使用任意数量的工具来观察来回的 HTTP 流量。

很可能您使用 Spring 的方式是在服务器端切换语言,因此您需要再次获取实际页面。特别要修复您的注册表单,最简单的方法就是丢弃用户已输入的数据,即使在表单验证失败后也只需将链接切换为指向/registration.html 即可。 (换句话说,不应根据将您带到该页面的任何 URL 来生成链接。)无论如何,用户很可能不会在中间切换语言。但是,如果您必须保留表单数据,则需要采用不同的方法。

关于java - Spring国际化: locale change problem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6433807/

相关文章:

haskell - 是否可以在 Yesod 的消息文件中添加注释或 HTML 标签?

java - 单元测试 "glue"代码或没有任何真正逻辑的代码

java.reflection ProceedingJoinPoint

java - Spring MVC View - 解析占位符

internationalization - 是否所有应用程序都默认本地化为塞尔维亚语?

struts2 - 如何在同一个 JSP 上多次显示资源包 key 但在不同的语言环境中?

java - Spring MVC 中的 Struts ActionMessages 相当于什么?

java - 用于处理 docx 文档的 Apache POI 或 docx4j

java - spring - 从类的静态字段中的属性文件中读取属性值

java - 在 Spring MVC/Hibernate 中简单地更新多个值