在spring mvc中如何获取控制器中的上下文路径 [英] In spring mvc how to get the context path in controller

查看:149
本文介绍了在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();
    }
}

这篇关于在spring mvc中如何获取控制器中的上下文路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆