简体   繁体   中英

In spring mvc how to get the context path in controller

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. 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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM