SSM-網站後臺管理系統制作(2)
阿新 • • 發佈:2019-01-05
SSM基本工作原理
講解網站:https://www.w3cschool.cn/wkspring/dcu91icn.html
構建基本工作環境:
mysql
eclipse(tomcat8.0)
Hbulider(前端頁面展示)
構建Dynamic Web Project,然後寫基本所需的domain,dao,service,到此,基本功能即可實行,然後加入db.properties連結資料庫,(applicationContext.xml,springmvc-config.xml,web.xml)就構建好了一個基本的ssm框架了。然後在Controller層裡面加入所需要的程式碼即可,到此,一個基本的ssm就可以跑起來了,當然,這是簡單講解,3個xml裡面還有很多需要學習的地方,相關問題見程式碼
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p"View Code6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:mvc="http://www.springframework.org/schema/mvc" 8 xmlns:tx="http://www.springframework.org/schema/tx" 9 xsi:schemaLocation="http://www.springframework.org/schema/beans 10 http://www.springframework.org/schema/beans/spring-beans.xsd11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context.xsd 13 http://www.springframework.org/schema/mvc 14 http://www.springframework.org/schema/mvc/spring-mvc.xsd 15 http://www.springframework.org/schema/tx 16 http://www.springframework.org/schema/tx/spring-tx.xsd 17 http://mybatis.org/schema/mybatis-spring 18 http://mybatis.org/schema/mybatis-spring.xsd "> 19 20 <!-- mybatis:scan會掃描org.fkit.dao包裡的所有介面當作Spring的bean配置,之後可以進行依賴注入--> 21 <mybatis:scan base-package="org.fkit.hrm.dao"/> 22 23 <!-- 掃描org.fkit包下面的java檔案,有Spring的相關注解的類,則把這些類註冊為Spring的bean --> 24 <context:component-scan base-package="org.fkit.hrm"/> 25 26 <!-- 使用PropertyOverrideConfigurer後處理器載入資料來源引數 --> 27 <context:property-override location="classpath:db.properties"/> 28 29 <!-- 配置c3p0資料來源 --> 30 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"/> 31 32 <!-- 配置SqlSessionFactory,org.mybatis.spring.SqlSessionFactoryBean是Mybatis社群開發用於整合Spring的bean --> 33 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" 34 p:dataSource-ref="dataSource"/> 35 36 <!-- JDBC事務管理器 --> 37 <bean id="transactionManager" 38 class="org.springframework.jdbc.datasource.DataSourceTransactionManager" 39 p:dataSource-ref="dataSource"/> 40 41 <!-- 啟用支援annotation註解方式事務管理 --> 42 <tx:annotation-driven transaction-manager="transactionManager"/> 43 44 </beans>
springmvc-config.xml
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:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context.xsd"> 14 15 <!-- 自動掃描該包,SpringMVC會將包下用了@controller註解的類註冊為Spring的controller --> 16 <context:component-scan base-package="org.fkit.hrm.controller"/> 17 <!-- 設定預設配置方案 --> 18 <mvc:annotation-driven/> 19 <!-- 使用預設的Servlet來響應靜態檔案 --> 20 <mvc:default-servlet-handler/> 21 22 <!-- 定義Spring MVC的攔截器 --> 23 <mvc:interceptors> 24 <mvc:interceptor> 25 <!-- 攔截所有請求 --> 26 <mvc:mapping path="/*"/> 27 <!-- 自定義判斷使用者許可權的攔截類 --> 28 <bean class="org.fkit.hrm.interceptor.AuthorizedInterceptor"/> 29 </mvc:interceptor> 30 </mvc:interceptors> 31 32 <!-- 檢視解析器 --> 33 <bean id="viewResolver" 34 class="org.springframework.web.servlet.view.InternalResourceViewResolver" 35 p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/> 36 37 <!-- 檔案上傳下載 --> 38 <bean id="multipartResolver" 39 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 40 <!-- 上傳檔案大小上限,單位為位元組(10MB) --> 41 <property name="maxUploadSize"> 42 <value>10485760</value> 43 </property> 44 <!-- 請求的編碼格式,必須和jSP的pageEncoding屬性一致,以便正確讀取表單的內容,預設為ISO-8859-1 --> 45 <property name="defaultEncoding"> 46 <value>UTF-8</value> 47 </property> 48 </bean> 49 50 </beans>View Code
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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_3_1.xsd" id="WebApp_ID" version="3.1"> 3 <!-- 配置spring核心監聽器,預設會以 /WEB-INF/applicationContext.xml作為配置檔案 --> 4 <listener> 5 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 6 </listener> 7 <!-- contextConfigLocation引數用來指定Spring的配置檔案 --> 8 <context-param> 9 <param-name>contextConfigLocation</param-name> 10 <param-value>/WEB-INF/applicationContext*.xml</param-value> 11 </context-param> 12 13 <!-- 定義Spring MVC的前端控制器 --> 14 <servlet> 15 <servlet-name>springmvc</servlet-name> 16 <servlet-class> 17 org.springframework.web.servlet.DispatcherServlet 18 </servlet-class> 19 <init-param> 20 <param-name>contextConfigLocation</param-name> 21 <param-value>/WEB-INF/springmvc-config.xml</param-value> 22 </init-param> 23 <load-on-startup>1</load-on-startup> 24 </servlet> 25 26 <!-- 讓Spring MVC的前端控制器攔截所有請求 --> 27 <servlet-mapping> 28 <servlet-name>springmvc</servlet-name> 29 <url-pattern>/</url-pattern> 30 </servlet-mapping> 31 32 <!-- 編碼過濾器 --> 33 <filter> 34 <filter-name>characterEncodingFilter</filter-name> 35 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 36 <init-param> 37 <param-name>encoding</param-name> 38 <param-value>UTF-8</param-value> 39 </init-param> 40 </filter> 41 <filter-mapping> 42 <filter-name>characterEncodingFilter</filter-name> 43 <url-pattern>/*</url-pattern> 44 </filter-mapping> 45 46 <!-- jsp的配置 --> 47 <jsp-config> 48 <jsp-property-group> 49 <!-- 配置攔截所有的jsp頁面 --> 50 <url-pattern>*.jsp</url-pattern> 51 <!-- 可以使用el表示式 --> 52 <el-ignored>false</el-ignored> 53 <!-- 不能在頁面使用java指令碼 --> 54 <scripting-invalid>true</scripting-invalid> 55 <!-- 給所有的jsp頁面匯入要依賴的庫,tablib.jsp就是一個全域性的標籤庫檔案 --> 56 <include-prelude>/WEB-INF/jsp/taglib.jsp</include-prelude> 57 </jsp-property-group> 58 </jsp-config> 59 60 <error-page> 61 <error-code>404</error-code> 62 <location>/404.html</location> 63 </error-page> 64 65 <welcome-file-list> 66 <welcome-file>index.jsp</welcome-file> 67 </welcome-file-list> 68 </web-app>View Code