java - @RequestMapping 不映射 URL

标签 java spring-mvc

我是 Spring MVC 的新手。我使用 Spring-MVC 4.0.1NetBeans 8.2 中创建了一个 WebApplication。我使用注释配置和自动扫描。该项目部署正常,但 @RequestMapping 未映射任何 Controller 方法。在 applicationContext.xml 中只有一个 URL /index 映射到 indexController。服务器日志显示 Controller 已创建,但 URL 未映射。

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>

applicationContext.xml:

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.company.product"/>
    <!--bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.url}"
    p:username="${jdbc.username}"
    p:password="${jdbc.password}" /-->

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->

</beans>

dispatcherServlet.xml:

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<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" />

MyController.java:

package com.company.product.controller;


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

@Controller
public class MyController
{
  public MyController()
  {
    System.out.println( "#### MyController created" );
  }

  @RequestMapping( value = "/index", method = RequestMethod.GET )
  public String indexHandler_GET( ModelMap model_ )
  {
    System.out.println( "####indexHandler_GET called" );
    return "index";
  }
}

服务器日志:

Info:   Registering WebSocket filter for url pattern /*
Info:   WebModule[/WebApplication2] ServletContext.log():No Spring WebApplicationInitializer types detected on classpath
Info:   Initializing Mojarra 2.3.2 ( 20170627-2139 e63598abf2ed2bb1a24674f308a734e0dce18a72) for context '/WebApplication2'
Info:   WebModule[/WebApplication2] ServletContext.log():Initializing Spring root WebApplicationContext
Info:   Root WebApplicationContext: initialization started
Info:   Refreshing Root WebApplicationContext: startup date [Fri May 25 23:23:04 CEST 2018]; root of context hierarchy
Info:   Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
Info:   JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Info:   #### MyController created
Info:   Root WebApplicationContext: initialization completed in 448 ms
Info:   WebModule[/WebApplication2] ServletContext.log():Initializing Spring FrameworkServlet 'dispatcher'
Info:   FrameworkServlet 'dispatcher': initialization started
Info:   Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri May 25 23:23:04 CEST 2018]; parent: Root WebApplicationContext
Info:   Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
Info:   Mapped URL path [/index.htm] onto handler 'indexController'
Info:   FrameworkServlet 'dispatcher': initialization completed in 137 ms
Info:   Loading application [WebApplication2] at [/WebApplication2]
Info:   WebApplication2 was successfully deployed in 1,146 milliseconds.

任何人都可以帮助我,为什么它在创建 Controller 时不映射 URL(自动扫描工作)?

最佳答案

我认为您应该将其添加到上下文文件中以启用 mvc。

xmlns:mvc="http://www.springframework.org/schema/mvc"

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

<mvc:annotation-driven />

关于java - @RequestMapping 不映射 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50537143/

相关文章:

Java从一个类到另一个类访问信息

spring-mvc - 我们可以在 Spring MVC 的 Controller 中获取 HttpRequest 对象吗

spring - 在springboot中,css和js文件未加载到jsp文件中

java - Spring 4 TestNG +@Autowired

java - HTTP 状态 404 – 源服务器未找到目标资源的当前表示或不愿意透露该资源的存在

java - 将数字数组从 Java 发送到 Arduino

java - 为什么生成的 key 总是不同,即使生成 key 的字符串在 appengine 中相同?

java - JSP 尝试将 String 转换为 Long

java - 我的带有内置 ssl 证书的库如何也允许使用默认证书

Java 8 使用枚举的方法