问题描述
在jsp页面中,我们可以使用以下命令获取服务器名称和服务器端口 request.getServerName()和request.getServerPort().
In jsp page we can get Server Name and Server Port by using request.getServerName() and request.getServerPort().`
由于我们无法从Liferay速度模板中获取HttpServletRequest,是否还有其他方法可以获得服务器名称和服务器端口?请回答一个小的代码段.
As we can't get HttpServletRequest from Liferay velocity template, Is there any other way toget both Server Name and Server Port ? Please answer with a small code snippet..
推荐答案
在Liferay资源中,您可以找到com.liferay.portal.velocity.VelocityVariablesImpl
.
In your Liferay sources you can find com.liferay.portal.velocity.VelocityVariablesImpl
.
此类位于portal-impl/src/com/liferay/portal/velocity/VelocityVariablesImpl.java
下.
如果您检查速度上下文的所有条目(如velocityContext.put(String key, Object value)
之类的行),并且特别检查insertVariables
方法中的那些条目,您会发现这会将您的httpServletRequest公开为"request"
.
If you check all entries to velocity context (lines like velocityContext.put(String key, Object value)
) and specially the ones in the insertVariables
method, you'll see that this exposes you httpServletRequest under the name "request"
.
因此,您可以在模板中使用键$request
来访问请求对象,就像访问其他任何速度上下文对象一样.
Therefore, in your template you access your request object as any other velocity context object with the key $request
.
然后,该对象将可用于其所有方法和属性(如果已公开).
This object will, then, be usable with all it's methods and properties (if public).
所以就做
$request.getServerName()
和
$request.getServerPort()
此外,如果要将速度变量设置为其中之一,请执行以下操作
Furthermore, if you want to set a velocity variable to one of those, just do as follows
#set ($my_amazing_variable = $request.getServerPort())
然后您就可以将$my_amazing_variable
用作任何常规的速度垃圾.
You'll then be able to use $my_amazing_variable
as any regular velocity litteral.
希望这会有所帮助.
NOTA BENE!
NOTA BENE !
请注意,您无权访问Liferay中所有类型的速度模板下的完全相同的变量和宏集.有不同的集合
Note that you do not have access to the exact same set of variables and macros under all types of velocity templates in Liferay.There are different sets for
- 主题模板
- 布局模板
- 网络内容模板
这篇关于如何从Liferay速度模板获取服务器名称和服务器端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!