{"id":1497,"date":"2015-11-17T15:15:55","date_gmt":"2015-11-17T22:15:55","guid":{"rendered":"http:\/\/10kdev.net\/?p=1497"},"modified":"2015-11-18T08:36:39","modified_gmt":"2015-11-18T15:36:39","slug":"jstl-time-travel-resolution","status":"publish","type":"post","link":"http:\/\/10kdev.net\/?p=1497","title":{"rendered":"JSTL <c:url> time travel resolution"},"content":{"rendered":"<div style=\"width: 484px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"\" src=\"http:\/\/10kdev.net\/wp-content\/uploads\/2015\/11\/4dec8532e8c1ac9e28f020349b8ab49c.png\" alt=\"\" width=\"474\" height=\"266\" \/><p class=\"wp-caption-text\">Your variable may be assigned anything at any time if you know the secret to time travel.<\/p><\/div>\n<p>Working on a ticket to address a wrong form action url being used for a different page condition, I ran across a curiosity in a jstl tag file. \u00a0It seems that the value for &lt;c:url&gt; was not resolving in a cascading manner as had been the intended behavior of the initial author.<\/p>\n<p>The bit of code I was working on handled mapping search form\u00a0that either used one of two user selectable conditions:<\/p>\n<ol>\n<li>Address\/zip entered by user, which would call endpoint <em><strong>\/mapping <\/strong><\/em>(and separate java controller method getMapping).<\/li>\n<li>\u00a0Or browser location (latitude\/longitude), should you as a user allow it.\u00a0This would call endpoint <em><strong>\/mapping\/location<\/strong><\/em> (and java controller method getLocation).<\/li>\n<\/ol>\n<p>The\u00a0defect: only \/mapping was being used in both cases, so if a lat\/long was passed getMapping would break.<\/p>\n<p>The initial tag code looked like this:<\/p>\n<pre>&lt;c:url value=\"\/mapping\" var=\"search\" \/&gt;\r\n&lt;form:form action=\"${mapping}\" method=\"get\" id=\"results\" commandName=\"MapForm\"&gt;\r\n  &lt;c:if test=\"${URL eq 'Mapping' }\"&gt;\r\n    &lt;input type=\"hidden\" value=\"${q}\" name=\"q\" \/&gt;\r\n    &lt;c:url value=\"\/mapping\" var=\"search\" \/&gt;\r\n  &lt;\/c:if&gt;\r\n  &lt;c:if test=\"${URL eq 'Location' }\"&gt;\r\n    &lt;input type=\"hidden\" value=\"${latitude}\" name=\"latitude\" \/&gt;\r\n    &lt;input type=\"hidden\" value=\"${longitude}\" name=\"longitude\" \/&gt;\r\n    &lt;c:url value=\"\/mapping\/location\" var=\"search\" \/&gt;\r\n  &lt;\/c:if&gt;\r\n  &lt;button\/&gt;\r\n&lt;\/form:form&gt;\r\n<\/pre>\n<p>In the above\u00a0code the original author had intended the variable <strong>search<\/strong> to be assigned a value LATER based on the source of the user&#8217;s choice, but the tag never assigns it and the form action is always &#8220;\/mapping&#8221;. \u00a0I looked around (rather quickly) for documentation on this and couldn&#8217;t find anything other than a few allusions to tag libraries executing in the order they are written. \u00a0This makes sense to me, because even in a jsp page compiled to a servlet, if a variable was assigned that&#8217;s the value, and if it changes later it&#8217;s previous value\u00a0is not going to change of course in it&#8217;s use without time travel. \u00a0Now that I think of it, this seems pretty obvious.<\/p>\n<p>My solution was to change the code to this:<\/p>\n<pre>&lt;c:url value=\"\/mapping\" var=\"search\" \/&gt;\r\n&lt;c:if test=\"${URL eq 'Location' }\"&gt;\r\n    &lt;c:url value=\"\/mapping\/location\" var=\"search\" \/&gt;\r\n&lt;\/c:if&gt;\r\n&lt;form:form action=\"${mapping}\" method=\"get\" id=\"results\" commandName=\"MapForm\"&gt;\r\n  &lt;c:if test=\"${URL eq 'Mapping' }\"&gt;\r\n    &lt;input type=\"hidden\" value=\"${q}\" name=\"q\" \/&gt;\r\n  &lt;\/c:if&gt;\r\n  &lt;c:if test=\"${URL eq 'Location' }\"&gt;\r\n    &lt;input type=\"hidden\" value=\"${latitude}\" name=\"latitude\" \/&gt;\r\n    &lt;input type=\"hidden\" value=\"${longitude}\" name=\"longitude\" \/&gt;\r\n  &lt;\/c:if&gt;\r\n  &lt;button\/&gt;\r\n&lt;\/form:form&gt;<\/pre>\n<p>You know something? \u00a0Now that I look at this I have a feeling this thing wasn&#8217;t even tested! \u00a0And seriously, who would write:<\/p>\n<pre>int i = 4;\r\nint j = i + 2;\r\ni = 17;\r\nprint j;\r\n<\/pre>\n<p>And expect the answer to be 19?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Working on a ticket to address a wrong form action url being used for a different page condition, I ran across a curiosity in a jstl tag file. \u00a0It seems that the value for &lt;c:url&gt; was not resolving in a cascading manner as had been the intended behavior of the initial author. The bit of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1497"}],"collection":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1497"}],"version-history":[{"count":6,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1497\/revisions"}],"predecessor-version":[{"id":1503,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1497\/revisions\/1503"}],"wp:attachment":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1497"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}