Java ModelAndView没有';t将数据返回到JSP

Java ModelAndView没有';t将数据返回到JSP,java,spring,jsp,redirect,modelandview,Java,Spring,Jsp,Redirect,Modelandview,我试图使用ModelAndView将数据从控制器发送到JSP。 但它没有显示。。 我错过了什么?我还需要做些什么吗 这是我的控制器 import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.spri

我试图使用ModelAndView将数据从控制器发送到JSP。 但它没有显示。。 我错过了什么?我还需要做些什么吗

这是我的控制器

import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

@RestController
public class RobotpaymentController {

    @RequestMapping(value="/seikyu", method={RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
    public ModelAndView seikyu(HttpServletRequest request) {
        RedirectView redirectView;
        ModelAndView mv;

        String seikyuId = request.getParameter("seikyuId");

        redirectView = new RedirectView("/pages/error.jsp");
        mv = new ModelAndView(redirectView);
        mv.addObject("title", "エラー");
        mv.addObject("message1", "ご連絡ください。");

        return mv;  
    }
}

这是JSP

<!DOCTYPE html>
<html>
<head> 
  <title></title>
  <meta http-equiv="Content-Type" content="text/html" charset="UTF-8"/>

  <script src="/assets/js/common/jquery-3.4.1.min.js"></script>

</head>
<body style="height: auto; min-height: 100%; background:#ecf0f5;">
    <div class="wrapper" style="height: auto; min-height: 100%;">
        <header class="main-header"></header>
        <div class="content-wrapper" style="min-height: 1046px;">
            <section class="content-header"></section>
            <section class="content">
                <div class="row">
                <div class="col-md-6 col-sm-offset-2">
                    <div class="box box-default">
                        <div class="box-header with-border">
                            <i class="fa fa-warning text-red"></i>
                            <h3 class="box-title"><strong>${title}</strong></h3>
                        </div>
                        <div class="box-body">
                            <h4>${message1}</h4>
                        </div>
                    </div>
                </div>
                </div>
            </section>
        </div>
    </div>
</body>
</html>


它起作用了,但我想使用
${}
而不是

另外,我不想在URL上显示查询字符串,所以我添加了行
redirectView.setExposeModelAttributes(false)
但此api似乎从URL中删除了参数,并且不再显示任何内容。

尝试从端点中删除
products=MediaType.APPLICATION\u JSON\u值
,并从普通控制器而不是
RestController
返回ModelAndView。rest控制器方法仅在响应中返回
json
,而不解析视图。因此,即使您在
ModelAndView
对象中指定了一个视图,它也不会得到解析,因此模型也不会绑定到您提供的jsp

@Controller
public class RobotpaymentController {

    @RequestMapping(value="/seikyu", method={RequestMethod.GET})
    public ModelAndView seikyu(HttpServletRequest request) {
        RedirectView redirectView;
        ModelAndView mv;

        String seikyuId = request.getParameter("seikyuId");

        redirectView = new RedirectView("/pages/error.jsp");
        mv = new ModelAndView(redirectView);
        mv.addObject("title", "エラー");
        mv.addObject("message1", "ご連絡ください。");

        return mv;  
    }
}

要访问jsp中查询字符串中的参数,需要使用
param
object。
您需要像这样更改jsp

<h3 class="box-title"><strong>${param.title}</strong></h3>
必须从该方法返回的字符串取决于
viewsolver
配置和JSP路径。


2)使用
重定向属性
并通过控制器将请求重定向到JSP。

当您使用
重定向属性#addFlashAttribute
放置数据时,您可以在下一个HTTP请求中访问数据。Spring在下一个请求中可以访问由
RedirectAttributes#addFlashAttribute
放置的数据,因此如果将请求直接重定向到JSP,则无法访问数据。因此,您必须将其重定向到另一个控制器的方法,并使该方法将其转发到JSP

控制器的方法如下所示

    @RequestMapping(value="/seikyu", method={RequestMethod.GET})
    public String seikyu(HttpServletRequest request. Model model) {

        String seikyuId = request.getParameter("seikyuId");
        ....
        model.addAttribute("title", "エラー");
        model.addAttribute("message1", "ご連絡ください。");

       // If you  configure ‘ViewResolver’ like `registry.jsp("/WEB-INF/",".jsp”)`
       // you can ‘forward’ the request to the /WEB-INF/redirect.jsp by returning “redirect”.
        return “redirect”;  
    }

    @RequestMapping(value="/seikyu", method={RequestMethod.GET})
    public String seikyu(HttpServletRequest request. RedirectAttributes redirectAttributes) {

        String seikyuId = request.getParameter("seikyuId");
        ....
        redirectAttributes.addFlashAttribute("title", "エラー");
        redirectAttributes.addFlashAttribute("message1", "ご連絡ください。");

        // If your servlet-mapping is ‘/app/*’, the string to be returned is something like this.
        return “redirect:/app/fromRedirect”;  
    }

   @RequestMapping("/fromRedirect")
   public String fromRedirect(....)  {
      ....
      return "redirect";
   }

seikyu
方法返回的字符串取决于
servlet映射的配置。
fromRedirect
方法将请求“转发”到JSP(如1)示例。

另请参见


您可以通过删除
products=MediaType.APPLICATION\u JSON\u VALUE
进行检查吗?仍然不工作…我删除了
products
并将
RestController
更改为
Controller
,但它仍然不工作。您是否正确配置了error.jsp以显示您作为模型提供的数据@HeesunKim你能分享更多的细节吗,比如error.jsp,如果你已经定制了error.jsp,那么也可以在问题中查看相关代码吗?谢谢你的回答。我试图共享原始文件,但太长太复杂了。我刚刚用newtry编辑了我的问题,但仍然不能使用
${}
。当我使用ModelAndView时,我必须更改配置吗?@HeesunKim那么现在您的
error.jsp
已正确填充,但您需要它来处理${},而不是
?对我还希望在调用JSP时隐藏查询sting。我使用了
setExposeModelAttributes
api,但是这个api似乎删除了参数,并且数据没有显示在JSP中。感谢您的回答。我尝试了
${param…}
,但没有成功。我检查了你推荐的链接,我使用了
,它成功了!但是我仍然想知道如何使用
${…}
.1)您的
web.xml
配置是否正确?请确保
符合Servlet 2.4或更新版本,并删除
。请看:()。2) 如果不想将数据公开到URL,最好使用“转发”而不是“重定向”。我想在稍后的回答中添加此解决方案。
<h4>${param.message1}</h4>
    @RequestMapping(value="/seikyu", method={RequestMethod.GET})
    public String seikyu(HttpServletRequest request. Model model) {

        String seikyuId = request.getParameter("seikyuId");
        ....
        model.addAttribute("title", "エラー");
        model.addAttribute("message1", "ご連絡ください。");

       // If you  configure ‘ViewResolver’ like `registry.jsp("/WEB-INF/",".jsp”)`
       // you can ‘forward’ the request to the /WEB-INF/redirect.jsp by returning “redirect”.
        return “redirect”;  
    }

    @RequestMapping(value="/seikyu", method={RequestMethod.GET})
    public String seikyu(HttpServletRequest request. RedirectAttributes redirectAttributes) {

        String seikyuId = request.getParameter("seikyuId");
        ....
        redirectAttributes.addFlashAttribute("title", "エラー");
        redirectAttributes.addFlashAttribute("message1", "ご連絡ください。");

        // If your servlet-mapping is ‘/app/*’, the string to be returned is something like this.
        return “redirect:/app/fromRedirect”;  
    }

   @RequestMapping("/fromRedirect")
   public String fromRedirect(....)  {
      ....
      return "redirect";
   }