【发布时间】:2011-06-22 01:07:27
【问题描述】:
我有一个国际化的网络项目。它与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"/>"/>
控制器:
/**
* @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/
【问题讨论】:
标签: java spring-mvc internationalization