没有调用Spring MVC控制器

时间:2018-05-23 21:41:37

标签: java spring servlets model-view-controller controller

项目结构如下图所示: project structure

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">
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>/mvc</url-pattern>
  </servlet-mapping>

  <context-param>
    <param-name>contextClass</param-name>
    <param-value>
      org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
  </context-param>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/webapp.WEB-INF/mvc-servlet.xml</param-value>
  </context-param>
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
</web-app>

mvc-servlet上下文:

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

    <context:component-scan base-package="main.java.controller"/>
    <mvc:annotation-driven />

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

</beans>

控制器:

package main.java.controller;

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

@Controller
public class HomePageController {

    @RequestMapping(value="/home", method= RequestMethod.GET)
    public String hello(){
        return "homepage";
    }

}

名为“home”的jsp只是一些随机的HTML。

但是,当我尝试访问网址时

  

http://localhost:8080/CatalogFacultate/mvc/home

我收到以下错误: enter image description here

我使用了调试器,甚至没有调用控制器中的方法。因此弹簧配置存在问题。我在互联网上搜索了很多,但我没有想法。我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:0)

在web.xml文件中的org.springframework.web.servlet.DispatcherServlet的url模式中将/ mvc更改为/ mvc / * 这将使所有带有pattern / mvc / *的uri被路由到spring调度程序,然后被路由到你的servlet