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" 6 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/context12 http://www.springframework.org/schema/context/spring-context.xsd13 http://www.springframework.org/schema/mvc14 http://www.springframework.org/schema/mvc/spring-mvc.xsd15 http://www.springframework.org/schema/tx16 http://www.springframework.org/schema/tx/spring-tx.xsd17 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>
View Code
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.xsd10 http://www.springframework.org/schema/mvc11 http://www.springframework.org/schema/mvc/spring-mvc.xsd 12 http://www.springframework.org/schema/context13 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.DispatcherServlet18 </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