在DispatcherServlet中找不到带有URI [/ HelloWeb /]的HTTP请求的映射,其名称为' HelloWeb'

时间:2016-01-31 19:29:49

标签: java spring jsp servlets

我知道同样的问题之前已经问过,但它没有帮助我,我正在尝试解决这个过去2天我正在使用" spring-framework-4.2.4.RELEASE" 这是我的代码:web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"metadata-complete="true">
<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中:

<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

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

</beans>

HelloController.java

package com.tutorialspoint.controller;


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
public class HelloController {

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

       return "hello";
    }
 }

这是我的hello.jsp。这个jsp的路径是WEB-INF / jsp / hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
  <h2>${message}</h2>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试执行以下操作:

  1. <context:component-scan base-package="com.tutorialspoint.controller"/>添加到HelloWeb-Servlet.xml。
  2. <url-pattern>*.jsp</url-pattern>更改为<url-pattern>/web/*</url-pattern>
  3. 然后希望您能够访问localhost:8080 / web / hello。