Search code examples
springspring-mvcmodel-view-controllerspring-annotations

SPRING WEB MVC ANNOTATIONS-Method for Partial Page Rendering


Hello Friends,

[Need some help regarding spring web mvc annotation based:]

How to use partial page rendering on spring web mvc annotations.

  • Master Page Name: CreateEntity.jsp
  • Partial jsp page: Account.jsp, Lead.jsp etc etc*

Now, this master page consists of drop down list where if Account is selected then partial page Account will get load into master page named CreateEntity. Else if Lead is Selected then Lead will get into the master page.

I have Created the Controller named CreateEntityController.java into which have written code as follows:

***CreateEntityController.java***
 @RequestMapping(value = { "/CreateEntity" }, method = RequestMethod.GET)  
 public ModelAndView CreateEntity(Model model) {  
 model.addAttribute("module", "Account");  
 model.addAttribute("msg", "This is the partial view page!");                                    
 return new ModelAndView("CreateEntity");
 }

JSP Page Code: (CreateEntity.jsp)-Master Page

<li><c:url value="/CreateEntity" var="CreateEntity"  /> 
                            <a data-params='{"module":"CreateLeadStatus"}'
                                href="<c:out value='${CreateEntity}'/>">Create Entity</a></li>

<div>
    <%@include file="CreateLeadStatus.jsp"%>
</div>

JSP Page Code 2: *Partial Page Code: ***Account.jsp****

 <spring:url value="CreateEntity" var="CreateEntity" />

<form class="form-Account" action="${CreateEntity}" method="POST"  role="form" >
<div class="crmBodyWin" style="">
    <div id="" class="p30 lbox set_mw">
        <div id="secDiv_Lead_Information" class="editParentSection pL30 pR30">
            <table style="width: 100%" cellspacing="0" cellpadding="0"
                id="secHead_Lead_Information">
                <tbody>
                    <tr>
                        <td class="contHeadInfo">Lead Status</td>
                    </tr>
                </tbody>
            </table>

            <div style="width: 100%" class="secContent" border="0"
                cellspacing="1" cellpadding="0" id="secContent_Lead_Information">
                <div class="contInfoTab floatL formViewCreate">
                    <div id="Leads_fldRow_COMPANY" class="textFld COMPANY tabDivCreate">
                        <div class="labelTabCreate pL5 pR newmandatory"
                            id="Crm_Leads_COMPANY_label">Status Name</div><div class="labelValCreate mL45">
                            <input
                                type="text" class="textField" style="width: 100%"
                                id="txtleadname" name="txtleadname" tabindex="5" maxlength="100"
                                data-maxlength="100" data-customfield="false"
                                data-decimal-length="2" data-label="leadname"
                                data-readonly="false" value="">
                        </div>
                        <div class="clearB"></div>
                    </div>
                    <br>
            <button class="btn btn-lg rest-btn-active btn-block" type="submit">Create Lead Status-TEST</button>
                </div>

                <div class="contInfoTab floatR formViewCreate"></div>


            </div>

        </div>
    </div>

</div>
</form>

Please tell me that is there any solution for it such a manner:

- Example:

If{ (Account )-is selected then Account.jsp - partial view loads on that master page**(CreateEntity)** } else if (Lead)-is selected then Lead.jsp -partial view loads on that master page**(CreateEntity)** }

module can be the common method in controller: For example on selected choice as Account. The URL lookes something like this:

  • For Account its should look something like this:

http://localhost:8080/projectname/CreateEntity?module=Account

  • For Lead its should look something like this:

http://localhost:8080/projectname/CreateEntity?module=Lead

Please kindly help me regarding this topic.

Thank You!


Solution

  • As far as i understand your question. Your goal is to include a jsp page based on your URL param(module). To do that you have to change your controller to this

    ***CreateEntityController.java***
      @RequestMapping(value = { "/CreateEntity" }, method = RequestMethod.GET)  
      public ModelAndView CreateEntity(String module,Model model) {  
          model.addAttribute("module", module);  
          model.addAttribute("msg", "This is the partial view page!");
          if(module.equals("Account"){
               //account related attributes
               // call your service or bo and send account related object
               model.addAttribute("account",yourAccountObject)
          }
          if(module.equals("Lead"){
              //lead related attributes
              // call your service or bo and send lead related object
               model.addAttribute("lead",yourLeadObject)
          }
          return new ModelAndView("CreateEntity");
      }
    

    CreateEntity.jsp

     <%-- common code --%>
     <li>
            <a data-params='{"module":"CreateAccountStatus"}' href="<c:url value='CreateEntity?
                  module=Account'/>">Create Account </a>
    </li>
     <li>
            <a data-params='{"module":"CreateLeadStatus"}' href="<c:url value='CreateEntity?
                  module=Lead'/>">Create Account </a>
     </li>
    
    <div>
        <%-- Your partial condition --%>
        <c:choose>
          <c:when test="${module=='Account'}">
             <jsp:include page="Account.jsp" /> 
          </c:when>
           <c:when test="${module=='Lead'}">
             <jsp:include page="Lead.jsp" /> 
          </c:when>
        </c:choose>
    
    </div>