I have a static import of my Utility class:
<%@ page import="static com.groupgti.webclient.util.MappingUtils.*" %>
I have some constants in this class:
public static final String HTM = ".htm";
public static final String SECURE_REQUEST_MAPPING = "/secure/";
public static final String CANDIDATE_DETAILS_DATA_FORM_MAPPING = "assessments/candidate-details-data";
I have a Spring MVC form:
<form:form cssClass="springForm"
action="${pageContext.servletContext.contextPath}<%=SECURE_REQUEST_MAPPING + CANDIDATE_DETAILS_DATA_FORM_MAPPING + HTM%>"
commandName="assessments/candidate-details-request">
</form:form>
Why when I am using like this:
<a href="${pageContext.servletContext.contextPath}<%=SECURE_REQUEST_MAPPING + CANDIDATE_DETAILS_DATA_FORM_MAPPING + HTM%>">
some text
</a>
Value of href attribute is generated correctly, and in spring form action attribute the HTML code is like this: /webclient<%=SECURE_REQUEST_MAPPING + CANDIDATE_DETAILS_DATA_FORM_MAPPING + HTM%>. The values of those constants are not shown. Why is that and what should I do to make it work?
JSP allows you to use scriptlet expressions (
<%= ... %>) in attributes of custom tags (such as<form:form>), but it must be the only content of the attribute. Therefore you cannot mix<%= ... %>expression with EL expressions or plain text in attributes of custom tags.However, you can use any content in attributes of regular HTML tags, because these tags have no special meaning for JSP, that’s why it works with
<a>.One possible solution is to put a result of scriptlet expression into request attribute and use it in EL expression:
Alternatively, you may choose to use scriptlets without EL, for example, by defining a method that appends context path in your
MappingUtils:Note that for historical reasons (EL expressions were designed to replace scriptlets) JSP doesn’t provide elegant ways to mix EL and scriptlets, therefore this kind of problems is pretty much expected.