I'm trying to send the data to JSP from Controller using ModelAndView. But it doesn't show.. What am I missing? Do I have to do something more?
This is my Controller.
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;
}
}
And this is 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>
And this is the image file.
As you can see ModelAndView return parameters as querystring but it doesn't show up in JSP.
I changed Java Code and 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>
It worked but I want to use ${}
instead of <%= %>
.
Also I didn't want to be shown query string on URL so I added line redirectView.setExposeModelAttributes(false);
but this api seems to remove parameters from URL and nothing to be shown again.