我正在尝试使用 ModelAndView 将数据从 Controller 发送到 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>
这是图像文件。如您所见,ModelAndView 以查询字符串的形式返回参数,但它没有出现在 JSP 中。
我更改了 Java 代码和 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.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
@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");
//redirectView.setExposeModelAttributes(false);
mv = new ModelAndView(redirectView);
mv.addObject("title", "エラー");
mv.addObject("message1", "ご連絡ください。");
return mv;
}
}
<!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><%= request.getParameter("title") %></strong></h3>
</div>
<div class="box-body">
<h4><%= request.getParameter("message1") %></h4>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
</body>
</html>
它有效,但我想使用${}
而不是<%= %>
. 此外,我不想在 URL 上显示查询字符串,所以我添加了行redirectView.setExposeModelAttributes(false);
,但是这个 api 似乎从 URL 中删除了参数,并且没有任何内容可以再次显示。