Java, Spring Framework MVC - redirection(Java,Spring Framework MVC - 重定向)
问题描述
我正在使用 Spring 框架 3.我有一个用于对文章发表评论的表格.提交表单时,会检查是否有任何错误.如果没有错误,控制器返回字符串
I am using spring framework 3. I have a form for posting comments to the article. When the form is submitted, it is checked if there any errors. In case there is no errors, controller returns string
"redirect:entryView/"+comment.getEntryId();
一切都很好.
但是当出现一些错误时,如果控制器返回
But when there are some errors, if controller returns
"redirect:entryView/"+comment.getEntryId();
错误应该显示在带有 spring-form.tld 标签的表单附近:
The errors should be displaed near the form with spring-form.tld tags:
<form:errors path="author"/>
但是没有显示错误!当我试图返回时
But there are no displayed errors! When i am trying to return
"entryView/"+comment.getEntryId();
如果没有 redirect: 前缀,那么它会去/rus/WEB-INF/jsp/entryView/8.jsp 并且有 HTTP 状态 404.但它必须去 http://example.com/rus/entryView/8,即文章和评论表单所在的页面!
Without redirect: prefix, then it is going to /rus/WEB-INF/jsp/entryView/8.jsp and there is HTTP Status 404. But it must go to http://example.com/rus/entryView/8, i.e page where the article and form for comments are!
这是视图解析器:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
我该怎么办?
控制器的其余部分:
@Controller
public class CommentController {
private RusService rusService;
private CommentValidator commentValidator;
@Autowired
public CommentController(RusService rusService,CommentValidator commentValidator){
this.rusService = rusService;
this.commentValidator = commentValidator;
}
@RequestMapping(value="/addComment",method=RequestMethod.POST)
public String addComment(Comment comment,BindingResult result){
commentValidator.validate(comment, result);
if(result.hasErrors()){
return "redirect:entryView/"+comment.getEntryId();
}else{
rusService.postComment(comment);
return "redirect:entryView/"+comment.getEntryId();
}
}
}
推荐答案
The display of errors in <form:errors path="author"/>
works like this:
- 在验证/绑定期间,
Errors
被保存为HttpServletResponse
对象中的属性 - 这个JSP标签的实现调用
response.getAttribute(name)
寻找Errors
实例来显示
- During validation/binding, the
Errors
are saved as an attribute in theHttpServletResponse
object - The implementation of this JSP tag calls
response.getAttribute(name)
to find theErrors
instance to display
当你使用 redirect:url
时,你是在指示 Spring 向客户端的浏览器发送 302 Found
以强制浏览器进行 秒em> 请求,到新的 URL.
When you use redirect:url
, you are instructing Spring to send a 302 Found
to the client's browser to force the browser to make a second request, to the new URL.
由于重定向到的页面使用不同的请求/响应对象集进行操作,因此原始 Errors
将丢失.
Since the redirected-to page is operating with a different set of request/response objects, the original Errors
is lost.
将错误"传递给您要重定向到的页面以便新页面显示它的最简单方法是自己处理它,方法是向 Session
对象添加一条消息第二个页面的控制器可以查看(或通过在 URL 中传递参数).
The simplest way to pass "errors" to a page you want to redirect to in order for the new page to display it would be to handle it yourself, by adding a message to the Session
object which the second page's controller can look at (or by passing an argument in the URL).
这篇关于Java,Spring Framework MVC - 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java,Spring Framework MVC - 重定向
基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01