Skip to content
Advertisement

Spring MVC – JSP files for various controllers to be more generic

I have a java spring MVC project with various controllers. These controllers have the functionality to add, edit, delete configurations. The JSP files for these various controllers are almost same (with only differences like the number of parameters, title of the page, etc).

Eg. I have a JSP file to add certificate for a server with the options for the end user to mention the name and the location of the certificate. Another JSP file is to add server details asking the end user about the server name, the OS etc.

All of these JSP files have a similar functionality on the Submit button, adding the code here –

        <div class="submitButton center">
            <input type="button" class="center actionButton" name="submitButton" value="${saveLbl}" id="submitButtonId" onclick="return submitForm('addForm', '${formAction}', '');"/>
            <c:if test="${repCertificateEntityForm.controllerModel.formType == 'ADD'}">
                <span class="padLeftMedium"></span>
                <input type="button" class="actionButton" name="saveButton"  value="Save and Add Another" id="saveAndAddAnotherButtonId" onclick="return submitForm('addForm', '${saveAndAddAnotherUrl}', '');"></input>
            </c:if>
            <c:if test="${repCertificateEntityForm.controllerModel.formType == 'EDIT' && showDelete}"> 
                <span class="padLeftMedium"></span>
                <input type="button" class="actionButton" name="deleteButton"  value="${deleteLbl}" id="deleteButtonId" onclick="return submitForm('addForm', '${deleteUrl}', 'Delete the Certificate?');"></input>
            </c:if>
            <span class="padLeftMedium"></span>
            <input type="button" class="actionButton" name="cancelButton"  value="Cancel" id="cancelButtonId" onclick="handleGetAction('${returnUrl}');" ></input>
        </div>

All my JSP files have this common code, the only difference being the form name (repCertificateEntityForm in this case). Can we have a single JSP file to contain this submit button code and all my JSPS would refer this single JSP file and pass the form name to it dynamically? Please suggest.

EDIT 1:

In my first jsp, I added the following code which calls the second jsp and passes a param :

    <jsp:include page="config.jsp" >
            <jsp:param name="formName" value="repCertificateEntityForm" />
        </jsp:include>

In config.jsp, I added the following code (I want to access the param formName with the value repCertificateEntityForm in this section)-

<div class="submitButton center">
    <input type="button" class="center actionButton" name="submitButton" value="${saveLbl}" id="submitButtonId" onclick="return submitForm('addForm', '${formAction}', '');"/>
    <c:if test="${param.formName.controllerModel.formType == 'ADD'}">
        <span class="padLeftMedium"></span>
        <input type="button" class="actionButton" name="saveButton"  value="Save and Add Another" id="saveAndAddAnotherButtonId" onclick="return submitForm('addForm', '${saveAndAddAnotherUrl}', '');"></input>
    </c:if>
    <span class="padLeftMedium"></span>
    <input type="button" class="actionButton" name="cancelButton"  value="Cancel" id="cancelButtonId" onclick="handleGetAction('${returnUrl}');" ></input>
</div> 

Right now, if I pass the hardcoded value of repCertificateEntityForm here in this div block, everything works, but I want this to be done via the jsp param so that the form names become dynamic. Thankyou.

Advertisement

Answer

You might use <jsp:include> with a <jsp:param>.

<jsp:include page="yourFragment.jsp" >
 <jsp:param name="formName" value="repCertificateEntityForm" />
</jsp:include>

See more details here.

Another option would be JSTL’s <c:import> tag with a <c:param> (more flexible than JSP include):

<c:import url="yourFragment.jsp">
  <c:param name="formName" value="repCertificateEntityForm" />
</c:import>

See more details here.

Alternatively, you can use JSP tag files.

<h:yourTag formName="repCertificateEntityForm" />

See more details here.


Note that my examples above were just that, examples, so I used repCertificateEntityForm as the name of the form. In your code using <jsp:include>, repCertificateEntityForm is an object with properties on it, and you are trying to retrieve those properties. But the parameters can only be strings, so you most likely get an exception of:

javax.el.PropertyNotFoundException: Property ‘controllerModel’ not found on type java.lang.String

or something similar.

You are testing for a form type like ADD, so you can change your code like this in your first JSP:

<jsp:include page="config.jsp">
  <jsp:param name="formType" value="${repCertificateEntityForm.controllerModel.formType}" />
</jsp:include>

And then, in your config.jsp, you can test with:

<c:if test="${param.formType == 'ADD'}">

One other thing to be aware of is that the included JSP is included in the same context as the current JSP, so it has access to scope attributes, like the request attributes for example. If your repCertificateEntityForm was added in scope with something like request.setAttribute("repCertificateEntityForm", form); then the config.jsp can access it directly because it will be in scope. In that case you can change your first JSP to include just this:

<jsp:include page="config.jsp" />

And in your config.jsp you can retrieve the whole form object and keep your test like this:

 <c:if test="${repCertificateEntityForm.controllerModel.formType == 'ADD'}">

Finally, I highly recommend you read some tutorials or documentation to get a better understanding of how things work in your application.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement