Myeclipse通過maven搭建SpringMvc框架(二)
阿新 • • 發佈:2018-12-13
突然想起來大家可能沒有安裝maven,不過這個我就不寫了,百度一下,你就會了,咱們繼續搭建SpringMvc框架。
一、配置web.xml檔案
既然我們是web程式,那麼web.xml檔案是必備的,maven建立的專案不知道為什麼沒有,我們就自己動手豐衣足食吧
自己建個資料夾,然後自己建個web.xml,下面是程式碼,配置前端控制器程式碼裡面會有spring-mvc配置檔案的路徑,大家注意一下下,它會在resource檔案下尋找配置檔案,大家參考一下,配置檔案在下面講,不過建立配置檔案大家就得自己建了
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>prince</display-name> <!--配置前端控制器 --> <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:spring/spring-mvc.xml,classpath:spring/spring-view-controller.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>/</url-pattern> </servlet-mapping> <!-- 過濾器,不是很關鍵 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 似乎是web.xml自帶的 --> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <error-page> <error-code>404</error-code> <location>/page/404.html</location> </error-page> <session-config> <session-timeout>30</session-timeout> </session-config> </web-app>
二、配置檔案內容
spring-mvc.xml
這個配置檔案三個簡單配置即可,不過我原來使用的是複雜的配置,就全部寫上了 ,在這裡大家可以瞭解一下springMvc的流程,當然我也是為了自己記錄一下,大家有興趣的話最好上網找找
- 傳送請求到前端控制器DispatcherServlet
- 前端控制器請求HandlerMapping查詢Handler,可以根據xml配置或註解進行查詢
- 處理器對映器HandlerMapping向前端控制器返回Handler
- 前端控制器呼叫處理器介面卡去執行Handler
- 處理器介面卡執行Handler
- Handler執行完成後向前端控制器返回ModelAndView
- 處理器介面卡向前端控制器返回ModelAndView
- 前端控制器請求檢視解析器去進行檢視解析,根據邏輯檢視名解析成真正的檢視
- 檢視解析器向前端控制器返回View
- 前端控制器進行檢視渲染,檢視渲染將模型資料填充到request域
- 前端控制器向用戶響應結果
<?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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置掃描的包 --> <context:component-scan base-package="controller"/> <!--表示使用註解驅動SpringMvc--> <mvc:annotation-driven/> <!-- 定義跳轉的檔案的前後綴 ,檢視模式配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/page/" /> <property name="contentType" value="text/html;charset=UTF-8" /> </bean> <!--以下配置可以省略,上面是springmvc快速配置,省略了其中handlermapping和handlerAdpter的配置--> <!--由於已經配置上面的context:componet-scan,可以省略此配置,使用annotation 自動註冊bean,並保證@Required,@Autowired的屬性被注入 --> <!-- <context:annotation-config/> --> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> --> <!-- <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html; charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> --> <!--避免 IE 執行 AJAX 時,返回 JSON 出現下載檔案 --> <!-- <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> --> <!-- 啟動 SpringMVC 的註解功能,完成請求和註解 POJO 的對映 --> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter" /> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> --> <!-- SpringMVC上傳檔案時,需要配置MultipartResolver處理器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8" > <property name="maxUploadSize"> <value>10485760</value> </property> </bean> </beans>
spring-view-controller.xml
一般springmvc都是經過Controller,但是當我們不想經過Controller,而是直接訪問檢視的時候。就可以通過mvc:view-controller。
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:view-controller path="/image/news" view-name="redirect:http://localhost/image/news"/>
<mvc:resources location="/image/" mapping="/image/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/page/" mapping="/page/**"/>
<mvc:resources location="/video/" mapping="/video/**"/>
</beans>
三、建立Controller類
四、建立HTML檔案
啟動伺服器,然後到瀏覽器中檢視
這是通過Controller返回的頁面
這是直接訪問的頁面
不行了,我太累了,具體的我也寫不動了,對不起大家。