在没有 HttpServletRequest 的情况下获取类中的主机和 contextroot

发布于 2024-12-22 11:20:41 字数 763 浏览 2 评论 0原文

我目前正在创建一个处理邮件发送的类。对于其中一些邮件,我需要构建链接(帐户激活、重置密码、确认等)。

我不想对链接的主机进行硬编码,因为开发、测试和生产的主机会有所不同。因此,我需要在邮件类中以编程方式获取主机名和应用程序上下文。

我们在这个项目中使用 Spring MVC。

我将用一个例子来说明我想要的。

在我们的 JSP 中,我们有这样的代码片段:

<script src="<%=request.getContextPath()%>/js/jquery-1.7.js" type="text/javascript">

在这个上下文中,request.getContextPath() 将解析为 http://localhost:8080/applicationName

这也是我想要的邮件类。

我尝试在我的班级中实现 ServletContextAware, 我想我可以使用 servletContext.getContextPath() 获得类似的结果。但这个语句只能解析为 /applicationName

所以我的问题是: 如何在邮件类中获取完整的主机和上下文(http://localhost:8080/applicationName)?除了 ServletContextAware 之外,还有其他接口可以使用吗?请帮忙:-)

问候 丹尼尔

I´m currently creating a class for handling sending of mails. For some of these mails I need to construct links (account activation, reset password, confirmations etc).

I don´t want to hardcode the host of the links, as the host will be different for dev, test and prod. So I need to get the hostname and application context programatically in my mail class.

We are using Spring MVC in this project.

I´ll illustrate what I want with an example.

In our JSPs, we have snippets like this:

<script src="<%=request.getContextPath()%>/js/jquery-1.7.js" type="text/javascript">

In this context, request.getContextPath() will resolve to
http://localhost:8080/applicationName

This is what I want for my mail class aswell.

I attempted to implement ServletContextAware in my class,
thinking I would be able to use servletContext.getContextPath() with similar results. But this statement only resolves to /applicationName

So to my question:
How can I get the full host and context (http://localhost:8080/applicationName) in my mail class? Is there some other interface than ServletContextAware that I can use? Help please :-)

Regards
Daniel

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

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

发布评论

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

评论(1

孤星 2024-12-29 11:20:41

我在发布之前进行了搜索,但显然不够努力。这个完全相同的问题已经被问过(并回答过)。

仅使用 ServletContext 查找应用程序的 URL

I did search before posting, but apparently not hard enough. This exact same question has been asked (and answered) already.

Finding your application's URL with only a ServletContext

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