Spring MVC显示HTTP状态404

时间:2016-08-03 14:03:59

标签: java spring jsp spring-mvc

这是错误消息: -

enter image description here

这是我的项目结构: -

enter image description here

以下是我的代码: -

HelloController.java

package com.home.pack;

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

@Controller
@RequestMapping("/hello")
public class HelloController{

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

      return "hello";
   }

}

的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>/</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.home.pack" />

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

的hello.jsp

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

我正在关注本教程 - &gt; http://www.tutorialspoint.com/spring/spring_mvc_hello_world_example.htm但我无法让它发挥作用。看来我在这里遗漏了一些东西,如果有人指出这一点,我会非常感激。感谢。

1 个答案:

答案 0 :(得分:-1)

您正试图显示该页面&#34;您好&#34;? Spring可能会将它视为控制器,因为你做了fn foo(r: &[&[usize]]) { } let r = [[0usize; 10]; 10]; foo(&r); //<== error expected type `&[&[usize]]` found type `&[[usize; 10]; 10]` ...所以我猜你在这里尝试将控制器显示为一个页面,这肯定会显示404错误。

尝试将您的方法更改为休闲:

@RequestMapping("/hello")

删除控制器上的请求映射 我想它会更好......

或者让请求映射到您的控制器并更改您的方法的请求映射,并通过添加... / hello / yourMethodValue

来访问它