出現404報錯信息:html
1.我出現的錯誤是配置沒問題,找了一圈發現本身單詞拼錯了,致使運行不了,出現404java
2.配置問題:web
jar包:首先導入spring 的jar包15個,log4j兩個,junit兩個。(我是直接在電腦上建了一個文件夾,用的時候直接複製粘貼,不用每次來回倒)spring
配置文件:log4j.xml:mvc
<?xml version="1.0" encoding="UTF-8"?> <!-- status=debug 能夠查看log4j的裝配過程 --> <configuration status="off" monitorInterval="1800"> <appenders> <!-- 定義控制檯輸出 --> <Console name="Console" target="SYSTEM_OUT" follow="true"> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" /> </Console> <RollingRandomAccessFile name="System" fileName="logs/mvchain/system.log" filePattern="logs/mvchain/system_%d{yyyy-MM-dd}_%i.log"> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" /> <Policies> <!-- 多長時間生成一個文件;默認1天 --> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <!-- 多大的文件要切分 --> <SizeBasedTriggeringPolicy size="5K" /> </Policies> </RollingRandomAccessFile> <!-- 外部打印日誌 --> <RollingRandomAccessFile name="Outer" fileName="logs/mvchain/outer.log" filePattern="logs/mvchain/outer_%d{yyyy-MM-dd}_%i.log"> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" /> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <SizeBasedTriggeringPolicy size="100M" /> </Policies> </RollingRandomAccessFile> <!-- 線程打印日誌 --> <RollingRandomAccessFile name="Timer" fileName="logs/mvchain/timer.log" filePattern="logs/mvchain/timer_%d{yyyy-MM-dd}_%i.log"> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" /> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <SizeBasedTriggeringPolicy size="100M" /> </Policies> </RollingRandomAccessFile> </appenders> <loggers> <!-- Root Logger(這個是整個配置文件的入口 --> <root level="trace"> <!-- 調用定義的log4j配置 console:指的是名字;(在這個標籤中定義configuration==>appenders這下面標籤的name屬性 ) --> <appender-ref ref="Console" /> <appender-ref ref="System" /> </root> <!-- 外部日誌,分開日誌,name爲getLogger() console:指的是名字;(在這個標籤中定義configuration==>appenders這下面標籤的name屬性 ) 下面定義的多個組合,logger標籤中任意組合 --> <logger name="SystemLog" level="info" additivity="false"> <appender-ref ref="Console" /> <appender-ref ref="System" /> </logger> <!-- 外部日誌,分開日誌,name爲getLogger() --> <logger name="OuterLog" level="info" additivity="false"> <!-- 日誌文件要輸出到三個地方,控制檯,系統文件,外部文件 --> <appender-ref ref="Console" /> <appender-ref ref="System" /> <appender-ref ref="Outer" /> </logger> <!-- 線程日誌,分開日誌,name爲getLogger() --> <logger name="TimerLog" level="info" additivity="false"> <!-- 日誌文件要輸出到三個地方,控制檯,系統文件,外部文件 --> <appender-ref ref="Console" /> <appender-ref ref="System" /> <appender-ref ref="Outer" /> </logger> </loggers> </configuration>
web.xml:app
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 5 id="WebApp_ID" version="4.0"> 6 <display-name>01_spring_mvc2</display-name> 7 <!-- 配置開始 --> 8 <servlet> 9 <servlet-name>DispatcherServlet</servlet-name> 10 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 11 <!-- 一塊兒動就初始化此servlet,數字小的先加載,大的後加載 --> 12 <init-param> 13 <param-name>contextConfigLocation</param-name> 14 <param-value>classpath*:applicationContext_*.xml</param-value> 15 </init-param> 16 17 <load-on-startup> 1</load-on-startup> 18 </servlet> 19 20 <servlet-mapping> 21 <servlet-name>DispatcherServlet</servlet-name> 22 <!-- 全部url啓動要以htm結尾纔會訪問此servlet --> 23 <url-pattern>*.htm</url-pattern> 24 25 </servlet-mapping> 26 <!-- 配置結束 --> 27 28 <welcome-file-list> 29 <welcome-file>index.html</welcome-file> 30 <welcome-file>index.htm</welcome-file> 31 <welcome-file>index.jsp</welcome-file> 32 <welcome-file>default.html</welcome-file> 33 <welcome-file>default.htm</welcome-file> 34 <welcome-file>default.jsp</welcome-file> 35 </welcome-file-list> 36 </web-app>
index.jspdom
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>首頁</title> 8 </head> 9 <body> 10 <!-- 項目命 --> 11 ${pageContext.request.contextPath } 12 <table border="1"style="width:80%"> 13 <tr> 14 15 <td>名字</td> 16 <td>鏈接</td> 17 </tr> 18 19 <tr> 20 21 <td>xml</td> 22 <td> 23 <a href="${pageContext.request.contextPath}/xmlController.htm" target="_blank">xml</a> 24 </td> 25 </tr> 26 <tr> 27 28 <td>anno</td> 29 <td><a href="${pageContext.request.contextPath}/test1.htm" target="_blank">anno</a></td> 30 </tr> 31 32 <tr> 33 34 <td>anno2</td> 35 <td><a href="${pageContext.request.contextPath}/test2.htm" target="_blank">anno2</a></td> 36 </tr> 37 38 39 </table> 40 41 </body> 42 </html>
applicationContext_common.xml:jsp
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 https://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 https://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/aop 11 https://www.springframework.org/schema/aop/spring-aop.xsd"> 12 <context:component-scan 13 base-package="conttroller"></context:component-scan> 14 15 <bean id="xmlController" name="/xmlController.htm" 16 class="conttroller.XmlController"> 17 </bean> 18 19 20 </beans>
另外還須要一個Java文件和jsp就不粘貼ide