路由访问还是404 not found问题...
来源:2-10 验证Controller
qq_Sakuragi10_0
2019-04-04
用的IDEA,之前写测试类的时候就踩了坑,还好坑比较小跳出来了,这次这个真的看了好多同学提问和老师的解答都de不出来…
跟着代码敲的,然而loaclhost:8080/o2o/superadmin/listarea发生404了…然后把o2o去掉以后,就是直接访问localhost:8080/superadmin/listarea出现下面信息:
{"success":false,"errMsg":"org.mybatis.spring.MyBatisSystemException:
nested exception is org.apache.ibatis.exceptions.
PersistenceException: \r\n### Error querying database.
Cause: org.springframework.jdbc.CannotGetJdbcConnectionException:
Could not get JDBC Connection; nested exception is java.sql.SQLException:
Connections could not be acquired from the underlying database!\r\n###
The error may exist in file [R:\\mooc\\o2o\\target\\o2o\\WEB-INF\\classes\\mapper\\AreaDao.xml]\r\n###
The error may involve dto.AreaDao.queryArea\r\n###
The error occurred while executing a query\r\n### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException:
Could not get JDBC Connection; nested exception is java.sql.SQLException:
Connections could not be acquired from the underlying database!"}
我记得之前调试测试类的时候有一个警告是说jdbc的驱动没有导入成功,不知道有没有影响…
下面附上代码,辛苦老师解答了…
ps. 有些base-package和老师的不太一样是因为我idea的目录结构导致的,写的时候能通过idea的自动匹配功能匹配出选项来…
Controller
@Controller
@RequestMapping("/superadmin")
public class AreaController {
@Autowired
private AreaService areaService;
@RequestMapping(value="/listarea", method = RequestMethod.GET)
@ResponseBody
private Map<String, Object> listArea() {
Map<String, Object> modelMap = new HashMap<String, Object>();
List<Area> list = new ArrayList<Area>();
try {
list = areaService.getAreaList();
modelMap.put("rows", list);
modelMap.put("total", list.size());
} catch (Exception e) {
e.printStackTrace();
modelMap.put("success", false);
modelMap.put("errMsg", e.toString());
}
return modelMap;
}
}
web-xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
jdbc.properties
jdbc.driver=com.mysql.Driver
jdbc.url=jdbc:mysql://localhost:3306/o2o?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=SlamDunk1011
spring-web.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 扩充了注解驱动,可以将参数绑定到控制器参数 -->
<mvc:annotation-driven/>
<!-- 静态资源的处理,css,js,img... -->
<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:default-servlet-handler />
<!-- ViewResolver:视图解析器。可以配置多个 但是一定要将这个ViewResolver(InternalResourceViewResolver)
放到最后 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/html/" />
<property name="suffix" value=".html" />
</bean>
<context:component-scan base-package="web" />
</beans>
写回答
1回答
-
同学好,在同学还没有解决这类问题的能力之前,建议同学还是使用目前市面仍然主流的5.x,这里报的错可能是因为使用mysql8.x导致驱动不兼容所致,此外,去掉o2o前缀去访问有反应就是因为idea里面没设置context所致
https://zhidao.baidu.com/question/1691948905619549988.html
012019-04-05
相似问题