在spring mvc中如何获取控制器中的上下文路径

问题描述:

我需要在控制器中使用应用程序上下文路径,我尝试下面的代码,它抛出NULLPOINTER EXCEPTION。

I need application context path in controller, I tried the below code its throwing NULLPOINTER EXCEPTION.


HttpServletRequest请求;

String Path = request.getContextPath();

HttpServletRequest request;
String Path = request.getContextPath();

请帮助我

感谢

Please help me
Thanks


  1. 已声明变量请求,但未初始化
    。难怪您会收到 NullPointerException

查看文档访问不同的请求相关数据。

Look at documentation to access different request related data.

读完后,并确定要将代码绑定到本机Servlet API,请尝试:

After you read that, and are sure you want to tie your code to native Servlet API, try this:

@Controller
class MyController {

    @RequestMapping
    public void handleMe(HttpServletRequest request) {
        String path = request.getContextPath();
    }
}