HTTP状态404 - 在Spring MVC控制器中

时间:2016-06-15 04:09:49

标签: spring jsp spring-mvc

我是Spring框架的新手。我正在处理一个但收到以下错误:

 HTTP Status 404 - /AMController/index.jsp
 ---------------------------
 type Status report
 message /AMController/index.jsp
 description The requested resource is not available.
-------------------------- 
Apache Tomcat/6.0.44

控制器类

@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

        <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.4">
      <display-name>Spring MVC Application</display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
        <servlet-name>AMController</servlet-name>
        <servlet-class>
                 org.springframework.web.servlet.DispatcherServlet
              </servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/AMController-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>AMController</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>

AMController-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"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   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
   http://www.springframework.org/schema/mvc 
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>

   <context:component-scan base-package="com.example.*" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean> 
   <!-- 
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean> -->

</beans>

/WEB-INF/jsp/hello.jsp

<html>
   <head>
   <title>Hello Spring MVC</title>
   </head>
   <body>
   <h2>${message}</h2>
   </body>
</html>

可能是我遗漏了一些我无法弄清楚的事情。 请帮我解决这个问题。

最好的问候

1 个答案:

答案 0 :(得分:0)

HTTP状态404。 没找到!

首先,检查设置和路径。

(servlet.xml中)

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

/ web-inf / jsp /(前缀)+“文件名”+ .jsp(后缀

此文件夹结构。

添加int xml

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

(控制器)

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

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

您在浏览器中输入了一个网址。

localhost:主机/上下文路径,web app项目名称/ hello / print / hello