java - Spring MVC无法识别Controller导致返回的结果是404?
问题描述
运行环境java8 + idea2016 + spring mvc4.3 + maven3.3 + tomcat8 + ubuntu
报错信息访问localhost:8080/HelloWeb/hello或者localhost:8080/hello都会返回404的状态码:
Web.xml
<web-app version='2.4' xmlns='http://java.sun.com/xml/ns/j2ee' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd'> <display-name>Spring MVC Form Handling</display-name> <servlet><servlet-name>HelloWeb</servlet-name><servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup> </servlet> <servlet-mapping><servlet-name>HelloWeb</servlet-name><url-pattern>/</url-pattern> </servlet-mapping></web-app>
HelloWeb-servlet.xml
<beans xmlns='http://www.springframework.org/schema/beans' xmlns:context='http://www.springframework.org/schema/context' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation=' http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd'> <context:component-scan base-package='org.neo.sml'/> <bean id='internalResourceViewResolver'> <property name='prefix' value='/WEB-INF/jsp/'/> <property name='suffix' value='.jsp'/> </bean></beans>
HelloController
import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloController { @RequestMapping(value='/hello') public String printHello(ModelMap model) {model.addAttribute('message', 'Hello Spring MVC Framework!');return 'hello'; }}
hello.jsp
<%@ page contentType='text/html;charset=UTF-8' language='java' %><html><head> <title>Hello World</title></head><body><h2>${message}</h2></body></html>补充
我猜测原因是spring mvc没有识别@Controller注解,但是我在HelloWeb-servlet.xml中已经声明了<context:component-scan base-package='org.neo.sml'/>,请问发生错误的原因可能是什么?
按照@傅易君的提示,参考so上面,我加入了<mvc:annotation-driven />,但是还是不行:
<beans xmlns='http://www.springframework.org/schema/beans' xmlns:context='http://www.springframework.org/schema/context' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:mvc='http://www.springframework.org/schema/mvc' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd'> <context:component-scan base-package='org.ziwenxie.sml'/> <mvc:annotation-driven/> <bean id='internalResourceViewResolver'><property name='prefix' value='/WEB-INF/jsp/'/><property name='suffix' value='.jsp'/> </bean></beans>
问题解答
回答1:把HelloWeb-servlet.xml放到resources里面,web.xml里面改为<servlet>
<servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:HelloWeb-servlet.xml</param-value> </init-param> </servlet>
配置HelloWeb-servlet.xml中声明了<context:component-scan base-package='org.neo.sml'/>访问路径写http://localhost:8080/sml/hello就可以了
回答2:controller打个断电,看看是没进controller吗?
回答3:试试 localhost:8080/sml
回答4:先开启注解支持:
<mvc:annotation-driven />回答5:
主要有以下几个方面需要注意:1.在web.xml文件中是否配置xml文件的位置,保证HelloWeb-servlet.xml在src目录下,配置如下:
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:HelloWeb-servlet.xml</param-value> </init-param> </servlet>
2.在HelloWeb-servlet.xml文件中是否配置,如下所示:
<mvc:annotation-driven></mvc:annotation-driven> <context:component-scan base-package='cn'></context:component-scan> <bean class='org.springframework.web.servlet.view.InternalResourceViewResolver'><property name='prefix' value='/WEB-INF/jsps/'></property><property name='suffix' value='.jsp'></property> </bean>
3.项目启动的目录是否正确,是否需要添加项目名称;4.启动之后,目录是否正确;
回答6:访问localhost:8080/HelloWeb/hello或者localhost:8080/hello都会返回404的状态码
这个路径我想你没输对吧?为什么是HelloWeb而不是sml?一般不是项目的名称么,默认是。或者,你找到tomcat的server.xml,找到如下配置。
<Context docBase='test' path='/test' reloadable='true' source='org.eclipse.jst.jee.server:test'/>
相关文章:
1. angular.js - 不适用其他构建工具,怎么搭建angular1项目2. python如何不改动文件的情况下修改文件的 修改日期3. mysql - 一个表和多个表是多对多的关系,该怎么设计4. javascript - git clone 下来的项目 想在本地运行 npm run install 报错5. mysql主从 - 请教下mysql 主动-被动模式的双主配置 和 主从配置在应用上有什么区别?6. android-studio - Android 动态壁纸LayoutParams问题7. 主从备份 - 跪求mysql 高可用主从方案8. angular.js - 三大框架react、vue、angular的分析9. python 如何实现PHP替换图片 链接10. python - django 里自定义的 login 方法,如何使用 login_required()
