在按照教程的基本配置写完helloworld后,访问时出现了404的问题,弄了好长时间发现了一下几个问题:
1.web.xml中的url-pattern问题,不能写/* ,写之后出现网页无法访问
2.在扫描注解时<context:component-scan base-package="com.test.*.controller">中如果写成这样<context:component-scan base-package="com.*.controller">就会出现不能访问controller的404错误,至于为什么还不清楚,如果有哪位前辈知道的可以指点一下
3.在查找这个问题的时候还发现在同时使用ContextLoaderListener、DispatcherServlet时,应在mvc的配置文件中可以这样
<context:component-scan base-package="com.test.*.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
在applicationContext.xml中
<context:component-scan base-package="com.test.*.service">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
至于第3点的作用,没有验证,但是项目中有看到这样使用