Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set dynamically scheme attribute in s:url tag?
    text
    copied!<p>I'm trying to build some URLs using <code>s:url</code> tag using the <code>scheme</code> attribute, whose value varies dynamically on some criteria (e.g. the same URL will be built with <code>http</code> for some requests and with <code>https</code> for some others). All <code>s:url</code> in the application will be built either with <code>http</code> or <code>https</code> for a given request.</p> <p>I have programmed the logic for this criteria and stored the schema to be used (<code>http</code> or <code>https</code>) as a String field in my Action, but seems I can't use expressions in the <code>scheme</code> attribute of the <code>s:url</code> tag:</p> <p>When I try to render an HTML anchor using an URL built with this:</p> <pre><code>&lt;s:url scheme="%{myScheme}" id="myUrl" action="MyAction" includeParams="none"&gt; &lt;s:param name="someParam" value="blah"/&gt; &lt;/s:url&gt; &lt;s:a href="%{myUrl}"&gt;click me&lt;/s:a&gt; </code></pre> <p>I get:</p> <pre><code>&lt;a href="%{myScheme}://myhost.com/mywebapp/MyAction.action?someParam=blah"&gt;click me&lt;/a&gt; </code></pre> <p>Also tried to do an ugly thing, even without using the actual action property (if that worked, I would change the literal for the actual value). Tried to use a scriptlet:</p> <pre><code>&lt;s:url scheme="&lt;%= "https" %&gt;" id="myUrl" action="MyAction" includeParams="none"&gt; &lt;s:param name="someParam" value="blah"/&gt; &lt;/s:url&gt; </code></pre> <p>But got a <code>500</code> error and this log message: </p> <pre><code>org.apache.jasper.JasperException: /path/to/my/file.jsp(123,456) According to TLD or attribute directive in tag file, attribute scheme does not accept any expressions </code></pre> <p>I tried also with the following ugly options, which didn't work:</p> <pre><code>&lt;s:url scheme="&lt;s:property value="myScheme"/&gt;" id="myUrl" action="MyAction" includeParams="none"&gt; &lt;s:param name="someParam" value="blah"/&gt; &lt;/s:url&gt; </code></pre> <p>and:</p> <pre><code>&lt;s:url scheme="&lt;c:out value="${myScheme}"/&gt;" id="myUrl" action="MyAction" includeParams="none"&gt; &lt;s:param name="someParam" value="blah"/&gt; &lt;/s:url&gt; </code></pre> <p>So I ran out of ideas. Did anybody face the same problem? The remainder of the question explains why I need to do this:</p> <p>My application is behind a load balancer that receives HTTPS requests, but forwards them to the application server with HTTP, and then sends the responses back with HTTPS to the internet. So the application servers always receive <code>HTTP</code> traffic.</p> <p>The links in the application must be generated with https scheme when requested via the load balancer (so users in the internet will be secured) and with http when requested directly (when we developers need to access a specific front-end within our internal network). I have no control over servers configuration nor so much influence over sysadmins, so changing server or network configurations seems rather problematic.</p> <p>UPDATE</p> <p>Seems that if I extend the <code>org.apache.struts2.components.ServletUrlRenderer</code> Struts2 class overriding the <code>renderUrl</code> method I would be able to do what I want. But I do not know how I would tell Struts2 to use my custom implementation of <code>UrlRenderer</code> instead of the default <code>ServletUrlRenderer</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload