<bdo id='1Uuq0'></bdo><ul id='1Uuq0'></ul>

      <legend id='1Uuq0'><style id='1Uuq0'><dir id='1Uuq0'><q id='1Uuq0'></q></dir></style></legend>

      <small id='1Uuq0'></small><noframes id='1Uuq0'>

    1. <i id='1Uuq0'><tr id='1Uuq0'><dt id='1Uuq0'><q id='1Uuq0'><span id='1Uuq0'><b id='1Uuq0'><form id='1Uuq0'><ins id='1Uuq0'></ins><ul id='1Uuq0'></ul><sub id='1Uuq0'></sub></form><legend id='1Uuq0'></legend><bdo id='1Uuq0'><pre id='1Uuq0'><center id='1Uuq0'></center></pre></bdo></b><th id='1Uuq0'></th></span></q></dt></tr></i><div id='1Uuq0'><tfoot id='1Uuq0'></tfoot><dl id='1Uuq0'><fieldset id='1Uuq0'></fieldset></dl></div>
      <tfoot id='1Uuq0'></tfoot>

        Java,Spring Framework MVC - 重定向

        时间:2023-09-24

        <small id='omPiS'></small><noframes id='omPiS'>

          <tbody id='omPiS'></tbody>
      1. <i id='omPiS'><tr id='omPiS'><dt id='omPiS'><q id='omPiS'><span id='omPiS'><b id='omPiS'><form id='omPiS'><ins id='omPiS'></ins><ul id='omPiS'></ul><sub id='omPiS'></sub></form><legend id='omPiS'></legend><bdo id='omPiS'><pre id='omPiS'><center id='omPiS'></center></pre></bdo></b><th id='omPiS'></th></span></q></dt></tr></i><div id='omPiS'><tfoot id='omPiS'></tfoot><dl id='omPiS'><fieldset id='omPiS'></fieldset></dl></div>
          <tfoot id='omPiS'></tfoot>
            • <bdo id='omPiS'></bdo><ul id='omPiS'></ul>
            • <legend id='omPiS'><style id='omPiS'><dir id='omPiS'><q id='omPiS'></q></dir></style></legend>
                  本文介绍了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 - 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <legend id='GfhG5'><style id='GfhG5'><dir id='GfhG5'><q id='GfhG5'></q></dir></style></legend>
                  1. <small id='GfhG5'></small><noframes id='GfhG5'>

                    <tfoot id='GfhG5'></tfoot>

                        <tbody id='GfhG5'></tbody>

                          <bdo id='GfhG5'></bdo><ul id='GfhG5'></ul>
                          <i id='GfhG5'><tr id='GfhG5'><dt id='GfhG5'><q id='GfhG5'><span id='GfhG5'><b id='GfhG5'><form id='GfhG5'><ins id='GfhG5'></ins><ul id='GfhG5'></ul><sub id='GfhG5'></sub></form><legend id='GfhG5'></legend><bdo id='GfhG5'><pre id='GfhG5'><center id='GfhG5'></center></pre></bdo></b><th id='GfhG5'></th></span></q></dt></tr></i><div id='GfhG5'><tfoot id='GfhG5'></tfoot><dl id='GfhG5'><fieldset id='GfhG5'></fieldset></dl></div>