Spring @RequestMapping 404

Spring @RequestMapping 404

我是 Spring 的新手,遇到 RequestMapping 的问题。我想学习使用注释,所以我尝试创建一个与这些一起使用的小项目。无论如何,我一直收到 404,但据我所知,我做的一切都是对的。如果能得到一些帮助,我会很高兴,在阅读几个基本相同的教程时找不到解决方案。谢谢!

WEB.XML:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>

    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

调度员-servlet.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:p="http://www.springframework.org/schema/p"
    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.controller" />
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>


    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>

            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />


</beans>

包 com.controller 中的控制器 "MainController.java":

package com.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
/**
 *
 * @author 
 */

@Controller
public class MainController{

    @RequestMapping("/main")
    public ModelAndView hithere() {

        ModelAndView mav = new ModelAndView ("main");
        mav.addObject("welcomeMessage", "Hi there");

        return mav;
    }
}

然后Web-inf/jsp文件夹里当然有一个main.jsp

将调度程序 servlet 的 url 映射更改为:

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

或者将您的 RequestMapping(value="/main") 更改为 RequestMapping(value="/main.htm")

哦找到了..

因此 web.xml 中缺少此内容:

<init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>

还有这个

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

已从调度程序移至 applikation.xml。没有注意这一点,因为 netbeans 以这种方式生成它,我相信它:D