【问题标题】:How to call JSP from a java Controller如何从 Java 控制器调用 JSP
【发布时间】:2015-03-13 15:24:53
【问题描述】:

我有一个简单的带有 Spring 的 J2EE 应用程序。 现在我想从 Java 控制器调用另一个页面。比如我在registrazione.jsp,点击一个按钮,调用registrazioneController.java中的一个方法。

现在我想从registrazioneController.java,调用另一个页面,例如

home.jsp,我想在 get 中传递任何参数。

有可能吗?

这是我单击按钮时使用的方法

registrazioenControlle.java

public ModelAndView internalLoadPage(HttpServletRequest request, HttpServletResponse response, Map model) throws Exception 
    {

        //to do
        //call another page for example home.html
        request.getRequestDispatcher("home.jsp").forward(request, response);
        return new ModelAndView("home", model); 
    }

我正在尝试使用此代码,但没有找到。

【问题讨论】:

标签: spring-mvc jakarta-ee


【解决方案1】:

除了 cmets 中提供的答案外,如果您想在重定向时将参数附加到 URL,还可以使用 RedirectAttributes 和 addAttribute 方法。这还将为您提供 addFlashAttribute,它将将属性存储在 Flash 范围内,从而使其可用于重定向页面。您还可以返回一个简单的字符串作为视图名称,例如喜欢

public String internalLoadPage(RedirectAttributes redirectAttributes) throws Exception 
    {
      redirectAttributes.addAttribute("paramterKey", "parameter");
      redirectAttributes.addFlashAttribute("pageKey", "pageAttribute");
      return "redirect:/home";
    }

这假设在你的视图解析器配置中配置了视图后缀

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 2023-03-26
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    相关资源
    最近更新 更多