获取 Velocity 模板内 web 应用程序的绝对上下文路径

发布于 2024-11-07 16:20:05 字数 1032 浏览 1 评论 0原文

我有一个相当简单(我希望:P)的问题。

我知道,在良好的旧 Java servlet 代码中,我可以导出进入 Web 服务器的请求的完整请求路径,如下所示:

String requestBaseUrl = 
    String.format("%s://%s:%s%s", request.getScheme(), request.getServerName(), 
            request.getServerPort(), request.getContextPath());

现在,在 JSP 中执行完全相同的操作非常简单。但是,我现在必须在 Velocity 1.5 模板中执行此操作。具体来说,我需要将该信息输出为需要集成到我的 Web 应用程序中的 Webstart 小程序的“代码库”属性。

我尝试了这个,但小程序拒绝启动,而是抛出 FileNotFoundException :P

<script src="http://www.java.com/js/deployJava.js"></script>
<script>
    var attributes = { code:'org.groundhog.MyApplet', width:360, height:280} ;
    var parameters = {
        jnlp_href: './webstart/myapplet.jnlp', <------------------ this
        bgColor: '#4f616d', mayscript: true
    };
    deployJava.runApplet(attributes, parameters, '1.6');

因此,我认为最可靠的方法是派生并粘贴应用程序运行的绝对 URL,并引用小程序的 jnlp 文件使用与我之前的 Java 代码片段相同的逻辑。

现在我正在翻阅 Velocity 的文档。但是,如果有人不得不处理类似的事情,我会感谢 Velocity 中的等效习惯用法。

我感谢您的时间和帮助!

I have a fairly simple (I hope :P) question.

I know that in good old Java servlet code, I can derive the full request path of a request that comes into the web server like this:

String requestBaseUrl = 
    String.format("%s://%s:%s%s", request.getScheme(), request.getServerName(), 
            request.getServerPort(), request.getContextPath());

Now, doing the exact same thing in JSP is pretty straightforward to do. However, I now have to do this in Velocity 1.5 template. Specifically, I need to output that information as the 'codebase' attribute of a webstart applet I need to integrate into my web application.

I tried this, but the applet refused to start, instead throwing a FileNotFoundException :P

<script src="http://www.java.com/js/deployJava.js"></script>
<script>
    var attributes = { code:'org.groundhog.MyApplet', width:360, height:280} ;
    var parameters = {
        jnlp_href: './webstart/myapplet.jnlp', <------------------ this
        bgColor: '#4f616d', mayscript: true
    };
    deployJava.runApplet(attributes, parameters, '1.6');

So I think that the most reliable way to do this is to derive and stick in the absolute URL where the application is running, and reference the applet's jnlp file from there using the same logic as in my previous Java snippet.

Right now I'm shuffling through Velocity's documentation. But, if anyone has had to deal with something similar, I'd be thankful for the equivalent idiom in Velocity.

I thank you for your time and help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽梦紫曦~ 2024-11-14 16:20:05

好吧,经过短暂的头脑清醒休息后,我意识到,本质上,Velocity 只是一个文本模板引擎,不应该在其视图中进行代码欺骗。马特的评论证实了我的怀疑;)。谢谢你!

最简单的事情是首先在我的控制器对象中派生代码库 URL,然后将其放入模板的模型中(例如,一个名为“codeBaseUrl”的变量),然后让 Velocity 渲染它。简而言之,就是上述两种方法的结合。

我表示歉意,因为我再次意识到 8 小时的睡眠几乎可以解决任何问题。 :P

谢谢您的宝贵时间。

Well, after a short mind-clearing break, I came to the realization that essentially, Velocity is just a text template engine, and should have no business in doing code trickery in its views. Matt's comment just confirmed my suspicions ;). Thank you!

The simplest thing to do was to first derive the code base URL in my controller object, and then put that into the template's model (say, a variable called "codeBaseUrl"), and just let Velocity render it. In short, a combination of the two approaches above.

I offer my apologies, since I once again realize that 8 hours of sleep can solve nearly any problem. :P

Thank you for your time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文