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

查看:121
本文介绍了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();

没有重定向:前缀,那么它将转到/rus/WEB-INF/jsp/entryView/8.jsp,并且存在HTTP状态404.但是必须转到

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();
    }
}
}

推荐答案

<form:errors path="author"/>中的错误显示如下:

  • 在验证/绑定期间,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发送到客户端的浏览器,以强制浏览器向新URL发出 second 请求.

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天全站免登陆