1. 后臺(tái)單一架構(gòu)介紹
1.1 概念
- 整個(gè)項(xiàng)目都封裝到一個(gè)war包在一個(gè)Tomcat上運(yùn)行,這就是單一架構(gòu)模式赫冬。也叫“單機(jī)版”妇萄、“all in one”。
- 優(yōu)點(diǎn):結(jié)構(gòu)簡(jiǎn)單例证,不涉及模塊之間調(diào)用路呜。易于開(kāi)發(fā)和部署。
- 缺點(diǎn):不能容納規(guī)模龐大织咧、訪問(wèn)量高的項(xiàng)目拣宰。
1.2 工程之間的關(guān)系
1.3 創(chuàng)建Maven工程即Maven Module
1.4 工程創(chuàng)建清單
父工程坐標(biāo)
- groupid:com.rgh.crowd.funding
- artifactid:atcrowdfunding-admin-0-parent
- packaging:pom
WebUI工程
- Module Name:atcrowdfunding-admin-1-webui
- 注意:打包方式選擇war
組件工程
- Module Name:atcrowdfunding-admin-2-component
通用工程
- Module Name:atcrowdfunding-admin-3-common
實(shí)體類(lèi)工程
- Module Name:atcrowdfunding-admin-4-entity
1.5 建立工程之間的依賴(lài)關(guān)系
-
WebUI依賴(lài)Component
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>atcrowdfunding-admin-0-parent</artifactId> <groupId>com.rgh.crowd.funding</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>atcrowdfunding-admin-1-webui</artifactId> <packaging>war</packaging> <dependencies> <dependency> <groupId>com.rgh.crowd.funding</groupId> <artifactId>atcrowdfunding-admin-2-component</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
-
Component依賴(lài)Common
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>atcrowdfunding-admin-0-parent</artifactId> <groupId>com.rgh.crowd.funding</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>atcrowdfunding-admin-2-component</artifactId> <dependencies> <dependency> <groupId>com.rgh.crowd.funding</groupId> <artifactId>atcrowdfunding-admin-3-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
-
Common依賴(lài)Entity
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>atcrowdfunding-admin-0-parent</artifactId> <groupId>com.rgh.crowd.funding</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>atcrowdfunding-admin-3-common</artifactId> <dependencies> <dependency> <groupId>com.rgh.crowd.funding</groupId> <artifactId>atcrowdfunding-admin-4-entity</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
1.6 父工程管理依賴(lài)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rgh.crowd.funding</groupId>
<artifactId>atcrowdfunding-admin-0-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>atcrowdfunding-admin-1-webui</module>
<module>atcrowdfunding-admin-2-component</module>
<module>atcrowdfunding-admin-3-common</module>
<module>atcrowdfunding-admin-4-entity</module>
</modules>
<properties>
<atguigu.spring.version>5.0.2.RELEASE</atguigu.spring.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring依賴(lài) -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${atguigu.spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${atguigu.spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${atguigu.spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<!-- 數(shù)據(jù)庫(kù)依賴(lài) -->
<!-- MySQL驅(qū)動(dòng) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-Java</artifactId>
<version>5.1.3</version>
</dependency>
<!-- 數(shù)據(jù)源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- MyBatis與Spring整合 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- MyBatis分頁(yè)插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.0</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<!-- Spring進(jìn)行JSON數(shù)據(jù)轉(zhuǎn)換依賴(lài) -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
<!-- JSTL標(biāo)簽庫(kù) -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- junit測(cè)試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 引入Servlet容器中相關(guān)依賴(lài) -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- JSP頁(yè)面使用的依賴(lài) -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1.3-b06</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
創(chuàng)建數(shù)據(jù)庫(kù)和數(shù)據(jù)庫(kù)表
創(chuàng)建數(shù)據(jù)庫(kù)
CREATE DATABASE `atcrowdfunding190105` CHARACTER SET utf8
創(chuàng)建管理員數(shù)據(jù)庫(kù)表
drop table if exists t_admin;
create table t_admin
(
id int not null auto_increment,
login_acct varchar(255) not null,
user_pswd char(32) not null,
user_name varchar(255) not null,
email varchar(255) not null,
create_time char(19),
primary key (id)
);
2. 基于Maven的逆向工程
2.1 創(chuàng)建Maven工程
- 建議逆向工程從項(xiàng)目本身獨(dú)立出來(lái)党涕。
- group id:com.rgh.crowd.funding
- artifact id:atcrowdfunding-reverse
- package:jar
2.2 pom配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rgh.crowd.funding</groupId>
<artifactId>atcrowdfunding-reverse</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 依賴(lài)MyBatis核心包 -->
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
</dependencies>
<!-- 控制Maven在構(gòu)建過(guò)程中相關(guān)配置 -->
<build>
<!-- 構(gòu)建過(guò)程中用到的插件 -->
<plugins>
<!-- 具體插件,逆向工程的操作是以構(gòu)建過(guò)程中插件形式出現(xiàn)的 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.0</version>
<!-- 插件的依賴(lài) -->
<dependencies>
<!-- 逆向工程的核心依賴(lài) -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 數(shù)據(jù)庫(kù)連接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2</version>
</dependency>
<!-- MySQL驅(qū)動(dòng) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
2.3 generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="atguiguTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自動(dòng)生成的注釋 true:是;false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--數(shù)據(jù)庫(kù)連接的信息:驅(qū)動(dòng)類(lèi)巡社、連接地址、用戶(hù)名手趣、密碼 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/atcrowdfunding190105"
userId="root"
password="360">
</jdbcConnection>
<!-- 默認(rèn)false晌该,把JDBC DECIMAL 和 NUMERIC 類(lèi)型解析為 Integer,為 true時(shí)把JDBC DECIMAL
和 NUMERIC 類(lèi)型解析為java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成Entity類(lèi)的路徑 -->
<javaModelGenerator targetProject=".\src\main\java"
targetPackage="com.rgh.crowd.funding.entity">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
<!-- 從數(shù)據(jù)庫(kù)返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:XxxMapper.xml映射文件生成的路徑 -->
<sqlMapGenerator targetProject=".\src\main\java"
targetPackage="com.rgh.crowd.funding.mapper">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:Mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetProject=".\src\main\java"
targetPackage="com.rgh.crowd.funding.mapper">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 數(shù)據(jù)庫(kù)表名字和我們的entity類(lèi)對(duì)應(yīng)的映射指定 -->
<table tableName="t_admin" domainObjectName="Admin" />
</context>
</generatorConfiguration>
2.4 執(zhí)行逆向生成操作的Maven命令
2.5 逆向工程生成的資源各歸各位
逆向工程目錄
項(xiàng)目工程目錄
WebUI工程將來(lái)在Tomcat上運(yùn)行時(shí)绿渣,現(xiàn)在resources目錄下的資源會(huì)直接放在WEB-INF/classes目錄(也就是類(lèi)路徑)下朝群,所以放在resources目錄下運(yùn)行的時(shí)候更容易找到。
3. 業(yè)務(wù)邏輯層
3.1 創(chuàng)建包
- com.rgh.crowd.funding.service
- com.rgh.crowd.funding.service.api
- com.rgh.crowd.funding.service.impl
3.2 在Component工程加入依賴(lài)
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<!-- Spring依賴(lài) -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-Java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
</dependency>
4. SSM整合總體思路
5. Spring和MyBatis整合
5.1 創(chuàng)建Spring配置文件
- spring-persist-mybatis.xml
- spring-persist-tx.xml
- spring-web-mvc.xml
5.2 創(chuàng)建jdbc.properties
jdbc.user=root
jdbc.password=360
jdbc.url=jdbc:mysql://localhost:3306/atcrowdfunding190105
jdbc.driver=com.mysql.jdbc.Driver
5.3 創(chuàng)建log4j.properties
log4j.rootLogger=DEBUG,myConsole
log4j.appender.myConsole=org.apache.log4j.ConsoleAppender
log4j.appender.myConsole.Target=System.out
log4j.appender.myConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsole.layout.ConversionPattern=%p [%C{1}:%L] %m%n
5.4 配置數(shù)據(jù)源
所在配置文件spring-persist-mybatis.xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 加載外部屬性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置數(shù)據(jù)源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
</bean>
5.5 測(cè)試類(lèi)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-persist-mybatis.xml","classpath:spring-persist-tx.xml"})
public class CrowdFundingTest {
@Autowired
private DataSource dataSource;
@Test
public void testConnection() throws SQLException {
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
}
所需依賴(lài)webui-pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
5.6 創(chuàng)建MyBatis全局配置文件
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
5.7 配置SqlSessionFactoryBean
所在的配置文件:spring-persist-mybatis.xml
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- MyBatis配置文件所在位置 -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<!-- 配置Mapper配置文件位置 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/>
<!-- 裝配數(shù)據(jù)源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置MyBatis掃描器 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定Mapper接口所在包 -->
<property name="basePackage" value="com.rgh.crowd.funding.mapper"/>
</bean>
5.8 測(cè)試整合效果
CrowdFundingTest.java
@Autowired
private AdminService adminService;
@Test
public void testMybatis() {
List<Admin> adminList = adminService.getAll();
for (Admin admin : adminList) {
System.out.println(admin);
}
}
spring-persist-mybatis.xml中配置掃描服務(wù)層
<!-- 掃描AdminService -->
<context:component-scan base-package="com.rgh.crowd.funding.service.*"/>
6. 配置聲明式事務(wù)
所在配置文件:spring-persist-tx.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 配置事務(wù)管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事務(wù)通知 -->
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
<!-- 配置事務(wù)屬性 -->
<tx:attributes>
<!-- 將查詢(xún)方法設(shè)置為只讀 -->
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="count*" read-only="true"/>
<!-- 給增刪改方法設(shè)置屬性 -->
<!-- propagation:配置傳播行為中符,REQUIRES_NEW表示必須工作在自己新開(kāi)的事務(wù)中姜胖,不受其他事務(wù)回滾影響 -->
<!-- rollback-for:配置回滾的異常,默認(rèn)是根據(jù)運(yùn)行時(shí)異车砩ⅲ回滾右莱,為了拋出編譯時(shí)異常也回滾,設(shè)置為java.lang.Exception -->
<tx:method name="save*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
<tx:method name="remove*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
<tx:method name="update*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<!-- 配置事務(wù)切面 -->
<aop:config>
<!-- 切入點(diǎn)表達(dá)式 -->
<aop:pointcut expression="execution(* *..*Service.*(..))" id="txPointCut"/>
<!-- 將事務(wù)通知和切入點(diǎn)表達(dá)式關(guān)聯(lián)起來(lái) -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
<!-- @Transactional -->
<!-- <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/> -->
</beans>
7. 加入Spring MVC
7.1 配置spring-web-mvc.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 配置自動(dòng)掃描的包 -->
<context:component-scan base-package="com.rgh.crowd.funding.handler"/>
<!-- 配置SpringMVC標(biāo)配:注解驅(qū)動(dòng) -->
<mvc:annotation-driven/>
<!-- 配置視圖解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
7.2 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
version="3.1">
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-persist-*.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-web-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</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>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
7.3 偽靜態(tài)【了解】
DispatcherServlet映射*.html擴(kuò)展名档插,那么所有以html結(jié)尾的請(qǐng)求都由SpringMVC處理慢蜓。反之,請(qǐng)求地址沒(méi)有以html結(jié)尾郭膛,不會(huì)由SpringMVC處理晨抡。
這樣就可以把一個(gè)本來(lái)是動(dòng)態(tài)處理的請(qǐng)求看起來(lái)像是請(qǐng)求了一個(gè)HTML頁(yè)面。
好處1:增加黑客入侵的難度则剃。
好處2:有利于SEO優(yōu)化耘柱。
注意:如果實(shí)質(zhì)上返回的響應(yīng)數(shù)據(jù)是JSON格式,那么Tomcat將認(rèn)為這個(gè)響應(yīng)數(shù)據(jù)和請(qǐng)求擴(kuò)展名“html”不匹配棍现,然后給出415錯(cuò)誤调煎。針對(duì)這種情況,需要另外再映射*.json擴(kuò)展名轴咱。
7.4 創(chuàng)建Controller類(lèi)測(cè)試
@Controller
public class AdminHandler {
@Autowired
private AdminService adminService;
@RequestMapping("/admin/get/all")
public String getAll(Model model){
List<Admin> list = adminService.getAll();
model.addAttribute("list",list);
return "admin-target";
}
}
目標(biāo)頁(yè)面jsp
7.5 在 atcrowdfunding-admin-1-webui的pom.xml加入依賴(lài)
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
admin-target.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${!empty list }">
<c:forEach items="${list }" var="admin">
${admin }<br/>
</c:forEach>
</c:if>
</body>
</html>