使用注释组件扫描标记时,MVC应用程序抛出404错误。使用显式xml bean注入也可以正常工作。请在下面找到相应的代码。
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>AbsSpringMVCProj</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
弹簧调度-servlet.xml中
<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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.happy.hellocontroller.HelloController" />
<!-- <bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> -->
<!-- <context:component-scan base-package = "com.happy.hellocontroller.HelloController" /> -->
<!-- <bean name="/welcome.html" class="com.happy.hellocontroller.HelloController" /> -->
<!-- <bean name="/page2.html" class="com.happy.hellocontroller.HelloController" />
<bean name="/greet/page2.html" class="com.happy.hellocontroller.HelloController" /> -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
HelloController.java
package com.happy.hellocontroller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
/*
* XML based
*
*//*
public class HelloController extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
ModelAndView modelandView = new ModelAndView("HelloPage");
modelandView.addObject("welcomeMsg","Hi, this is coming from HelloController class");
return modelandView;
}
}*/
/*
* Annotation base
*
*
*/
@Controller
public class HelloController {
@RequestMapping("/welcome")
public ModelAndView helloAbs()
{
ModelAndView modelandView = new ModelAndView("HelloPage");
modelandView.addObject("welcomeMsg","Annotation Based, this is coming from HelloController class");
return modelandView;
}
@RequestMapping("/page2")
public ModelAndView helloPage2()
{
ModelAndView modelandView = new ModelAndView("HelloPage");
modelandView.addObject("welcomeMsg","Annotation Based, this is coming from HelloController-helloPage2 class");
return modelandView;
}
@RequestMapping("/page3")
public ModelAndView helloPage3()
{
ModelAndView modelandView = new ModelAndView("HelloPage");
modelandView.addObject("welcomeMsg","Annotation Based, this is coming from HelloController-helloPage33 class");
return modelandView;
}
}
当尝试点击网址“http://localhost:8080/AbsSpringMVCProj/welcome.html”时,它说
HTTP 404,找不到资源。
当 spring-dispatcher-servlet.xml 中的以下块取消注释时,同样正常工作: -
<!-- <bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> -->
<!-- <context:component-scan base-package = "com.happy.hellocontroller.HelloController" /> -->
<!-- <bean name="/welcome.html" class="com.happy.hellocontroller.HelloController" /> -->
<!-- <bean name="/page2.html" class="com.happy.hellocontroller.HelloController" />
<bean name="/greet/page2.html" class="com.happy.hellocontroller.HelloController" /> -->
请帮助我理解:
答案 0 :(得分:0)
您不需要在<context:component-scan ..>
中声明特定的类名。只需要声明包级别。根据您的 spring-dispatcher-servlet.xml ,无需声明 HelloController 类。更改您的组件扫描标签
<context:component-scan base-package = "com.happy.hellocontroller" />