一、介绍
- DispatcherServlet:前端控制器, 是整体流程控制的中心,由其调用其它组件处理用户的请求, 有
效的降低了组件间的耦合性 - HandlerMapping:处理器映射器, 负责根据用户请求找到对应具体的Handler处理器
- Handler:处理器,业务处理的核心类,通常由开发者编写,描述具体的业务
- HandlAdapter:处理器适配器,通过它对处理器进行执行
- View Resolver:视图解析器, 将处理结果生成View视图
- View:视图,最终产出结果, 常用视图如jsp、 html
1、基本配置
<context:component-scan base-package="com.itheima">
<context:include-filter
type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
静态资源加载
<!--放行指定类型静态资源配置方式-->
<mvc:resources mapping="/img/**" location="/img/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<!--SpringMVC提供的通用资源放行方式-->
<mvc:default-servlet-handler/>
中文乱码处理
<!--乱码处理过滤器,与Servlet中使用的完全相同,差异之处在于处理器的类由Spring提供-->
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注解驱动
@Configuration
@ComponentScan(value = "com.itheima",includeFilters =
@ComponentScan.Filter(type=FilterType.ANNOTATION,classes = {Controller.class})
)
public class SpringMVCConfiguration implements WebMvcConfigurer{
//注解配置放行指定资源格式
// @Override
// public void addResourceHandlers(ResourceHandlerRegistry registry) {
// registry.addResourceHandler("/img/**").addResourceLocations("/img/");
// registry.addResourceHandler("/js/**").addResourceLocations("/js/");
// registry.addResourceHandler("/css/**").addResourceLocations("/css/");
// }
//注解配置通用放行资源的格式
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();;
}
}
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
//创建Servlet容器时,使用注解的方式加载SPRINGMVC配置类中的信息,并加载成WEB专用的 //ApplicationContext对象
//该对象放入了ServletContext范围,后期在整个WEB容器中可以随时获取调用
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMVCConfiguration.class);
return ctx;
}
//注解配置映射地址方式,服务于SpringMVC的核心控制器DispatcherServlet
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
//乱码处理作为过滤器,在servlet容器启动时进行配置,相关内容参看Servlet零配置相关课程
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
CharacterEncodingFilter cef = new CharacterEncodingFilter();
cef.setEncoding("UTF-8");
FilterRegistration.Dynamic registration = servletContext.addFilter("characterEncodingFilter", cef);
registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST,DispatcherType.FORWARD,DispatcherType.INCLUDE),false,"/*");
}
}
2、请求
- 普通类型传参
- pojo传参
- 数组与集合类型传参
- 类型转换器
日期类型转换器
<!--5.启用自定义Converter-->
<mvc:annotation-driven conversion-service="conversionService"/>
<!--1.设定格式类型Converter,注册为Bean,受SpringMVC管理-->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!--2.自定义Converter格式类型设定,该设定使用的是同类型覆盖的思想-->
<property name="formatters">
<!--3.使用set保障相同类型的转换器仅保留一个,避免冲突-->
<set>
<!--4.设置具体的格式类型-->
<bean class="org.springframework.format.datetime.DateFormatter">
<!--5.类型规则-->
<property name="pattern" value="yyyy-MM-dd"/>
</bean>
</set>
</property>
</bean>
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;
public String requestParam12(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
System.out.println("date="+date);
return "page.jsp";
}
自定义类型转换器
<!--1.将自定义Converter注册为Bean,受SpringMVC管理-->
<bean id="myDateConverter" class="com.itheima.converter.MyDateConverter"/>
<!--2.设定自定义Converter服务bean-->
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<!--3.注入所有的自定义Converter,该设定使用的是同类型覆盖的思想-->
<property name="converters">
<!--4.set保障同类型转换器仅保留一个,去重规则以Converter<S,T>的泛型为准-->
<set>
<!--5.具体的类型转换器-->
<ref bean="myDateConverter"/>
</set>
</property>
</bean>
//自定义类型转换器,实现Converter接口,接口中指定的泛型即为最终作用的条件
//本例中的泛型填写的是String,Date,最终出现字符串转日期时,该类型转换器生效
public class MyDateConverter implements Converter<String, Date> {
//重写接口的抽象方法,参数由泛型决定
public Date convert(String source) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
//类型转换器无法预计使用过程中出现的异常,因此必须在类型转换器内部捕获,不允许抛出,框架无法预计此类异常如何处理
try {
date = df.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
<!--开启注解驱动,加载自定义格式化转换器对应的类型转换服务-->
<mvc:annotation-driven conversion-service="conversionService"/>
- 请求映射
@requestmapping:作用在类或者方法上面
常用属性
@RequestMapping(
value="/requestURL3", //设定请求路径,与path属性、 value属性相同
method = RequestMethod.GET, //设定请求方式
params = "name", //设定请求参数条件
headers = "content-type=text/*", //设定请求消息头条件
consumes = "text/*", //用于指定可以接收的请求正文类型(MIME类型)
produces = "text/*" //用于指定可以生成的响应正文类型(MIME类型)
)
public String requestURL3() {
return "/page.jsp";
}
3、响应
- 页面跳转
转发
@RequestMapping("/showPage1")
public String showPage1() {
System.out.println("user mvc controller is running ...");
return "forward:page.jsp";
}
重定向
@RequestMapping("/showPage2")
public String showPage2() {
System.out.println("user mvc controller is running ...");
return "redirect:page.jsp";
}
- 带数据页面跳转
使用HttpServletRequest类型形参进行数据传递
@RequestMapping("/showPageAndData1")
public String showPageAndData1(HttpServletRequest request) {
request.setAttribute("name","itheima");
return "page";
}
使用Model类型形参进行数据传递
@RequestMapping("/showPageAndData2")
public String showPageAndData2(Model model) {
model.addAttribute("name","itheima");
Book book = new Book();
book.setName("SpringMVC入门实战");
book.setPrice(66.6d);
model.addAttribute("book",book);
return "page";
}
使用ModelAndView类型形参进行数据传递
//使用ModelAndView形参传递参数,该对象还封装了页面信息
@RequestMapping("/showPageAndData3")
public ModelAndView showPageAndData3(ModelAndView modelAndView) {
//ModelAndView mav = new ModelAndView(); 替换形参中的参数
Book book = new Book();
book.setName("SpringMVC入门案例");
book.setPrice(66.66d);
//添加数据的方式,key对value
modelAndView.addObject("book",book);
//添加数据的方式,key对value
modelAndView.addObject("name","Jockme");
//设置页面的方式,该方法最后一次执行的结果生效
modelAndView.setViewName("page");
//返回值设定成ModelAndView对象
return modelAndView;
}
基于response返回数据的简化格式,返回JSON数据
//使用jackson进行json数据格式转化
@RequestMapping("/showData3")
@ResponseBody
public String showData3() throws JsonProcessingException {
Book book = new Book();
book.setName("SpringMVC入门案例");
book.setPrice(66.66d);
ObjectMapper om = new ObjectMapper();
return om.writeValueAsString(book);
}
使用SpringMVC提供的消息类型转换器将对象与集合数据自动转换为JSON数据
@RequestMapping("/showData4")
@ResponseBody
public Book showData4() {
Book book = new Book();
book.setName("SpringMVC入门案例");
book.setPrice(66.66d);
return book;
}
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
使用SpringMVC注解驱动简化配置
<!--开启springmvc注解驱动,对@ResponseBody的注解进行格式增强,追加其类型转换的功能,具体实现由MappingJackson2HttpMessageConverter进行-->
<mvc:annotation-driven/>
- Servlet相关接口-Servlet相关接口替换方案
pringMVC提供访问原始Servlet接口API的功能,通过形参声明即可
@RequestMapping("/servletApi")
public String servletApi(HttpServletRequest request,
HttpServletResponse response, HttpSession session){
System.out.println(request);
System.out.println(response);
System.out.println(session);
request.setAttribute("name","itheima");
System.out.println(request.getAttribute("name"));
return "page.jsp";
}
Head数据获取
@RequestMapping("/headApi")
public String headApi(@RequestHeader("Accept-Language") String head){
System.out.println(head);
return "page.jsp";
}
Cookie数据获取
@RequestMapping("/cookieApi")
public String cookieApi(@CookieValue("JSESSIONID") String jsessionid){
System.out.println(jsessionid);
return "page.jsp";
}
Session数据获取
@RequestMapping("/sessionApi")
public String sessionApi(@SessionAttribute("name") String name){
System.out.println(name);
return "page.jsp";
}
Session数据设置
@Controller
@SessionAttributes(names={"name"})
public class ServletController {
@RequestMapping("/setSessionData2")
public String setSessionDate2(Model model) {
model.addAttribute("name", "Jock2");
return "page.jsp";
}
}
二、应用
1、异步调用
<a href="javascript:void(0);" id="testAjax">访问controller</a>
<script type="text/javascript" src="/js/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function(){
$("#testAjax").click(function(){ //为id="testAjax"的组件绑定点击事件
$.ajax({ //发送异步调用
type:"POST", //请求方式: POST请求
url:"ajaxController", //请求参数(也就是请求内容)
data:'ajax message', //请求参数(也就是请求内容)
dataType:"text", //响应正文类型
contentType:"application/text", //请求正文的MIME类型
});
});
});
</script>
- 接受异步请求参数 @RequestBody
@RequestMapping("/ajaxController")
public String ajaxController(@RequestBody String message){
System.out.println(message);
return "page.jsp";
}
2、异步请求 跨域访问
跨域访问支持:@CrossOrigin
3、拦截器
拦截器VS过滤器
归属不同: Filter属于Servlet技术, Interceptor属于SpringMVC技术
拦截内容不同: Filter对所有访问进行增强, Interceptor仅针对SpringMVC的访问进行增强
- 实现HandlerInterceptor接口
- 配置拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/showPage"/>
<bean class="com.itheima.interceptor.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
4、异常处理
- 实现HandlerExceptionResolver接口
- @ControllerAdvice @ExceptionHandler(Exception.class)
- 异常处理解决方案
业务异常:
发送对应消息传递给用户,提醒规范操作
系统异常:
发送固定消息传递给用户,安抚用户
发送特定消息给运维人员,提醒维护
记录日志
其他异常:
发送固定消息传递给用户,安抚用户
发送特定消息给编程人员,提醒维护
纳入预期范围内
记录日志
5、使用技术
- 文件上传下载
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
页面表单
<form action="/fileupload" method="post" enctype="multipart/form-data">
上传LOGO: <input type="file" name="file"/><br/>
<input type="submit" value="上传"/>
</form>
SpringMVC配置
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
控制器
@RequestMapping(value = "/fileupload")
public void fileupload(MultipartFile file){
file.transferTo(new File("file.png"));
}
- 文件上传注意事项
文件命名问题, 获取上传文件名,并解析文件名与扩展名
文件名过长问题
文件保存路径
重名问题
@RequestMapping(value = "/fileupload")
//参数中定义MultipartFile参数,用于接收页面提交的type=file类型的表单,要求表单名称与参数名相同
public String fileupload(MultipartFile file,MultipartFile file1,MultipartFile file2, HttpServletRequest request) throws IOException {
System.out.println("file upload is running ..."+file);
// MultipartFile参数中封装了上传的文件的相关信息
// System.out.println(file.getSize());
// System.out.println(file.getBytes().length);
// System.out.println(file.getContentType());
// System.out.println(file.getName());
// System.out.println(file.getOriginalFilename());
// System.out.println(file.isEmpty());
//首先判断是否是空文件,也就是存储空间占用为0的文件
if(!file.isEmpty()){
//如果大小在范围要求内正常处理,否则抛出自定义异常告知用户(未实现)
//获取原始上传的文件名,可以作为当前文件的真实名称保存到数据库中备用
String fileName = file.getOriginalFilename();
//设置保存的路径
String realPath = request.getServletContext().getRealPath("/images");
//保存文件的方法,指定保存的位置和文件名即可,通常文件名使用随机生成策略产生,避免文件名冲突问题
file.transferTo(new File(realPath,file.getOriginalFilename()));
}
//测试一次性上传多个文件
if(!file1.isEmpty()){
String fileName = file1.getOriginalFilename();
//可以根据需要,对不同种类的文件做不同的存储路径的区分,修改对应的保存位置即可
String realPath = request.getServletContext().getRealPath("/images");
file1.transferTo(new File(realPath,file1.getOriginalFilename()));
}
if(!file2.isEmpty()){
String fileName = file2.getOriginalFilename();
String realPath = request.getServletContext().getRealPath("/images");
file2.transferTo(new File(realPath,file2.getOriginalFilename()));
}
return "page.jsp";
}
三、拓展
1、rest风格
优点
隐藏资源的访问行为,通过地址无法得知做的是何种操作
书写简化
方式
GET(查询) http://localhost/user/1 GET
POST(保存) http://localhost/user POST
PUT(更新) http://localhost/user PUT
DELETE(删除) http://localhost/user DELETE
<!--配置拦截器,解析请求中的参数_method,否则无法发起PUT请求与DELETE请求,配合页面表单使用-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<servlet-name>DispatcherServlet</servlet-name>
</filter-mapping>
开启SpringMVC对Restful风格的访问支持过滤器,即可通过页面表单提交PUT与DELETE请求
页面表单使用隐藏域提交请求类型,参数名称固定为_method,必须配合提交类型method=post使用c
<form action="/user/1" method="post">
<input type="hidden" name="_method" value="PUT"/>
<input type="submit"/>
</form>
@RestController
public class UserController {
@RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
public String restDelete(@PathVariable String id){
System.out.println("restful is running ....delete:"+id);
return "success.jsp";
}
}
2、校验框架
- 分类
校验位置:
客户端校验
服务端校验
校验内容与对应方式:
格式校验
客户端:使用Js技术,利用正则表达式校验
服务端:使用校验框架
逻辑校验
客户端:使用ajax发送要校验的数据,在服务端完成逻辑校验,返回校验结果
服务端:接收到完整的请求后,在执行业务操作前,完成逻辑校验
表单校验规则
长度:例如用户名长度,评论字符数量
非法字符:例如用户名组成
数据格式:例如Email格式、 IP地址格式
边界值:例如转账金额上限,年龄上下限
重复性:例如用户名是否重复
- 校验框架hibernate-validator
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.0.Final</version>
</dependency>
开启校验
名称:@Valid 、 @Validated
类型:形参注解
位置:处理器类中的实体类类型的方法形参前方
作用:设定对当前实体类类型参数进行校验
@RequestMapping(value = "/addemployee")
public String addEmployee(@Valid Employee employee) {
System.out.println(employee);
}
设置校验规则
名称:@NotNull
类型:属性注解 等
位置:实体类属性上方
作用:设定当前属性校验规则
public class Employee{
@NotNull(message = "姓名不能为空")
private String name;//员工姓名
}
获取错误信息
@RequestMapping(value = "/addemployee")
public String addEmployee(@Valid Employee employee, Errors errors, Model model){
System.out.println(employee);
if(errors.hasErrors()){
for(FieldError error : errors.getFieldErrors()){
model.addAttribute(error.getField(),error.getDefaultMessage());
}
return "addemployee.jsp";
}
return "success.jsp";
}
规则校验
@NotNull(message = "请输入您的年龄")
@Max(value = 60,message = "年龄最大值不允许超过60岁")
@Min(value = 18,message = "年龄最小值不允许低于18岁")
private Integer age;//员工年龄
嵌套校验
public class Employee {
//实体类中的引用类型通过标注@Valid注解,设定开启当前引用类型字段中的属性参与校验
@Valid
private Address address;
}
分组校验
对不同种类的属性进行分组,在校验时可以指定参与校验的字段所属的组类别
定义组(通用)
为属性设置所属组,可以设置多个
开启组校验
public interface GroupOne {
}
public String addEmployee(@Validated({GroupOne.class}) Employee employee){
}
@NotEmpty(message = "姓名不能为空",groups = {GroupOne.class})
private String name;//员工姓名
四、ssm整合
1、依赖
<?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.xingyu</groupId>
<artifactId>03_springmvc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
</exclusions>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
</dependencies>
<!-- <profiles>
<profile>
<id>env</id>
<properties>
<jdbc.url></jdbc.url>
</properties>
<!–设置默认启动–>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>-->
<build>
<!--<resources>
<resource>
<!–设定配置文件对应的位置目录,支持使用属性动态设定路径–>
<directory>${project.basedir}/src/main/resources</directory>
<!–开启对配置文件的资源加载过滤–>
<filtering>true</filtering>
</resource>
</resources>-->
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、配置文件
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启bean注解扫描-->
<context:component-scan base-package="com.xingyu">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<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.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置sqlsessionfactorybean-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.xingyu.pojo"/>
<property name="dataSource" ref="dataSource"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
<!--配置包扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xingyu.mapper"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<context:component-scan base-package="com.xingyu.controller"/>
<mvc:annotation-driven>
<!--<mvc:message-converters>
<!– 处理响应中文内容乱码 –>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="defaultCharset" value="UTF-8" />
<property name="supportedMediaTypes">
<list>
<value>text/html</value>
<value>application/json</value>
</list>
</property>
</bean>
</mvc:message-converters>-->
</mvc:annotation-driven>
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.xingyu.interceptor.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring.xml</param-value>
</context-param>
<!--启动服务器时,通过监听器加载spring运行环境-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcherServlet</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>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
mysql配置
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db1
jdbc.username=root
jdbc.password=root
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xingyu.mapper.AccountMapper">
<insert id="save" parameterType="account">
insert into account(username,password) values (#{account.username},#{account.password})
</insert>
<update id="update" parameterType="account">
update account set password = #{account.password} where username = #{account.username}
</update>
<delete id="delete" parameterType="account">
delete from account where username = #{account.username}
</delete>
<select id="get" parameterType="string" resultType="account">
select * from account where username = #{username}
</select>
<select id="findAll" resultType="account">
select * from account
</select>
</mapper>
3、模块
mapper
@Mapper
public interface AccountMapper {
public boolean save(Account user);
public boolean update(Account user);
public boolean delete(String username);
public Account get(String username);
public List<Account> findAll();
}
services
@Transactional(readOnly = true)
public interface AccountService {
boolean save(Account user);
boolean update(Account user);
boolean delete(String username);
Account get(String username);
List<Account> findAll(int page,int size);
}
services
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
public boolean save(Account account){
return accountMapper.save(account);
}
public boolean update(Account account){
return accountMapper.update(account);
}
public boolean delete(String account){
return accountMapper.delete(account);
}
public Account get(String uuid){
return accountMapper.get(uuid);
}
public List<Account> findAll(int page, int size){
PageHelper.startPage(1, 2);
List<Account> all = accountMapper.findAll();
return all;
}
}
controller
@RestController
public class TestController {
@Autowired
private AccountService accountService;
@GetMapping(value = "/save",produces = "text/html;charset=utf-8")
public String save(Model model) {
List<Account> accounts = accountService.findAll(1, 2);
model.addAttribute("accounts",accounts);
int i =1/0;
return "success";
}
}
@Component
@ControllerAdvice
public class ExceptionAdvice{
@ExceptionHandler(Exception.class)
@ResponseBody
public HashMap<String, String> doOtherException(Exception ex,
HttpServletRequest request,
HttpServletResponse response){
HashMap<String, String> map = new HashMap<>();
map.put("异常","数字异常");
return map;
}
}
```java
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion");
}
}
4、注解开发
- 用注解替代applicationContext.xml
//扫描组件,排除SpringMVC对应的bean,等同于<context:component-scan />
@ComponentScan(value = "com.itheima",excludeFilters = {
@ComponentScan.Filter(type= FilterType.ANNOTATION,classes = {Controller.class})})
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MyBatisConfig.class})
//等同于<tx:annotation-driven transaction-manager="txManager"/>,导入的默认名称为transactionManager
@EnableTransactionManagement
public class SpringConfig {
//等同于<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
@Bean("transactionManager")
public DataSourceTransactionManager getDataSourceTxManager(@Autowired DataSource dataSource){
DataSourceTransactionManager dtm = new DataSourceTransactionManager();
//等同于<property name="dataSource" ref="dataSource"/>
dtm.setDataSource(dataSource);
return dtm;
}
}
- 用注解替代spring-mvc.xml
@Configuration
@ComponentScan("com.itheima.controller")
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
}
原文地址:https://blog.csdn.net/weixin_45844810/article/details/116196348
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。