Caused by: java.lang.IllegalArgumentException: Invalid <url-pattern> [url] in servlet mapping
技術標籤:SSMspringmybatisssmspringmvc
使用SSM框架的時候我測試springmvc的時候遇到的錯誤:
一、Server Tomcat v9.0 Server at localhost failed to start.
啟動Tomcat服務的時候報錯資訊:
①Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/testSSM_2020_12_04_Evening_classes]] at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440)
②
Caused by: java.lang.IllegalArgumentException: Invalid <url-pattern> [url] in servlet mapping
錯誤原因: servlet-mapping中的url-pattern的 url 忘記改寫為 /
我的web.xml配置檔案是這樣的:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name> contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>url</url-pattern>
</servlet-mapping>
</web-app>
解決辦法:把錯誤原因改寫為下面的
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
二、當我解決前一個問題,可以正常啟動之後又有這個錯誤了:HTTP Status 404 – 未找到
報錯資訊:[org.springframework.web.context.ContextLoaderListener] org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in class path resource [applicationContext.xml]: Could not resolve placeholder 'jdbc.driver' in value "${jdbc.driver}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbc.driver' in value "${jdbc.driver}"
原因:我寫的資料庫連線的外部檔案db.properties的Driver是這樣的:
jdbc.jdbcDriver=com.mysql.jdbc.Driver
但是我spring配置檔案呼叫的時候是這樣寫的:
<property name="driverClass" value="${jdbc.driver}"></property>
這其中jdbc.Driver和jdbc.driver就不一樣就出錯了。
解決辦法:隨便改一個錯誤:就是要讓外部檔案的前面的值和裡面配置檔案取的值的名稱相同,才能找到
<property name="driverClass" value="${jdbc.jdbcDriver}"></property>
三、spring中mybatis報錯
錯誤報錯資訊:
①Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\eclipse\eclipse_new_new\web_new_new\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\testSSM_2020_12_04_Evening_classes\WEB-INF\classes\mybatis\mapper\UserMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [D:\eclipse\eclipse_new_new\web_new_new\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\testSSM_2020_12_04_Evening_classes\WEB-INF\classes\mybatis\mapper\UserMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Blog'. Cause: java.lang.ClassNotFoundException: Cannot find class: Blog
②
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Blog'. Cause: java.lang.ClassNotFoundException: Cannot find class: Blog
錯誤原因:
這個錯誤是因為我寫mapper.xml檔案(我的是UserMapper.xml)的時候是直接copymybatis3中文文件https://mybatis.org/mybatis-3/zh/getting-started.html中的樣例,然而我寫的專案中又不一樣
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
解決辦法:把那個無效的mapper.xml檔案UserMapper.xml檔案中的select方法註釋掉或者去掉
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.feng.SSMTest.dao.userMapper">
<!-- <select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select> -->
</mapper>
四、把以上三個問題解決了終於執行成功了
SSM框架每一部分都是有聯絡的,哪裡有一個問題都會影響全域性。每一個小問題都是大問題。