【问题标题】:Spring MVC: RequestMapping not working, no URL paths identifiedSpring MVC:RequestMapping 不起作用,没有识别出 URL 路径
【发布时间】:2011-11-27 13:16:24
【问题描述】:

我用谷歌搜索过这个问题,但似乎没有人和我有完全相同的问题。

我正在尝试设置一个简单的 Spring MVC 应用程序。这些是相关文件:

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

...

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

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

    <servlet-mapping>
        <servlet-name>MyApp</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

myapp-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        <mvc:annotation-driven/>
        <context:component-scan base-package="myapp.gui"/>

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

HomeController.java

@Controller
public class HomeController 
{
    @RequestMapping({"/"})
    public String showHomePage(Map<String,Object> model)
    {
        return "home";
    }
}

我在 WEB-INF\views 中有一个 home.jsp。问题是我的应用程序没有返回主页。我刚得到一个 404,虽然 Spring 正在找到我的控制器(日志这样说),但它给出了错误:没有识别 URL 路径。

谁能看出我做错了什么?

保罗

【问题讨论】:

    标签: java spring model-view-controller


    【解决方案1】:

    您可以尝试使用 /app/* 作为 servlet 映射 url 和 /home 作为 RequestMapping。然后尝试使用 /app/home 访问它。映射 /* 存在某些问题 - 一旦您使用 /app 进行映射的其余部分 - 我们可以考虑删除 /app

    【讨论】:

    • 嗨 gkamal,我也遇到了同样的问题。所以在添加应用程序后它正在工作。你能告诉现在如何删除应用程序吗?
    【解决方案2】:

    我曾经想用其他 URL 完成一个 Spring MVC 项目,但我也找不到任何真正有用的资源。我确实在我的博客上发表了一篇文章,但后来删除了该博客。以下是该帖子的摘录:

    我正在使用:

    • NetBeans 6.9.1
    • Spring Framework 3.0.2 发布(包括 JSTL)
    • JAVA JDK 6
    • GlassFish 服务器 3
      • 开始新的 NetBeans 项目
      • 选择 Java Web – Web 应用程序
      • 点击下一步>
      • 输入项目名称“restMVC”并选择项目位置
      • 点击下一步>
      • 选择 GlassFish Server 3 - 选择 Java EE 6 Web
      • 点击下一步>
      • 检查 Spring Web MVC – 单击配置选项卡并将调度程序名称更改为“restMVC”并将调度程序映射更改为“/”
      • 点击完成
    1. 打开restMVC-servlet.xml——我们想让servlet在特定包内的类中搜索@Controller注解,为此我们需要在文件中插入一个context:component-scan,这意味着我们还需要添加 schemeLocation 如下:

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
      
        http://www.springframework.org/schema/beans
      
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
      <context:component-scan base-package="restMVC.web"/>
      ...
      

      当然,我们还没有创建包,所以让我们这样做吧。

      • 右键单击“源包”
      • 转到新建
      • 点击 Java 包
      • 输入包名restMVC.web
    2. 现在我们可以创建我们的第一个控制器了——

      • 右键点击新建的restMVC.web包
      • 转到新建
      • 点击 Java 类
      • 输入类名myController
    3. 接下来添加@Controller注解,然后右键——修复导入:

      package restMVC.web;
      import org.springframework.stereotype.Controller;
      
      @Controller
      public class myController {
      }
      
    4. 现在我们可以添加一些请求映射 URI,记住我们现在有许多使用 @RequestMapping 注解进行映射的不同选项,因此请务必查看 Spring 文档。

      package restMVC.web;
      
      import java.util.ArrayList;
      import java.util.List;
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RequestMapping;
      
      @Controller
      public class myController {
          @RequestMapping("/test")
          public String getTests(Model model) {
              List tests = new ArrayList();
              tests.add("<a href=\"test/1\">test 1</a>");
              tests.add("<a href=\"test/2\">test 2</a>");
              model.addAttribute("tests", tests);
              return "tests";
          }
          @RequestMapping("/test/{testId}")
          public String getTest(@PathVariable String testId, Model model) {
              List tests = new ArrayList();
              tests.add("test " + testId);
              model.addAttribute("tests", tests);
              return "tests";
          }
      }
      
    5. 我们需要为tests.jsp 创建一个视图。这是通过获取从 getTestsgetTest 方法(“测试”)返回的字符串并使用附加 .jsprestMVC-servlet.xml 中的 viewResolver 来创建的 - 更多内容即将到来。

      • 右键单击(项目视图)restMVC -> Web Pages -> WEB-INF -> jsp 文件夹
      • 点击新建
      • 点击 JSP..
      • 输入文件名“tests”
    6. 编辑tests.jsp。在页面顶部输入以下内容:

      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      

      然后在body元素内:

      <table>
        <c:forEach var="test" items="${tests}">
          <tr>
            <td>${test}</td>
          </tr>
        </c:forEach>
      </table>
      
    7. 最后,我们需要确保 viewResolver 在restMVC-servlet.xml 中。完整的文件应如下所示:

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        <context:component-scan base-package="restMVC.web"/>
        <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />
      </beans>
      
    8. 您可能想要编辑redirect.jsp,使其重定向到/test

    9. 现在点击“运行主项目”

    【讨论】:

      猜你喜欢
      • 2012-06-08
      • 2021-03-20
      • 2013-07-06
      • 2018-05-26
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 1970-01-01
      • 2015-05-03
      相关资源
      最近更新 更多