前言:
眼前大家对“woff2apache”大约比较着重,姐妹们都想要分析一些“woff2apache”的相关文章。那么小编也在网摘上收集了一些关于“woff2apache””的相关资讯,希望咱们能喜欢,你们一起来学习一下吧!什么是SSM框架?
ssm是基于Spring、Spring MVC以及MyBatis3个开源框架整合的系统架构,遵循标准的MVC设计模式,是web项目常用的技术架构。
Spring
个人理解,spring在ssm整套架构主要承担的是管理的角色。其核心是IoC(控制反转)容器,IoC容器使用工厂模式(BeanFactory)维护着对象的创建、依赖关系以及生命周期等。简单点说,IoC容器帮助开发者省去了new操作,直接可以通过依赖注入(DI)的方式调用对象,而不用管对象是怎么产生的。
此外,Spring提供了一种“横切”技术——面向切面编程(AOP),其底层采用的是动态代理模式(Dynamic Proxy),通过预编译方式和运行期动态代理实现功能的统一维护。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
Spring MVC
Spring MVC其实是Spring大家庭中的一员,采用的经典的MVC设计模式。这里单独拎出来说,只要是Spring MVC在ssm框架中负责的是整个业务流程的生命周期。如何理解这一点呢?下面套用下Spring MVC的工作流程:
1)用户向服务器发送请求,请求被Spring前端控制Servelt DispatcherServlet捕获;
2)DispatcherServlet对请求URL进行解析,得到请求资源标识符(URI)。然后根据该URI,调用HandlerMapping获得该Handler配置的所有相关的对象(包括Handler对象以及Handler对象对应的拦截器),最后以HandlerExecutionChain对象的形式返回;
3)DispatcherServlet 根据获得的Handler,选择一个合适的HandlerAdapter。(附注:如果成功获得HandlerAdapter后,此时将开始执行拦截器的preHandler(…)方法)
4)提取Request中的模型数据,填充Handler入参,开始执行Handler(Controller)。 在填充Handler的入参过程中,根据你的配置,Spring将帮你做一些额外的工作:
HttpMessageConveter: 将请求消息(如Json、xml等数据)转换成一个对象,将对象转换为指定的响应信息;
数据转换:对请求消息进行数据转换。如String转换成Integer、Double等;
数据根式化:对请求消息进行数据格式化。 如将字符串转换成格式化数字或格式化日期等;
数据验证: 验证数据的有效性(长度、格式等),验证结果存储到BindingResult或Error中。
5)Handler执行完成后,向DispatcherServlet 返回一个ModelAndView对象;
6)根据返回的ModelAndView,选择一个适合的ViewResolver(必须是已经注册到Spring容器中的ViewResolver)返回给DispatcherServlet ;
7)ViewResolver 结合Model和View,来渲染视图;
8)将渲染结果返回给客户端。
MyBatis
Mybatis是个半持久层框架,主要负责与数据库进行数据交互。Mybaits更加专注于SQL本身,着力于POJO 与SQL之间的映射关系。然后通过映射配置文件,将SQL所需的参数(输入映射),以及返回的结果字段映射到指定POJO(输出映射),是一种“SQL Mapping“的ORM实现。
相较Hibernate来说,虽然Mybatis映射的能力弱点,但是入门门槛也较低,主要是Mybatis够灵活,个人推荐。
瞎bb了这么多,下面就上手操作吧。上一文《初识maven》简单的介绍了一下maven,现在我们将基于maven将ssm框架搭建起来。开动~
先将项目测试的编译输出目录修改下,怕编译结果混在一起了,操作如下:
项目右键》Build Path》configure Build Path,修改src/test/java和src/test/resource两个编译输出目录为target/test-classess。
修改项目的版本配置,将web配置版本调整为2.5,java为1.8。但是会出现下面这种更改不了的情况
这时需要将Dynamic Web Module去除后保存,然后重新勾选版本
并修改路径为src/main/webapp。同时,将web.xml内容修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<web-app xmlns:xsi=""
xmlns=""
xsi:schemaLocation=" "
id="WebApp_ID" version="2.5">
</web-app>
1234567
接着完善下项目的目录结构(仅代表个人的习惯)
然后在pom.xml中配置相关的引用。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!—jackjson,支持接口返回输出json格式 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.1</version>
</dependency>
<!--打印日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
在src/main/resources目录下添加项目所需的配置信息和配置文件,然后修改web.xml配置文件。
1.数据库信息(database.properties)
dataSource.username= ${数据库账号}
dataSource.password= ${数据库密码}
dataSource.databaseName= ${数据库名称}
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.dialect=org.hibernate.dialect.MySQL5Dialect
dataSource.serverName=
dataSource.url=jdbc:mysql://${dataSource.serverName}/${dataSource.databaseName}?useUnicode=true&characterEncoding=UTF-8&useOldAliasMetadataBehavior=true&useSSL=false
dataSource.properties=user=${dataSource.username};databaseName=${dataSource.databaseName};serverName=${dataSource.serverName};password=${dataSource.password}
12345678
2.日志配置(log4j.properties)
log4j.rootLogger=Console,File
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c]%m%n
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=../log/error.log
log4j.appender.File.MaxFileSize=10MB
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
1234567891011
3.spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:p=""
xmlns:aop=""
xmlns:context=""
xmlns:mvc=""
xmlns:tx=""
xmlns:xsi=""
xsi:schemaLocation="
">
<!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scan base-package="com.maven4web.controller" />
<context:annotation-config />
<!-- 解决@ResponseBody中文乱码 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 启动切面注入代理 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/view/" p:suffix=".jsp" />
123456789101112131415161718192021222324252627282930313233343536
4.spring-content.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:aop=""
xmlns:context=""
xmlns:p=""
xmlns:tx=""
xmlns:mvc=""
xmlns:xsi=""
xmlns:task=""
xsi:schemaLocation="
">
<!-- 扫描包 -->
<context:component-scan base-package="com.maven4web" />
<context:annotation-config />
<mvc:annotation-driven />
<!-- 数据源 -->
<context:property-placeholder location="classpath*:/database.properties"
ignore-unresolvable="true" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${dataSource.driverClassName}" />
<property name="url" value="${dataSource.url}" />
<property name="username" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
</bean>
<!-- 会话工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:com/maven4web/mapper/impl/*.xml"></property>
</bean>
<!-- 事务管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 事务注解驱动,注解@Transactional的类和方法将具有事务性 -->
<tx:annotation-driven transaction-manager="txManager" />
<!-- spring和mybatis的整合 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.maven4web.mapper"></property>
</bean>
</beans>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
5.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<web-app xmlns:xsi=""
xmlns="" xmlns:web=""
xsi:schemaLocation=" "
id="WebApp_ID" version="2.5">
<display-name>maven4web</display-name>
<!-- 工程编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<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>
<!-- log4j配置 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<!-- Spring容器加载所有的配置文件的路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 配置SpringMVC核心控制器 -->
<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-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<!--为DispatcherServlet建立映射 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.eot</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.svg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ttf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.woff</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.woff2</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.txt</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/index.jshtml</url-pattern>
</servlet-mapping>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
<!-- 起始页 -->
<welcome-file-list>
<welcome-file>index.jshtml</welcome-file>
</welcome-file-list>
</web-app>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
配置完这些,下面实现个简单的功能来验证这个框架是否可用。
首先,在数据库执行下面的SQL创建一张名为t_test的表。
CREATE TABLE `t_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
123456
然后,在项目model下创建一个名为Test的实体。
src/main/java/model/TestMapper.java
public class Test {
private Integer id;
private String username;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
12345678910111213141516
完后。在mapper添加对Test这个是数据库相关操作的接口声明,同时在同级目录下新建一个impl的package,放入该接口的实现。
src/main/java/mapper/TestMapper.java
public interface TestMapper {
Integer insert(@Param("username") String username);
List<Test> getAll();
}
1234
src/main/java/mapper/impl/TestMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "">
<mapper namespace="com.maven4web.mapper.TestMapper">
<select id="getAll" resultType="com.maven4web.model.Test">
select
t.id,
t.username
from
t_test t
</select>
<insert id="insert" parameterType="String">
insert into t_test(
username
)
values(
#{username}
)
</insert>
</mapper>
12345678910111213141516171819
然后在controller下新建一个TestController。
src/main/java/controller/TestController.java
@Controller
public class TestController {
@Autowired
private TestService testService;
@RequestMapping("/test.action")
@ResponseBody
public List<?> test(
@RequestParam(value="username", required=true) String username
){
testService.insert(username);
List<Test> tests = testService.getAll();
return tests;
}
}
1234567891011121314
再在service新建业务功能TestService接口,同时在同级目录下新建个impl,放入其实现类。
src/main/java/service/TestService.java
public interface TestService {
void insert(String username);
List<Test> getAll();
}
1234
src/main/java/service/impl/TestServiceImpl.java
@Service
@Transactional
public class TestServiceImpl implements TestService {
@Autowired
private TestMapper testMapper;
@Override
public void insert(String username) {
testMapper.insert(username);
}
@Override
public List<Test> getAll() {
return testMapper.getAll();
}
}
1234567891011121314
到此整合和实例基本完成,可以把项目跑起来看看效果了。验证方式在浏览器输入,看浏览器是否返回:
[{"id":1,"username":"hello"}]
标签: #woff2apache