Spring MVC调度员无法正常工作。 HTTP 404

时间:2016-10-30 22:18:27

标签: spring jsp spring-mvc http-status-code-404

我见过很多类似的线程,但没有解决我的'问题?'。请告诉我我错过了什么?当我尝试转到http://localhost:8080/helloworld.html时,我会收到“HTTP状态404”。

  

HelloWorldController.java

   @Controller
    public class HelloWorldController {

   @RequestMapping(value="/helloworld.html")
    public ModelAndView helloWorld() {

        return new ModelAndView("HelloWorldPage"); }
     } 
  

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

</web-app>
  

调度-servlet.xml中

   <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"
       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">

    <context:component-scan base-package="com.site.controller"/>

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

    </beans>

文件结构: https://s18.postimg.org/4j699vn6x/stack.png

  

的pom.xml

...
    <dependencies>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.1.0.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.1.0.RELEASE</version>
        </dependency>
    </dependencies>
...

1 个答案:

答案 0 :(得分:0)

这是您应该如何配置应用程序:

在您的web.xml上,您应该将调度程序servlet简化为:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>


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

控制器应如下所示:

@Controller
public class HelloWorldController {

   @RequestMapping(value="/")
   public ModelAndView helloWorld() {

        return new ModelAndView("HelloWorldPage"); }
   }

现在,根据你的形象我猜测你的tomcat上下文是untitled7所以在tomcat启动之后&amp;正在尝试点击下一个网址:

http://localhost:8080/untitled7