Monica Jain
Monica Jain

Reputation: 1

Redirect from one controller to another in Spring MVC

I am writing an application in Spring MVC. I am using the search operation and displaying the result in jsp in tabular format with a radio button to each row and I again have a update button in the searchResult.jsp to update the selected row in the table. But after the update operation is done through my controller I want to redirect it to the same searchResult.jsp but with updated records.

Please help me....

And my first controller which gives me the search result uses @RequestParams as arguments. I am mentioning this because I googled a lot and found the suggestions of calling the same search controller again but it is not working in my case.

my jsp for search

<!-- sac search -->
<div id="sacSearch">
    <div id="searchContact">
                    <form:form class="form-horizontal paddingTop10" role="form" action="autoRestrictSACSearchResult.htm" id="autoRestSACForm" method="POST">
            <div class="form-group">
                <label id="sacLabel" for="sac" class="col-sm-3 control-label"><spring:message code="manage.barrying.sac" /></label>
                <div class="col-sm-3">
                    <input class="form-control" type="text" id="sac" name="sac">
                </div>
                <label id="sacNameLabel" for="sacName" class="col-sm-3 control-label"><spring:message code="manage.barrying.sac.name" /></label>
                <div class="col-sm-3">
                    <input class="form-control" type="text" id="sacName" name="sacName" data-toggle="tooltip" data-placement="right" title=""
                        data-original-title="Wildcard search - use * symbol">
                </div>
            </div>
            <hr>
            <div class="form-group paddingSide30 marginBottom0">
                <button type="submit" class="btn btn-default btnStyle floatRight">
                    <spring:message code="manage.barrying.search" />
                </button>
            </div>
        </form:form>
    </div>
</div>

my first controller for search

  @RequestMapping(value = /autoRestrictSACSearchResult, method = RequestMethod.POST)
@CDBusinessProcess("ThirdPartySACSearch")
public ModelAndView ThirdPartysearchUsingSAC(@RequestParam(value="sac") String sac, @RequestParam(value="sacName") String sacName){
    ThreadLocalResources.get().logger().logMessage(E2EMessageGroup.INFO, "ThirdPartySACSearch" + ": searchUsingAccount, sac = "+sac + ", sacName = "+sacName);
    ModelAndView modelAndView = new ModelAndView();List<RestrictionBarryingDTO> restricBarryindSACList = null;
    restricBarryindSACList = restrictBarryingService.thirdPartygetSACSearchList(sac, sacName);
     modelAndView.setViewName("thirdPartySACSearchResult");
     modelAndView.addObject("restricBarryindSACList",restricBarryindSACList);
modelAndView.addObject("sac",sac);
modelAndView.addObject("sacName",sacName);
     return modelAndView;
}

and again the thirdPartySACSearchResult.jsp has a form like :

       <form:form id="sacEditForm" role="form"
    action="thirdPartyapplyRemoveBarryingforSac.htm" method="POST">
       <c:choose>
    <c:when test="${not empty sac and not empty sacName}">
    <p>
    <strong>Search results for Sales account Code:</strong> ${sac}<strong>
    and Sales Account Name:</strong> ${sacName}
    </p>
    </c:when>
    <c:when test="${not empty sac}">
    <p>
    <strong>Search results for Sales Account Code:</strong> ${sac}
    </p>
         </c:when>
    <c:when test="${not empty sacName}">
    <p>
    <strong>Search results for Sales Account Name:</strong> ${sacName}
    </p>
    </c:when>
    </c:choose>
    <spring:message code="manage.barrying.apply.bar.to"/>
    <div class="verticalDivider topScope historyFullWidth">
    <table class="tablesorter standardText stripedTableJS">
    <thead>
    <tr class="greyBackground">
    <th scope="col" class="width10 hideBackgroundImage">&nbsp;</th>
    <th scope="col"><spring:message code="manage.barrying.sac" /></br>
    <sapn> & name</span></th>
    <th scope="col"><spring:message code="manage.barrying.le.num" /></br> <span> & name</span></th>
    <th scope="col"><spring:message code="manage.barrying.auto.rest.bar.flag" /></th>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${restricBarryindSACList}" var="searchResult"
        varStatus="loop">
        <c:choose>
            <c:when test="${loop.index eq 0}">
            <tr class="cellSelected">
            <td><input type="radio" id="option${loop.index}"
                    value="${loop.index}" name="index" checked="checked" /></td>
        </c:when>
        <c:otherwise>
        <tr>
        <td><input type="radio" id="option${loop.index}"
                    value="${loop.index}" name="index" /></td>
        </c:otherwise>
        </c:choose>
        <td><span class="SAC">${searchResult.salesAccountCode}</span></br>${searchResult.salesAccountName}
        </td>
        <td><span class="leNum">${searchResult.legalEntityNumber}</span></br>${searchResult.legalEntityName}
        </td>
        <c:choose>
        <c:when test="${searchResult.thirdPartyBaringFlag eq 'N'}">
        <td><span class="bar">Off</span></td>
        </c:when>
        <c:otherwise>
        <td><span class="bar">On</span></td>
        </c:otherwise>
        </c:choose>
        </tr>
        </c:forEach>
        </tbody>
    </table>
    <input type="hidden" id="bar" name="bar" value="" /> 
    <input type="hidden" id="code" name="code" value="" />
    <input type="hidden" id="leNum" name="leNum" value="" />    
    </div>
    <hr>
    <div id="apply">
        <input type="submit" class="btn btn-default btnStyle floatRight" id="sacFields4ClickB" value="Apply Bar"/> 
    </div>
    <div id="remove">               
         <input type="submit" class="btn btn-default btnStyle floatRight" id="sacFields5ClickB" value="Remove Bar"/> 
    </div>
    </form:form>
</div>

and the controller for the above form id like:

       @RequestMapping(value="/thirdPartyapplyRemoveBarryingforSac", method=RequestMethod.POST)
@CDBusinessProcess("AutoRestrictApplyRemoveBarr")
//@RequestParam(value="sac") String sac,@RequestParam(value="applyRemoveFlag") String applyRemoveFlag,@RequestParam("searchType") String searchType,
public ModelAndView thirdPartyapplyRemoveBarrying(@RequestParam(value="bar") String barFlag, @RequestParam(value="code") String sacCode,@RequestParam(value="leNum") String leNum,HttpServletRequest request) {
    System.out.println("-----------------------Priting the requestParameterNames " + barFlag + " , " + sacCode +" , "+leNum);
    System.out.println("#####################..................... Inside the controller method applyRemoveBarrying ...."); 
         ApplyRemoveBarryingDTO newone = new ApplyRemoveBarryingDTO();
        newone.setSacCode(sacCode);
        newone.setLeCode(leNum);
        newone.setRecordType(searchType);
        newone.setAutoBarFlag(applyRemoveFlag);
        System.out.println("#################........................... Calling the applyRemoveBarry method of the Service Class");
         StoredProcedureResult result = restrictBarryingService.thirdPartyapplyRemoveBarry(newone,userId);
          if(result.isSuccess())
         {
             modelAndView.addObject("isSuccess",true);
         } else
         {
             modelAndView.addObject("isSuccess",false);
         }            
       return modelAndView;
}

and once this controller is executed i want to go the the first page that is autoRestrictSACSearchResult.jsp which should show the updated result for the values i passed in the first form which was didplayed thai is my jsp for search

<!-- sac search -->
  which is written above.

Upvotes: 0

Views: 1155

Answers (1)

dReAmEr
dReAmEr

Reputation: 7196

return new ModelAndView("redirect:/url");

Upvotes: 1

Related Questions