web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring/spring-ctx.xml</param-value>
</context-param>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
spring-ctx.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: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.prs.dps"/>
</beans>
spring-mvc
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven enable-matrix-variables="true"/>
<bean
class="org.springframework.web.servlet.view.
InternalResourceViewResolver" p:prefix="/views/" p:suffix=".jsp" />
</beans>
controller
package com.prs.dps;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class Test {
@RequestMapping(value = "/toindex",method = RequestMethod.GET)
public String toIndex(){
return "index";
}
}
There are two containers in this project.
Spring application context
andSpring webapplication context
.Spring application context
和Spring webapplication context
。分别对应两个配置文件
applicationContext.xml
和{servletName}-servlet.xml
。他们之间并不会共享管理的对象。
通过你的配置文件可以看出只有根容器
Spring application
进行了扫描,Spring MVC
的容器(webapplication context
)中没有管理的对象。而
Spring 根容器
(application context
Corresponds to two configuration filesapplicationContext.xml
and{servletName}-servlet.xml
. They will not share managed objects between them.You can see from your configuration file that only the root container
Spring application
has been scanned, and the container ofSpring MVC
(webapplication context
)There are no managed objects
. 🎜TheSpring root container
(application context
)🎜does not have the function to handle mapping and cannot handle request mapping🎜. 🎜 🎜 🎜So the configuration should be like this. 🎜Modify spring-mvc.xml as follows:
Try this instead:
Fallen Angel 008’s solution is right
Additional information·
DispatcherServlet
loads the beans of the Web componentDispatcherServlet
加载Web组件的bean·
ContextLoaderListener
加载中间层和数据层的bean组件补充下
· 建议把spring mvc项目中controller和service组件的包分开
· 建议题主在
spring-mvc.xml
·ContextLoaderListener
loads the bean components of the middle layer and data layer· Suggestions Separate the packages of controller and service components in the spring mvc project
· It is recommended that the subject add a static resource processor tospring-mvc.xml
🎜 🎜If the problem has not been solved, it is recommended to read the spring mvc helloworld example. The example is relatively concise and clear. You can also download the example project🎜