今天搭建一个SSM框架打算做一个微信小程序的后台管理系统,然而吐血的问题来了,平时搭建的时候都是轻轻松松搞定的事情,今天因为前台报404,后台不报错,连controller方法都没进去,网上查阅了大量的资料,问题仍然没有得到解决。以下是我对路径错误的几个排除过程
第一步:查看springmvc扫描路径是否正确,注解驱动是否配置
<!-- 可以访问静态资源 -->
<mvc:default-servlet-handler />
<!-- 解析@RequestMapping -->
<mvc:annotation-driven />
<!-- 设置使用注解的类所在的包 -->
<context:component-scan base-package="com.ht.weichat.controller" />
从上面可以看的出来,配置没问题,一般会出现 com.ht.weichat.controller.*这种配置是错误的
第二步:查看视图解析器
<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
也没毛病,
第三步:检查web.xml配置
<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:config/spring_mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
这里常见的错误配置有
1:spring_mvc.xml是放在WEB-INF目录下的话,value是直接写成classpath:spring_mvc.xml就能正常读取,如果是像我那样整理到一个包config里面,如上图所示也是没问题的
2:url-pattern写成/*也是错误的写法,正确的是写/即可,当然有很多类似*.htm/*.action/*.do这种也是没问题的
第四步:也是我们最容易出错的一个地方,确实是路径写错了,这时候我们可以检查下发布的路径,controller的映射路径是否存在问题,我这里因为项目命名比较长,就直接改成方便好看的。更改方式如下图(新手参考,大佬忽略)
假如我要访问如下图所示接口
那路径就是http://localhost:8080/weixin/user/main.do
最后一步:clean下项目,看下哪里报错,我用的是eclipse,clean的方式为Project---->Clean选中你要清理的项目
然而以上这些都排除没有问题,但前台访问还是报404,无法进入后台,那么问题究竟出在什么地方,以下是我解决这个问题的方式
这是我的错误解决方式,错误发生前如下图所示
错误发生前:
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd ">
spring_mvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd">
解决方式:对比发现,两边都有MVC,然后我去掉application.xml 头部的mvc,问题解决。
具体原因没弄清楚,刚开始写这种博客,写的不咋地,纯粹只想分享这让我头痛了3个多小时的问题,如有不正确的地方,欢迎大家指正。