spring MVC配置檔案解讀
阿新 • • 發佈:2019-01-06
本人spring新手,初玩是spring的感覺就是坑爹的配置檔案,為此寫此文為正在苦苦掙扎於spring配置檔案的同學簡單的解釋下配置檔案。
歡迎拍磚。
先來看web.xml配置檔案:
下面我們在來看看applicationContext.xml配置檔案:<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 宣告spring的核心類(其實是一個servlet,所以也逃不了要宣告),同時此處載入spring的配置檔案 --> <servlet> <servlet-name>dipatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> </servlet> <!-- 對那種請求進行轉發 --> <servlet-mapping> <servlet-name>dipatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 定義編碼過濾器,對中文等進行重新編碼,去除該過濾器則中文亂碼 --> <filter> <filter-name>Set Character Encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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-2.0.xsd"> <!-- 表示層的宣告:主要關於表示層字首路徑(如下:/view/)和字尾路徑(如下:.jsp). 這麼宣告代表你的表示層檔案都在/view/下,而且都是.jsp檔案 --> <!-- efinition of View Resolver --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.JstlView</value> </property> <property name="prefix"> <value>/view/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <!--此處用於宣告action,在此下方要一一列出action的配置 --> <!-- Request Mapping --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/regAction.do">regAction</prop> <prop key="/loginAction.do">loginAction</prop> </props> </property> </bean> <!-- Action Definition --> <bean id="regAction" class="org.lee.springmvc.demo.RegAction"> <property name="commandClass"> <value>org.lee.springmvc.demo.RegInfo</value> </property> <property name="error_view"> <value>error</value> </property> <property name="success_view"> <value>success</value> </property> </bean> <bean id="loginAction" class="org.lee.springmvc.demo.LoginAction"> <!-- 此類用於提取自動提取表單並填充該類(因此,沒有get、set是會報錯的),這是非常方便的 --> <property name="commandClass"> <value>org.lee.springmvc.demo.RegInfo</value> </property> <property name="error_view"> <value>error</value> </property> <property name="success_view"> <value>loginSuccess</value> </property> </bean> </beans>
至此,簡單解釋了配置檔案。另附一個spring mvc的demo ,有簡單的註冊和登入功能(請用Myeclipse開啟)。