【问题标题】:No Mapping Found in HTTP Request with URI [springcodes/functions.jsp]在带有 URI 的 HTTP 请求中找不到映射 [springcodes/functions.jsp]
【发布时间】:2015-08-05 10:53:25
【问题描述】:

在提出新问题之前,我一直在阅读此参考资料,并且可能有重复的问题:

No mapping found for HTTP request

我已经浪费了好几个小时来拉扯头发。

这是我的示例代码:

Dispatcher-servlet

<context:component-scan base-package="springcodes.controller" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

Web.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

控制器(自从我的 index.jsp 成功显示以来,此控制器似乎正在工作。)

@RequestMapping("/welcome")
public String displayIndex(@RequestParam(value="name", required=false, defaultValue="Guestxx") String name, Model model) {    
    model.addAttribute("name", name);      
    // returns the view name
    return "welcome";
}

@RequestMapping("/")
public String displayIndex2(@RequestParam(value="name", required=false, defaultValue="Guesii") String name, Model model) {    
    model.addAttribute("name", name);
    // returns the view name
    return "welcome";
}

有问题的控制器:(当我输入此 URL 时:http://localhost:8080/springcodes/functions.jsp 应该显示某些内容,但目前无法正常工作

@Controller
public class FunctionController {

@RequestMapping("/functions")
public String displayFunctionsPage(Model model) {     

    model.addAttribute("funcList", "Function List");
    // returns the view name
    System.out.println("are we inside?");
    return "functions/functionpage";

   }
}

【问题讨论】:

  • 您展示的所有映射都在同一个 Controller 类中?
  • 不同控制器的功能和欢迎方式
  • @OpenJDK 欢迎页面是:localhost:8080/springcodes
  • @regularslasher 你的应用程序的名称是什么。弹簧代码?
  • 当遇到访问 URL 的问题时,org.springframework 在信息或调试模式下的堆栈跟踪很有用...

标签: java jsp spring-mvc


【解决方案1】:

这里有两个不同的问题。

首先,您拥有一个InternalResourceViewResolver,前缀为/WEB-INF/views/(后缀为.jsp),因此您的回报应该是:

return "functions/functionpage";

不要重复前缀或后缀。

但另一件事闻起来很香。您的 DispatcherServlet 映射到 /,而不是 /*。即使两者看起来都像 catchall,但它们确实是不同的:

  • /* 表示:我想要一切 - 通常你必须设置一个 /&lt;mvc:resource&gt; 标记以允许提供静态资源
  • / 表示:我接受别人不想要的一切。这允许 servlet 容器直接提供 WEB-INF 文件夹之外的所有静态资源和 JSP

因此 servlet 容器应该尝试为自己提供 URL http://localhost:8080/springcodes/functions.jsp,甚至不尝试将其传递给 Spring DispatcherServlet。但是如果你解决了第一个问题,http://localhost:8080/springcodes/functions 应该可以工作。

【讨论】:

  • 第一个问题发生在从控制器返回视图以加载 jsp 时。我认为解决这个问题并不能解决到达控制器的问题。
  • @FranMontero:不会,但如果不修复,它可能会隐藏/functions(没有.jsp)应该工作的事实。
【解决方案2】:

你试过了吗 http://localhost:8080/springcodes/functions?

   @RequestMapping("/functions")
   public String displayFunctionsPage(Model model) {     

     model.addAttribute("funcList", "Function List");
     // returns the view name
     System.out.println("are we inside?");
    return "functions/functionpage.jsp";
   }

编辑—— 由于您没有分享完整的代码,所以不容易猜到,请尝试以下方法-

    @Controller
    @RequestMapping("/functions")
    public class FunctionController {

      @RequestMapping("/page")
      public String displayFunctionsPage(Model model) {     

       model.addAttribute("funcList", "Function List");
       // returns the view name
       System.out.println("are we inside?");
       return "functions/functionpage";
     }
   }

网址 - http://localhost:8080/springcodes/functions/page

【讨论】:

  • springcodes 是您的 Web 应用程序的上下文根?
  • 是的,[springcodes] 是我的网络应用程序的上下文根目录。
【解决方案3】:

尝试点击这个 [http://localhost:8080/springcodes/functions]。 当您尝试访问没有关联映射的 URI 时,因为您的控制器期待 springcodes/functions

【讨论】:

  • 能否在您的问题中附上错误的堆栈跟踪?
  • 而且正如其他人所建议的,您还应该将 return 语句更改为 'return "functions/functionpage";'
  • 2015 年 8 月 5 日下午 7:07:22 org.springframework.web.servlet.DispatcherServlet noHandlerFound 警告:在名为 ' 的 DispatcherServlet 中找不到带有 URI [/springcodes/functions]] 的 HTTP 请求的映射调度员'
  • 您的控制器上是否有“/springcodes”请求映射声明?如果没有,请尝试 [localhost:8080/functions]
【解决方案4】:

@Controller注释你的控制器

还要确保你有

<mvc:annotation-driven/>
<context:component-scan base-package="com.acss.springcodes.controller"/>

指向包含您的控制器的正确包。

【讨论】:

  • 当我尝试添加 时,即使是欢迎页面也会出现 404 错误,这是另一个问题。
【解决方案5】:

控制器的正确映射是:

  @RequestMapping("/functions/**")

一个人必须以艰苦的方式学习这一点。

【讨论】:

    【解决方案6】:

    尝试改变:

    return "WEB-INF/views/functions/functionpage.jsp";
    

    收件人:

    return "functions/functionpage";
    

    【讨论】:

    • 是的,我也相应地改了,还是不行。
    • 没有结尾 .jsp ?如果您的文件与您的welcome.jsp 位于同一目录中,那么如果它适用于welcome.jsp,它应该以这种方式工作。你能发布你的文件夹结构吗?
    • 明确一点:您正在调用此网址:http://localhost:8080/functions 不是吗?
    • WEB-INF ---> 视图 (welcome.jsp) --> 函数 (functionpage.jsp)
    • 那么这意味着您的@Controller-Class 上有一个@RequestMapping("/springcodes"),您的displayFunctionsPage 就在其中?否则 URL 必须是 http://localhost:8080/functions 并且应该这样工作......
    【解决方案7】:

    如果您的应用部署正确并且您的应用名称是springcodes,那么您只需从您的网址中删除.jsp,因为映射只是/functionshttp://localhost:8080/springcodes/functions 还要检查 Controller 类在类级别没有映射。

    【讨论】:

    • 常用映射为:/appName/ClassLevelMapping/MethodLevelMappping
    猜你喜欢
    • 2016-02-05
    • 2012-04-16
    • 1970-01-01
    • 2017-09-17
    • 2016-06-11
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多