I am using spring mvc in my application,when i created user i have to show the success message in a header,
so i used this code
modelAndView.setViewName("redirect:/mainPage");
modelAndView.addObject("successMessage", "user created successfully");
return modelAndView;
But my problem is i am getting this message in browser url. suppose if i use like as below,Success message is showing in a header of mainpage ,but the records in main page disappear.
modelAndView.setViewName("mainPage");
modelAndView.addObject("successMessage", "user created successfully");
return modelAndView;
this is my mainpage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<jsp:include page="../templates/header.jsp"></jsp:include>
<script type="text/javascript" src="js/bootstrap-dropdown.js"></script>
<script type="text/javascript">
// Implement checkbox-like buttons
$(".toggle-buttons > .g-button").click(function() {
$(this).siblings(".g-button").removeClass("checked");
$(this).addClass("checked");
});
</script>
<style>
.errorblock {
color: #060505;
background-color: #ffEEEE;
border: 3px solid #ff0000;
padding: 8px;
margin: 16px;
}
.successblock {
color: #060505;
background-color: #9EF2DF;
border: 3px solid #06B98F;
padding: 8px;
margin: 16px;
}
</style>
<c:if test="${not empty errorMessage}">
<div class="errorblock">${errorMessage}</div>
</c:if>
<c:if test="${not empty successMessage}">
<div class="successblock" >${successMessage}</div>
</c:if>
<!-- full width -->
<div class="widget" >
<div class="header"><span><img src="images/icon/color_18/audio_knob.png" alt="dashboard"/>Mobee Robot</span>
</div>
<div class="content" >
<div id="UITab">
<ul class="tabs" >
<li><a href="#tab1">Mobee User(s)</a></li>
</ul>
<div class="tab_container" >
<sec:authorize access="hasRole('ROLE_USER')">
<div id="tab1" class="tab_content">
<div class="load_page">
<form>
<div class="tableName inTab">
<h3><img src="images/icon/color_18/user.png" alt="users"/>Mobee Users</h3>
<table id="userData" class="display data_table2">
<thead>
<tr>
<th><div class="th_wrapp">Id</div></th>
<th><div class="th_wrapp">User Name</div></th>
<th><div class="th_wrapp">Email Id</div></th>
<th><div class="th_wrapp">Created By</div></th>
<th><div class="th_wrapp">Created On</div></th>
<th><div class="th_wrapp">Status</div></th>
<th><div class="th_wrapp">Manage</div></th>
</tr>
</thead>
<tbody>
<c:forEach items="${mobeeUsers}" var="mobeeRobotUser">
<tr class="odd gradeX">
<td>${mobeeRobotUser.id}</td>
<td><a href="<%=request.getContextPath() %>/useredit?id=${mobeeRobotUser.id}">${mobeeRobotUser.userName}</a> </td>
<td>${mobeeRobotUser.emailId}</td>
<td>${mobeeRobotUser.createdBy}</td>
<td><fmt:formatDate value="${mobeeRobotUser.createdOn}" pattern="dd-MMM-yyyy"/></td>
<td>${mobeeRobotUser.status}</td>
<td>
<span class="tip">
<a id="1" href="<%=request.getContextPath() %>/userdelete?id=${mobeeRobotUser.id}" class="Delete" name="${mobeeRobotUser.userName}" title="Delete">
<img src="images/icon/color_18/close.png"/>
</a>
</span>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</form>
</div>
</div><!-- End tab1 -->
</sec:authorize>
</div><!-- End tab Container -->
</div><!-- End UITab -->
<!-- clear fix -->
<div class="clear"></div>
</div><!-- End Content -->
</div><!-- End full width -->
<jsp:include page="../templates/footer.jsp"></jsp:include>
please help me
thanks in advance
Once you do a redirect the value is not preserved. So you are not getting successMessage in the mainPage controller.
See here a similar question.
You can put the value in session as suggested.
Or in the next controller fetch it from the request and then set it to the modelandview.
Best of luck.