从 Spring Web 应用程序检索 servlet 上下文路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12236590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:21:24  来源:igfitidea点击:

Retrieving the servlet context path from a Spring web application

springservletscontextpath

提问by balteo

I would like to be able to dynamicallyretrieve the "servlet context path" (e.g. http://localhost/myappor http://www.mysite.com) for my spring web application from a Service spring bean.

我希望能够从Service spring bean动态检索我的 spring web 应用程序的“ servlet 上下文路径”(例如http://localhost/myapphttp://www.mysite.com)。

The reason for this is that I want to use this value in email that are going to be sent to users of the website.

这样做的原因是我想在将要发送给网站用户的电子邮件中使用这个值。

While it would be pretty easy to do this from a Spring MVC controller, it is not so obvious to do this from a Service bean.

虽然从 Spring MVC 控制器执行此操作非常容易,但从 Service bean 执行此操作并不那么明显。

Can anyone please advise?

任何人都可以请指教吗?

EDIT: Additional requirement:

编辑:附加要求:

I was wondering if there wasn't a way of retrieving the context path upon startup of the applicationand having it available for retrieval at all time by all my services?

我想知道是否没有办法在应用程序启动时检索上下文路径并让我的所有服务始终可以检索它?

回答by Andreas Berger

If you use a ServletContainer >= 2.5 you can use the following code to get the ContextPath:

如果您使用 ServletContainer >= 2.5,您可以使用以下代码来获取 ContextPath:

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component

@Component
public class SpringBean {

    @Autowired
    private ServletContext servletContext;

    @PostConstruct
    public void showIt() {
        System.out.println(servletContext.getContextPath());
    }
}

回答by stuchl4n3k

As Andreas suggested, you can use the ServletContext. I use it like this to get the property in my components:

正如 Andreas 所建议的,您可以使用ServletContext。我像这样使用它来获取组件中的属性:

    @Value("#{servletContext.contextPath}")
    private String servletContextPath;

回答by Rich Cowin

I would avoid creating a dependency on the web layer from your service layer. Get your controller to resolve the path using request.getRequestURL()and pass this directly to the service:

我会避免从您的服务层创建对 web 层的依赖。让您的控制器使用解析路径request.getRequestURL()并将其直接传递给服务:

String path = request.getRequestURL().toString();
myService.doSomethingIncludingEmail(..., path, ...);

回答by Biju Kunjummen

If the service is triggered by a controller, which I am assuming it is you can retrieve the path using HttpSerlvetRequest from the controller and pass the full path to the service.

如果服务是由控制器触发的,我假设它是您可以使用 HttpSerlvetRequest 从控制器检索路径并将完整路径传递给服务。

If it is part of the UI flow, you can actually inject in HttpServletRequestin any layer, it works because if you inject in HttpServletRequest, Spring actually injects a proxy which delegates to the actual HttpServletRequest (by keeping a reference in a ThreadLocal).

如果它是 UI 流的一部分,您实际上可以HttpServletRequest在任何层中注入,它会起作用,因为如果您注入HttpServletRequest,Spring 实际上会注入一个代理,该代理委托给实际的 HttpServletRequest(通过在 a 中保留一个引用ThreadLocal)。

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;

public class AServiceImpl implements AService{

 @Autowired private HttpServletRequest httpServletRequest;


 public String getAttribute(String name) {
  return (String)this.httpServletRequest.getAttribute(name);
 }
}