How to get server name and server port from Liferay rate template?

In the jsp page, we can get the server name and server port using request.getServerName () and request.getServerPort (). `

Since we cannot get the HttpServletRequest from the Liferay rate template, is there any other way to get the server name and server port? Please answer with a small piece of code.

+3


source to share


2 answers


In Liferay sources you can find com.liferay.portal.velocity.VelocityVariablesImpl

.

This class is placed under portal-impl/src/com/liferay/portal/velocity/VelocityVariablesImpl.java

.

If you check all entries in the context of speed (type strings velocityContext.put(String key, Object value)

) and especially the ones in the method insertVariables

, you will see that this provides you with a httpServletRequest under the name "request"

.

Therefore, in your template, you access your request object like any other speed context object using a key $request

.

Thus, this object can be used with all its methods and properties (if they are public).

So just

$request.getServerName()

      

and

$request.getServerPort()

      



Also, if you want to set the speed variable to one of them, follow these steps

#set ($my_amazing_variable = $request.getServerPort())

      

Then you can use it $my_amazing_variable

as any regular speed letter.

Hope it helps.


NOTA BENE!

Please note that you do not have access to the exact set of variables and macros under all types of speed templates in Liferay. There are different sets for

  • theme templates
  • layout templates
  • web content templates
+4


source


I created my url with below code to login up. Thanks Ar3s.



#set($protocol = "http://")
#set($host = "$request.getServerName()")
#set($port = "$request.getServerPort()")
#set($column = ":" )
#set($url = "/c/portal/login?p_l_id=10858" )
#set($hrefurl = "$protocol$host$column$port$url")


<a class="sign-in" data-redirect="false" href="$hrefurl" id="yui_patched_v3_11_0_1_1420097083820_231" role="menuitem" title=""> <span id="yui_patched_v3_11_0_1_1420097083820_865" class="nav-item-label"> Sign In </span> </a>

      

0


source







All Articles