The application supports spring mvc + hibernate + maven + tomcat. There is one controller:

@Controller public class ControllerBookmarks { private BookmarksService bookmarksService; private static final Logger logger = LoggerFactory.getLogger(ControllerBookmarks.class); @Autowired @Qualifier("bookmarksService") public void setBookmarksService(BookmarksService bookmarksService) { this.bookmarksService = bookmarksService; } @RequestMapping(value = "/bookmarks", method = RequestMethod.GET) public String listBookmarks(Model model) { logger.info("Вызван: listBookmarks"); model.addAttribute("bookmark", new Bookmarks()); model.addAttribute("bookmarks", bookmarksService.listBookmarks()); return "bookmarks"; } 

And there is one jsp, her name - bookmarks
Here is the project structure.

enter image description here

  • The application is deployed in tomcat with context - / .
  • Dispatcher servlet Springs handles any requests to the address - / .

    If I launch the application this way, then everything works fine, the url is localhost:8080/bookmarks . But I want to write @RequestMapping("/bm") above the controller. When I do this and contact the address - localhost:8080/bm/bookmarks . I don’t get my jsp That's what it says to me in error - Message - /bm/WEB-INF/view/bookmarks.jsp. Error code 404. Where I could not indicate something or missed, please tell me, at least approximately?


That's what I want to do, I wrote the request mappings and when I write such a URL in the browser - localhost:8080/bm/bookmarks . I get 404 and message, which I wrote above. It must process the listBookmarks and return the necessary jsp. I understand that this message - Message - /bm/WEB-INF/view/bookmarks.jsp is based on what I wrote @RequestMapping("/bm") above the controller and what the resolver finds in the folder with the view. But why does he build such a path and how can I not fix it

 @Controller @RequestMapping("/bm") public class ControllerBookmarks { private BookmarksService bookmarksService; private static final Logger logger = LoggerFactory.getLogger(ControllerBookmarks.class); @Autowired @Qualifier("bookmarksService") public void setBookmarksService(BookmarksService bookmarksService) { this.bookmarksService = bookmarksService; } @RequestMapping(value = "/bookmarks", method = RequestMethod.GET) public String listBookmarks(Model model) { logger.info("Вызван: listBookmarks"); model.addAttribute("bookmark", new Bookmarks()); model.addAttribute("bookmarks", bookmarksService.listBookmarks()); return "bookmarks"; } 
  • Well, just write @RequestMapping (value = "/ bm / bookmarks", method = RequestMethod.GET) - ermak0ff
  • Put jsp in the folder with resources. - Roman C
  • @ ermak0ff, and why is it written above the method, if I want to write this part of /bm above the controller, this part of the URL is substituted for each method. I will add the controller code to the end of the question as I write and this option does not work for me. - Maks Ohotnikov pm
  • @Roman C, how then to specify a resolver that displays these jsp paths to the folder that is behind the web folder - Maks Ohotnikov
  • I tried to combine 2a @RequestMapping in my project, it works as you expected. Looking for a mistake elsewhere. Try running the debugger and find out where the error occurs and what the exception is thrown. - Krychun Ivan pm

1 answer 1

The problem is in the viewResolver settings. The prefix must begin with /. If the prefix does not begin with /, then the pattern path is considered relative and is determined relative to the requested path, from where / bm is taken. The correct option to customize the viewResolver is:

 <bean id="resolver" сlass="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/resources/views/"/> <property name="suffix" value=".jsp"/> </bean> 
  • Yes, everything worked, I did not pay attention to it at all. I tried 100,500 times different options and constantly natykalsy on this incomprehensible way for me - /bm/WEB-INF/view/bookmarks.jsp . But now it became clear why /bm becomes before /WEB-INF . Thank you very much. - Maks Ohotnikov