环境总体目标
创建工程
项目结构图
工程创建计划
只有web是war包
reverse是逆向工程
util是工具模块
建立工程的依赖关系
看项目结构图完成项目依赖
创建数据库和数据库表
1、物理建模
1、第一范式:原子性,每一column独立不可再分
2、第二范式:所有非主键字段完全依赖主键字段
3、第三范式:所有非主键字段不完全依赖非主键字段
2、创建数据库
/* Navicat Premium Data Transfer Source Server : root Source Server Type : MySQL Source Server Version : 80016 Source Host : localhost:3306 Source Schema : campusfunding Target Server Type : MySQL Target Server Version : 80016 File Encoding : 65001 Date: 18/07/2020 20:52:37 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for inner_admin_role -- ---------------------------- DROP TABLE IF EXISTS `inner_admin_role`; CREATE TABLE `inner_admin_role` ( `id` int(11) NOT NULL AUTO_INCREMENT, `admin_id` int(11) DEFAULT NULL, `role_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for inner_role_auth -- ---------------------------- DROP TABLE IF EXISTS `inner_role_auth`; CREATE TABLE `inner_role_auth` ( `id` int(11) NOT NULL AUTO_INCREMENT, `role_id` int(11) DEFAULT NULL, `auth_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_address -- ---------------------------- DROP TABLE IF EXISTS `t_address`; CREATE TABLE `t_address` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', `receive_name` char(100) DEFAULT NULL COMMENT '收件人', `phone_num` char(100) DEFAULT NULL COMMENT '手机号', `address` char(200) DEFAULT NULL COMMENT '收货地址', `member_id` int(11) DEFAULT NULL COMMENT '用户 id', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_admin -- ---------------------------- DROP TABLE IF EXISTS `t_admin`; CREATE TABLE `t_admin` ( `id` int(11) NOT NULL AUTO_INCREMENT, `login_acct` varchar(255) NOT NULL, `user_pswd` char(100) NOT NULL, `user_name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `create_time` char(19) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `login_acct` (`login_acct`) ) ENGINE=InnoDB AUTO_INCREMENT=1136 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_auth -- ---------------------------- DROP TABLE IF EXISTS `t_auth`; CREATE TABLE `t_auth` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(200) DEFAULT NULL, `title` varchar(200) DEFAULT NULL, `category_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_member -- ---------------------------- DROP TABLE IF EXISTS `t_member`; CREATE TABLE `t_member` ( `id` int(11) NOT NULL AUTO_INCREMENT, `login_acct` varchar(255) NOT NULL, `user_pswd` char(200) NOT NULL, `user_name` varchar(255) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, `auth_status` int(4) DEFAULT NULL COMMENT '实名认证状态0-未实名认证,1-实名认证申请中,2-已实名认证', `user_type` int(4) DEFAULT NULL COMMENT '0-个人,1-企业', `real_name` varchar(255) DEFAULT NULL, `card_num` varchar(255) DEFAULT NULL, `acct_type` int(4) DEFAULT NULL COMMENT '0-企业,1-个体,2-个人,3-政府', PRIMARY KEY (`id`), UNIQUE KEY `login_acct` (`login_acct`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_member_confirm_info -- ---------------------------- DROP TABLE IF EXISTS `t_member_confirm_info`; CREATE TABLE `t_member_confirm_info` ( `id` int(11) NOT NULL AUTO_INCREMENT, `memberid` int(11) DEFAULT NULL COMMENT '会员 id', `paynum` varchar(200) DEFAULT NULL COMMENT '易付宝企业账号', `cardnum` varchar(200) DEFAULT NULL COMMENT '法人身份证号', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_member_copy1 -- ---------------------------- DROP TABLE IF EXISTS `t_member_copy1`; CREATE TABLE `t_member_copy1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `login_acct` varchar(255) NOT NULL, `user_pswd` char(200) NOT NULL, `user_name` varchar(255) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, `auth_status` int(4) DEFAULT NULL COMMENT '实名认证状态0-未实名认证,1-实名认证申请中,2-已实名认证', `user_type` int(4) DEFAULT NULL COMMENT '0-个人,1-企业', `real_name` varchar(255) DEFAULT NULL, `card_num` varchar(255) DEFAULT NULL, `acct_type` int(4) DEFAULT NULL COMMENT '0-企业,1-个体,2-个人,3-政府', PRIMARY KEY (`id`), UNIQUE KEY `login_acct` (`login_acct`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_member_launch_info -- ---------------------------- DROP TABLE IF EXISTS `t_member_launch_info`; CREATE TABLE `t_member_launch_info` ( `id` int(11) NOT NULL AUTO_INCREMENT, `memberid` int(11) DEFAULT NULL COMMENT '会员 id', `description_simple` varchar(255) DEFAULT NULL COMMENT '简单介绍', `description_detail` varchar(255) DEFAULT NULL COMMENT '详细介绍', `phone_num` varchar(255) DEFAULT NULL COMMENT '联系电话', `service_num` varchar(255) DEFAULT NULL COMMENT '客服电话', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_menu -- ---------------------------- DROP TABLE IF EXISTS `t_menu`; CREATE TABLE `t_menu` ( `id` int(11) NOT NULL AUTO_INCREMENT, `pid` int(11) DEFAULT NULL, `name` varchar(200) DEFAULT NULL, `url` varchar(200) DEFAULT NULL, `icon` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_order -- ---------------------------- DROP TABLE IF EXISTS `t_order`; CREATE TABLE `t_order` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', `order_num` char(100) DEFAULT NULL COMMENT '订单号', `pay_order_num` char(100) DEFAULT NULL COMMENT '支付宝流水号', `order_amount` double(10,5) DEFAULT NULL COMMENT '订单金额', `invoice` int(11) DEFAULT NULL COMMENT '是否开发票(0 不开,1 开)', `invoice_title` char(100) DEFAULT NULL COMMENT '发票抬头', `order_remark` char(100) DEFAULT NULL COMMENT '订单备注', `address_id` char(100) DEFAULT NULL COMMENT '收货地址 id', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_order_project -- ---------------------------- DROP TABLE IF EXISTS `t_order_project`; CREATE TABLE `t_order_project` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', `project_name` char(200) DEFAULT NULL COMMENT '项目名称', `launch_name` char(100) DEFAULT NULL COMMENT '发起人', `return_content` char(200) DEFAULT NULL COMMENT '回报内容', `return_count` int(11) DEFAULT NULL COMMENT '回报数量', `support_price` int(11) DEFAULT NULL COMMENT '支持单价', `freight` int(11) DEFAULT NULL COMMENT '配送费用', `order_id` int(11) DEFAULT NULL COMMENT '订单表的主键', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_project -- ---------------------------- DROP TABLE IF EXISTS `t_project`; CREATE TABLE `t_project` ( `id` int(11) NOT NULL AUTO_INCREMENT, `project_name` varchar(255) DEFAULT NULL COMMENT '项目名称', `project_description` varchar(255) DEFAULT NULL COMMENT '项目描述', `money` bigint(11) DEFAULT NULL COMMENT '筹集金额', `day` int(11) DEFAULT NULL COMMENT '筹集天数', `status` int(4) DEFAULT NULL COMMENT '0-即将开始,1-众筹中,2-众筹成功,3-众筹失败 ', `deploydate` varchar(10) DEFAULT NULL COMMENT '项目发起时间', `supportmoney` bigint(11) DEFAULT NULL COMMENT '已筹集到的金额', `supporter` int(11) DEFAULT NULL COMMENT '支持人数', `completion` int(3) DEFAULT NULL COMMENT '百分比完成度', `memberid` int(11) DEFAULT NULL COMMENT '发起人的会员 id', `createdate` varchar(19) DEFAULT NULL COMMENT '项目创建时间', `follower` int(11) DEFAULT NULL COMMENT '关注人数', `header_picture_path` varchar(255) DEFAULT NULL COMMENT '头图路径', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_project_item_pic -- ---------------------------- DROP TABLE IF EXISTS `t_project_item_pic`; CREATE TABLE `t_project_item_pic` ( `id` int(11) NOT NULL AUTO_INCREMENT, `projectid` int(11) DEFAULT NULL, `item_pic_path` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_project_tag -- ---------------------------- DROP TABLE IF EXISTS `t_project_tag`; CREATE TABLE `t_project_tag` ( `id` int(11) NOT NULL AUTO_INCREMENT, `projectid` int(11) DEFAULT NULL, `tagid` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=94 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_project_type -- ---------------------------- DROP TABLE IF EXISTS `t_project_type`; CREATE TABLE `t_project_type` ( `id` int(11) NOT NULL AUTO_INCREMENT, `projectid` int(11) DEFAULT NULL, `typeid` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=77 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_return -- ---------------------------- DROP TABLE IF EXISTS `t_return`; CREATE TABLE `t_return` ( `id` int(11) NOT NULL AUTO_INCREMENT, `projectid` int(11) DEFAULT NULL, `type` int(4) DEFAULT NULL COMMENT '0 - 实物回报, 1 虚拟物品回报', `supportmoney` int(11) DEFAULT NULL COMMENT '支持金额', `content` varchar(255) DEFAULT NULL COMMENT '回报内容', `count` int(11) DEFAULT NULL COMMENT '回报产品限额,“0”为不限回报数量', `signalpurchase` int(11) DEFAULT NULL COMMENT '是否设置单笔限购', `purchase` int(11) DEFAULT NULL COMMENT '具体限购数量', `freight` int(11) DEFAULT NULL COMMENT '运费,“0”为包邮', `invoice` int(4) DEFAULT NULL COMMENT '0 - 不开发票, 1 - 开发票', `returndate` int(11) DEFAULT NULL COMMENT '项目结束后多少天向支持者发送回报', `describ_pic_path` varchar(255) DEFAULT NULL COMMENT '说明图片路径', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_role -- ---------------------------- DROP TABLE IF EXISTS `t_role`; CREATE TABLE `t_role` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(100) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_tag -- ---------------------------- DROP TABLE IF EXISTS `t_tag`; CREATE TABLE `t_tag` ( `id` int(11) NOT NULL AUTO_INCREMENT, `pid` int(11) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_type -- ---------------------------- DROP TABLE IF EXISTS `t_type`; CREATE TABLE `t_type` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL COMMENT '分类名称', `remark` varchar(255) DEFAULT NULL COMMENT '分类介绍', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; SET FOREIGN_KEY_CHECKS = 1;
父依赖管理
<properties> <!-- 声明属性,对 Spring 的版本进行统一管理 --> <spring.version>5.0.2.RELEASE</spring.version> <!-- 声明属性,对 SpringSecurity 的版本进行统一管理 --> <spring.security.version>4.2.10.RELEASE</spring.security.version> <mysql.version>8.0.16</mysql.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <!--使用父工程管理项目依赖的版本--> <spring.version>5.0.2.RELEASE</spring.version> <spring.security.version>5.0.1.RELEASE</spring.security.version> </properties> <!--父项目管理项目依赖--> <dependencyManagement> <dependencies> <!--加入spring security--> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-taglibs</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${spring.security.version}</version> </dependency> <!--spring security 标签库--> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-taglibs</artifactId> <version>${spring.security.version}</version> </dependency> <!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!--aspectj--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.5</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <!--mysql 驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <!--数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <!--mybatis整合spring--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.4</version> </dependency> <!--mybatis 分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.4</version> </dependency> <!--日志框架--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.30</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <!--其他日志框架的转换包--> <dependency> <groupId>org.slf4j</groupId> <artifactId>jul-to-slf4j</artifactId> <version>1.7.30</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.25</version> </dependency> <!--spring进行json数据转换--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.10.2</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency> </dependencies> </dependencyManagement>
Spring整合Mybatis(配置+注解)
目标
adminMapper通过IOC容器装配到当前组建后,就可以直接调用,方便快捷.
@Service public class AdminServiceImpl implements AdminService { @Autowired private AdminMapper adminMapper; public Admin getAdminById(Integer adminId){ return adminMapper.selectByPrimaryKey(adminId); } }
思路
配置步骤
- 在子工程(admin-web)导入需要的依赖
- 配置spring整合mybatis
- 准备jdbc.properties
- 创建spring.xml
- 配置数据源
- 配置SqlSessionFactoryBean
- 装配数据源
- 指定xxxMapper.xml配置文件的位置
- 指定Mybatis全局配置文件
- 配置MapperScannerConfiguter
- 测试
<?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.xsd "> <!--1开启注解的扫描,希望处理service和dao,controller不需要Spring框架去处理--> <context:component-scan base-package="com.adom"> <!--配置哪些注解不扫描--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <context:property-placeholder location="classpath:spring/jdbc.properties"/> <!--2Spring整合MyBatis框架--> <!--配置连接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--配置SqlSessionFactory工厂--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--装配数据源--> <property name="dataSource" ref="dataSource"/> <!--指定mybatis全局配置--> <property name="configLocation" value="classpath:spring/mybatis-config.xml"/> <!--指定Mapper配置文件位置--> <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/> </bean> <!-- 3配置 MapperScannerConfigurer --> <!-- 把 MyBatis 创建的 Mapper 接口类型的代理对象扫描到 IOC 容器中 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 使用 basePackage 属性指定 Mapper 接口所在包 --> <property name="basePackage" value="com.adom.mapper"/> </bean> <!--<!–3配置Spring框架声明式事务管理–>--> <!--<!–配置事务管理器–>--> <!--<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">--> <!--<property name="dataSource" ref="dataSource" />--> <!--</bean>--> <!--<!–配置事务通知–>--> <!--<tx:advice id="txAdvice" transaction-manager="transactionManager">--> <!--<tx:attributes>--> <!--<tx:method name="find*" read-only="true"/>--> <!--<tx:method name="*" isolation="DEFAULT"/>--> <!--</tx:attributes>--> <!--</tx:advice>--> <!--<!–配置AOP增强–>--> <!--<aop:config>--> <!--<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.adom.service.impl.*ServiceImpl.*(..))"/>--> <!--</aop:config>--> </beans>
小结
日志系统
声明式事务
表述层工作机制
访问过程
表述层环境搭建
导入依赖
使用springmvc环境引入spring-mvc依赖即可
web.xml配置
1、contextLoaderListener
作用:加载spring的配置文件,根据Spring的配置文件初始化IOC容器
2、characterEncodingFilter
解决post请求的字符乱码问题
3、HiddenHttpMethodFilter
遵循restful风格将post请求转换为put请求、delete请求使用
4、DispatcherSerlet
前端控制器
代码
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--配置contextLoaderListener--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mybatis.xml</param-value> </context-param> <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> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置 SpringMVC 的前端控制器 --> <!-- 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> <!-- 以初始化参数的形式指定 SpringMVC 配置文件的位置 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <!-- 让 DispatcherServlet 在 Web 应用启动时创建对象、初始化 --> <!-- 默认情况:Servlet 在第一次请求的时候创建对象、初始化 --> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>*.html</url-pattern> <url-pattern>*.json</url-pattern> </servlet-mapping>
<!-- Map all requests to the DispatcherServlet for handling --> <!-- DispatcherServlet 映射的 URL 地址 --> <!-- 大白话:什么样的访问地址会交给 SpringMVC 来处理 --> <!-- 配置方式一:符合 RESTFUL 风格使用“/” --> <!-- <url-pattern>/</url-pattern> --> <!-- 配置方式二:请求扩展名 -->
</web-app>
请求扩展名
让请求扩展名和预计的响应体数据格式一致
Springmvc.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置创建 spring 容器要扫描的包 --> <context:component-scan base-package="com.adom.controller"/> <!-- 配置视图解析器 --> <!-- 拼接公式→前缀+逻辑视图+后缀=物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀:附加到逻辑视图名称前 --> <property name="prefix" value="/WEB-INF/pages/"/> <!-- 后缀:附加到逻辑视图名称后 --> <property name="suffix" value=".jsp"/> </bean> <mvc:resources mapping="/static/**" location="/static/"/> <!--配置不拦截页面--> <mvc:view-controller path="/admin/to/login/page.html" view-name="admin-login"/> <!-- 启用注解驱动 --> <mvc:annotation-driven/> <!--异常映射--> <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.Exception">system-error</prop> </props> </property> </bean> </beans>
页面上的base标签
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <base href="http://${pageContext.request.serverName }:${pageContext.request.serverPort }${pageContext.request.contextPath }/> </head> <body> </body> </html>
springmvc环境下的ajax请求
前端页面
静态资源引入
由于SpringMVC会默认拦截所有请求,因此需要在SpringMVC配置资源不拦截或者在web.xml配置如下:
<servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <!-- DispatcherServlet 映射的 URL 地址 --> <!-- 大白话:什么样的访问地址会交给 SpringMVC 来处理 --> <!-- 配置方式一:符合 RESTFUL 风格使用“/” --> <!-- <url-pattern>/</url-pattern> --> <!-- 配置方式二:请求扩展名 --> <url-pattern>*.html</url-pattern> <url-pattern>*.json</url-pattern> </servlet-mapping>
初步显示管理员登陆页面
配置view-controller,这样就能够直接跳转到某个页面.
<mvc:view-controller path="/admin/to/login/page.html" view-name="admin-login"/>
异常映射
以常量管理属性名和异常消息
作用:以防打字错误
public class ConstantUtil { public static final String MESSAGE_STRING_INVALIDATE = "密码不能为空"; public static final String MESSAGE_LOGIN_ERROR = "账号错误"; public static final String MESSAGE_PASSWORD_ERROR = "密码错误"; public static final String ATTR_NAME_LOGIN_ADMIN = "loginAdmin"; public static final String MESSAGE_ACCESS_FORBIDEN = "请先登录,再访问"; public static final String ATTR_NAME_PAGEINFO = "pageInfo"; }