Java,Spring Framework MVC - 重定向 [英] Java, Spring Framework MVC - redirection

查看:36
本文介绍了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 the HttpServletResponse object
  • The implementation of this JSP tag calls response.getAttribute(name) to find the Errors 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 - 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆