我有一个国际化的网络项目。它与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 将表格留空并提交。您将看到验证消息。而不是更改语言并再次提交表单以查看指定的错误。
找到我们所有的资源答案 0 :(得分:1)
您正在将lang参数附加到将您带到页面的任何URL。因此,如果您通过/registration.html访问表单,那么En和Ru链接也会导致/registration.html。据推测,该控制器支持GET,因此当您单击链接并生成GET请求时,一切正常。
一旦您发布了表单,En和Ru链接就会导致/user.html,因为这就是您编写页面的方式。单击这些链接,然后生成对/user.html的GET请求。大概这会失败,因为/user.html的控制器支持POST但不支持GET。
仅供参考,您可以清楚地看到客户端和服务器端的情况。在任何一种情况下,都可以使用任意数量的工具来观察来回的HTTP流量。
您可能正在使用Spring,以便在服务器端切换语言,因此您需要再次获取实际页面。要特别修复您的注册表单,最简单的方法是抛弃用户已经输入的数据,并且即使在表单验证失败后也只需将链接切换为指向/registration.html即可。 (换句话说,不应该根据带到页面的任何URL生成链接。)用户无论如何都不会在中间切换语言。但是,如果您必须保留表单数据,则需要采用不同的方法。
答案 1 :(得分:0)
我通过更改
的映射来解决问题@RequestMapping(value = "/branch/{branchId}/topic", method = RequestMethod.POST)
到
@RequestMapping(value = "/branch/{branchId}/topic", method = {RequestMethod.POST, RequestMethod.GET})
并且错误消失了。
答案 2 :(得分:0)
我认为你必须使用POST / REDIRECT / GET模式。
http://en.wikipedia.org/wiki/Post/Redirect/Get
例如:
@Controller
public final class TopicController {
@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);
}
@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 {
/*Here you must create your topic, and add to DB, then redirect..*/
return new ModelAndView("redirect:/TO_THE_VIEW_PAGE");
}
}
@RequestMapping(value = "/TO_THE_VIEW_PAGE", method = RequestMethod.GET)
public ModelAndView view(/*need parameters*/) {
/*...There you must get your topic from DB, and go to view page..*/
return new ModelAndView("/PATH_TO_THE_VIEW_PAGE");
}
如果您需要将数据(对象)传递给重定向后调用的(view
)方法,则可以使用RedirectAttributes
类。
http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html
例如:
@RequestMapping(value = "/branch/{branchId}/topic", method = RequestMethod.POST)
public ModelAndView create(@Valid @ModelAttribute TopicDto topicDto,
BindingResult result,
@PathVariable("branchId") Long branchId,
RedirectAttributes redirectAttributes) throws NotFoundException {
redirectAttributes.addFlashAttribute("topic", topicDto);
redirectAttributes.addFlashAttribute("branchId", branchId);
return new ModelAndView("redirect:/TO_THE_VIEW_PAGE");
}
}
然后在view
方法中获取这些属性。