Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

java - 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...