没有找到Spring MVC http 404,有什么想法?

时间:2017-12-14 21:56:46

标签: spring spring-mvc

http://localhost:8080/AnchorMail/可以正常工作并转到index.jsp。

http://localhost:8080/AnchorMail/greeting导致找不到HTTP状态404。

我相信它应该是一个hello JSP页面。我是新手,任何帮助都会非常感激。谢谢。

下面的控制器

   @Controller
    public class HelloController {

        @RequestMapping(value="/greeting")
        public String sayHello(Model model) {

            model.addAttribute("greeting","Hello World");

            return "hello";
        }

}

servlet的config.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        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-3.2.xsd">

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

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

下面的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <servlet>
    <servlet-name>anchorMailServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/servlet-config.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>anchorMailServlet</servlet-name>
     <url-pattern>*.html</url-pattern>
  </servlet-mapping>

  <display-name>Archetype Created Web Application</display-name>

</web-app>

下面的Pom.xml

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>AnchorMail</groupId>
      <artifactId>AnchorMail</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>AnchorMail Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
      </dependencies>
      <build>
        <finalName>AnchorMail</finalName>
      </build>
    </project>

2 个答案:

答案 0 :(得分:0)

请查看此配置元素:

<servlet-mapping>
  <servlet-name>anchorMailServlet</servlet-name>
  <url-pattern>*.html</url-pattern>
</servlet-mapping>

注意您创建的配置。

以* .html结尾的请求将转发到您的spring调度程序servlet。

http://localhost:8080/AnchorMail/greeting未以.html

结尾

另外,请尝试将网址格式更改为/*.html

答案 1 :(得分:0)

@RequestMapping(value="/greeting.html")

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