0

我尝试使用简单的 jsp 加载 spring mvc 的简单示例,但失败并出现以下错误:No mapping found for HTTP request with URI [/HelloWeb/WEB-INF/jsp/hello.jsp] 我正在使用 Spring 3.2 .2. 我真的很感谢你在这里的帮助。我尝试了几件事,但似乎我无法在控制器中映射我的 jsp 页面。

web.xml看起来像这样:

<web-app id="WebApp_ID" version="2.4"
               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">
<display-name>Spring MVC Application</display-name>

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

  <servlet-mapping>
    <servlet-name>HelloWeb</servlet-name>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>
</web-app>

HelloWeb-servlet.xml看起来像这样:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   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="com.tutorial.spring" />

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

HelloControll.class看起来像这样:

package com.tutorial.spring;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/hello")
public class HelloController {
  @RequestMapping(method= RequestMethod.GET)
  public String printHello(ModelMap modelMap) {
    modelMap.addAttribute("message", "Hello Spring MVC Framework!");

    return "hello";
  }
}
4

6 回答 6

1

printHello访问您的处理程序方法的正确 URL是

/HelloWeb/hello.jsp 
于 2013-08-13T08:37:15.720 回答
1

我想到了。愚蠢的错误。我用大写的“H”命名我的jsp“Hello.jsp”,并在我的控制器中返回小写的“hello”。

刚刚将我的 jsp 页面更改为“hello.jsp”。

谢谢你的帮助。

于 2013-08-13T16:49:20.963 回答
0

像这样修改web.xml

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

尝试访问 / YourAppName /hello。然后 Spring-MVC 将触发方法printHello()。然后返回 /WEB-INF/jsp/ 中的 jsp 视图。

于 2013-08-13T08:55:19.907 回答
0

调度 servlet 必须映射正确的主路由:

<servlet-mapping>
    <servlet-name>HelloWeb</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
于 2013-08-13T08:56:16.620 回答
0

正确的 URL 是http://localhost:8080/hello,没有“/HelloWeb”

于 2019-03-05T08:01:35.657 回答
0

将 jsp 文件移动到 servlet 中定义的正确位置

/WEB-INF/jsp/

于 2017-05-16T19:37:13.783 回答