In spring mvc how to get the context path in contr

2019-01-25 10:53发布

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

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

Please help me
Thanks

1条回答
Root(大扎)
2楼-- · 2019-01-25 11:38
  1. Variable request is declared, but is not initialized. No wonder you get a NullPointerException.

  2. Look at documentation to access different request related data.

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();
    }
}
查看更多
登录 后发表回答