我有一个国际化的网络项目。这与http://www.springbyexample.org/examples/basic-webapp-internationalization-jsp-example.html非常相似。 在装饰器中有切换区域设置的链接。他们将lang param添加到当前网址中:春季国际化:语言环境变化问题
<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/
感谢您的解释。但我能做些什么来避免这个错误?我可以将用户重定向到交换语言的主页面。但它不是用户友好的。可能我可以坚持得到控制器之前的某个地方的参数吗? – NullPointer
我也可以设置spirng特定的cookie形式而不是传递GET-param。它很好用,但我认为这不是最好的解决方案。 – NullPointer