简体   繁体   English

为什么只有当控制器请求映射注释中存在一个级别的URL路径时,Spring才会解析视图

[英]Why does spring resolve views only when one level url path present on controller request mapping annotation

I am implementing a website with Spring MVC and I've noticed the following problem. 我正在使用Spring MVC实现一个网站,并且注意到以下问题。 I have a controller like this one 我有一个像这样的控制器

/**
 * Created by adelin.ghanayem@cayetanogaming.com on 4/7/15.
 */
@Controller
@RequestMapping(value = "/")
public class UserControlPanelController {

    // this url has a second level. /user/panel
    @RequestMapping(value = "/user/panel",method = RequestMethod.GET)
    @Secured(value = {"ROLE_USER"})
    public ModelAndView mainPage(){
         return new ModelAndView("user-panel.jsp");
    }
}

The view user-panel.jsp is not resolver I receive 我收到的视图user-panel.jsp不是解析器

HTTP ERROR 404

Problem accessing /myapp/user/login-user.jsp. Reason:

    /myapp/user/login-user.jsp

However, when I map the method to @RequestMapping(value = "/user",method = RequestMethod.GET) 但是,当我将方法映射到@RequestMapping(value = "/user",method = RequestMethod.GET)

The view is resolved. 视图已解决。 The jsp page is placed at the root of the webapp (web directory ) jsp页面位于webapp的根目录(Web目录)

Now when I tried to map the method to @RequestMapping(value = "/user/panel",method = RequestMethod.GET) and return new ModelAndView("/user-panel.jsp"); 现在,当我尝试将方法映射到@RequestMapping(value = "/user/panel",method = RequestMethod.GET)return new ModelAndView("/user-panel.jsp"); NOTE THE ROOT SLASH. 注意根碎片。 The view resolved properly. 视图已正确解决。

You need to specify a view resolver in your configuration so spring knows where to look for the views. 您需要在配置中指定视图解析器,以便Spring知道在哪里查找视图。 If you do not provide one, you must specify the path to the file, which is why the "user-panel.jsp" does not work, but the "/user-panel.jsp" does work. 如果不提供,则必须指定文件的路径,这就是为什么“ user-panel.jsp”不起作用,而“ /user-panel.jsp”起作用的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring请求映射URL到一个控制器 - Spring Request Mapping URL's to One Controller 这个 Spring Boot controller 方法注释有什么问题? (请求的路径在 class 级别定义,路径变量在方法中定义) - What is wrong in this Spring Boot controller method annotation? (the path of the request is defined at class level and the path variable at method) Spring 中的分页通过路径变量引导 - Controller 请求映射注释中不存在请求的路径变量 - Pagination in Spring Boot via path variable - Requested path variable is not present in Controller request mapping annotations Spring Controller请求映射无法正常工作 - Spring Controller Request Mapping Does Not Work Correctly 用于根路径的Spring Boot请求映射控制器仅适用于双斜杠 - Spring Boot request mapping Controller for root path working only with double slash 如何解决spring控制器中请求映射“不明确”的情况? - How to resolve request mapping “ambiguous” situation in spring controller? Spring Controller的URL请求映射仅对带有字符串的空白页无效 - Spring Controller's URL request mapping not working only a blank page with a string 两种Spring Controller方法,一种返回200,另一种返回404。仅与映射URL不同 - Two Spring Controller Methods, One Returns 200, Other Returns 404. Only Differ By The Mapping URL 当文件存在于多个包中时,Spring 引导 URL 映射不起作用 - Spring Boot URL mapping not working when files are present in multiple packages 为什么控制器中的@RequestMapping Spring注释会捕获更多我想要的? - Why does @RequestMapping Spring annotation in controller capture more that I want?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM