我是Spring MVC
的新人。我在NetBeans 8.2
中使用Spring-MVC 4.0.1
创建了一个WebApplication。我使用注释配置和自动扫描。该项目部署良好,但@RequestMapping不映射任何控制器方法。只有一个网址/index
映射到indexController
中的applicationContext.xml
。服务器日志显示控制器已创建,但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.
有人可以帮我解释为什么控制器创建时不会映射URL(自动扫描工作)?
答案 0 :(得分:1)
我认为你应该将它添加到你的上下文文件中以启用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 />
答案 1 :(得分:0)
SpringBoot让它变得更容易,现在你不需要用xml文件来图,你可以用几乎cero配置创建一个spring web项目,看看 Spring-building restful services