【问题标题】:Spring - How to pass a message between Views?Spring - 如何在视图之间传递消息?
【发布时间】:2012-09-28 05:08:43
【问题描述】:

我有一个显示、编辑和删除帐户的应用程序。

  1. Display 使用displayAccounts.jsp 并由/accounts url 调用
  2. Edit 使用editAccount.jsp 并由/accounts/{id}/edit 调用,其中id 是要编辑的帐户的主键。
  3. 删除没有自己的视图,只是简单的调用Controller中的@RequestMapping方法,被/accounts/{id}/delete调用

每次操作成功后,都会重定向到显示页面。

  1. 成功执行操作(编辑/删除)后,我想发送一条成功消息,该消息可以显示在“显示”页面上。我怎样才能做到这一点?

  2. 由于我没有删除操作的视图,我也想在显示页面中显示错误。

请帮忙。提前致谢。

【问题讨论】:

  • 分享你的控制器代码快照

标签: java spring controller


【解决方案1】:

使用RedirectAttributes 是您实现这一目标所需要的。我假设从 EDIT 和 DELETE 操作的 POST 方法中,您使用“redirect:”通过其控制器重定向到 DISPLAY 视图。

要实现这一点,您需要将RedirectAttributes attributes 作为控制器函数中的参数。然后在 return 语句之前,您应该添加以下代码行。

attributes.addFlashAttribute("successMsg", "Account Edit/Delete successfully");

attributes.addFlashAttribute("errorMsg", "Edit/Delete account is unsuccessful");

如果出现错误消息。

然后在 displayAccounts.jsp 页面上您只需要显示带有${successMsg}${errorMsg} 的消息

一旦显示消息,如果您刷新页面,则消息将不会出现。它只会向用户显示一次。

这非常适合您的场景。连我都用这个。

希望这对您有所帮助。干杯。

【讨论】:

  • 这正是我想要的,@japs。你似乎很好地理解了这个问题。感谢您的回答。
【解决方案2】:

如果您的 EDIT 操作打开了不同的视图,那么您可以使用以下方法将消息从 Controller 传递到视图

ModelAndView  mav = new ModelAndView();

然后将消息对象设置为

mav.addObject("message", "EDIT sucessfully completed");

最后,设置视图名称

mav.setViewName("views/afterEditOperation/");

您可以在视图上直接使用消息作为 ${mesasage}

【讨论】:

  • 让我尝试一下并告诉你,@arun-kumar
  • @th3an0maly 当然可以。请试一试。
  • 好吧,我有一个更简单、更直接的解决方案来解决我的问题。无论如何,感谢您的帮助:)
【解决方案3】:

在Controller方法的edit和delete方法中,将消息字符串写入Model as

编辑

model.addAttribute("message", "Record Edited Sucessfully");
model.addAttribute("accountList", accountList);

删除

model.addAttribute("accountList", accountList);
model.addAttribute("message", "Record Deleted Sucessfully");

并在您的 displayAccounts.jsp 中编写代码以将此消息显示为

<div>
  <span>${message}</span>
</div>

【讨论】:

  • 这种方法的问题是,视图displayAccounts 将只包含result 属性。除非我执行redirect,否则它不会显示帐户。此外,按照您建议的方式,链接仍然是/accounts/{id}/delete,此后,即使是刷新也不会在我的页面中填充要显示的帐户。
  • 您可以在模型中添加多个属性,请查看以上更新答案。请分享您的控制器代码快照
【解决方案4】:

在这种情况下,您可以使用 AJAX。有了这个,您可以将您的操作从同一页面发送到 Controller 进行删除操作,并从同一页面上的控制器获取消息以显示控制器删除操作的成功/错误消息。

【讨论】:

  • 还有其他不涉及Ajax的方法吗?那么我的编辑操作呢,它发生在一个单独的视图中?
【解决方案5】:

如果您想从控制器传递一些消息(错误/成功)以显示页面,而不是下面的代码帮助,

@Controller
@RequestMapping("/controllerPath")
public class editDeleteController {

    @RequestMapping(method = RequestMethod.GET)
public String methodName(ModelMap model) { 
        model.addAttribute("message", "Message1");
                model.addAttribute("moremessage", "Message2");
        return "viewName";//veiwName here in your case it is displayPage 
    }
}

在显示页面你会得到它,

<h1>Message : ${message}</h1>

如果这对您不起作用,请详细说明。

谢谢


好吧,我想我明白你的意思了,你可以这样想,

@RequestMapping(method = RequestMethod.GET)
    public String editDelete(ModelMap model) {

        if(success)
        {
            model.addAttribute("result", "Success");
            return new ModelAndView("redirect:/displaytagView");//Chaining your controller so it will refresh your Accounts
        }else{
            model.addAttribute("result", "Error");
                        return "displayPage";
        }
    }

将控制器链接到 displayPage 控制器,以便显示刷新的数据。试试这个希望它的工作

【讨论】:

  • 这种方法的问题是,视图displayAccounts 将只包含result 属性。除非我执行redirect,否则它不会显示帐户。此外,按照您建议的方式,链接仍然是/accounts/{id}/delete,此后,即使是刷新也不会在我的页面中填充要显示的帐户。
  • 我想我遇到了你的问题,你可以使用链式控制器解决这个问题。我已经编辑了我的答案。谢谢
  • 感谢您的回答,@nishant-solanki。我的问题得到了更直接、更直接的答案。
【解决方案6】:

Spring提供了ModelMap类型的对象,你可以把你的数据放到ModelMap类型的对象中。这个对象在整个应用程序中都是可以访问的。在这里,您可以放置​​一个 String 类型的变量,其值为 Success 或 Failure(基于条件)。稍后您可以从 jsp 页面访问此模型对象。这里是示例。

   @RequestMapping(value="/accounts/{id}/edit ")
 //  public String yourMethod(ModelMap model)
public String yourMethod(HttpServletRequest request,
      HttpServletResponse response,ModelMap model){
   if(someCondition){
     String result = "Success";
    model.addAttribute("result", result);
    }
      else{
          String result="failure";
          model.addAttribute("result", result);
        }
        // return "displayAccounts";
       return new YourControllerClass().yourMethodforAccountDisplay(request, response,model);//this is where you specify account display method with appropriate argument
   }

您可以从 jsp 页面访问结果,如下所示 -

         ${result}

请在 Spring 配置文件中启用 viewresolver。

【讨论】:

  • 这种方法的问题是,视图displayAccounts 将只包含result 属性。除非我执行redirect,否则它不会显示帐户。此外,按照您建议的方式,链接仍然是/accounts/{id}/delete,此后,即使是刷新也不会在我的页面中填充要显示的帐户。
  • 我已经更新了代码。现在您可以在这里调用帐户显示方法表单。另一种替代方法是使用转发而不是重定向。
  • 好吧,我有一个更简单、更直接的解决方案来解决我的问题。无论如何,感谢您的帮助:)
  • 不客气。我也从你的问题中学到了新东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多