SpringMVC配置文件.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.bookstore" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!--视图解析-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

<!-- 使用thymeleaf解析 -->
<bean id="templateResolver"
class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/"/>
<property name="suffix" value=".html"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="order" value="1"/>
<property name="templateMode" value="HTML5"/>
<property name="cacheable" value="false"/>
</bean>

<bean id="templateEngine"
class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
</bean>

<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>

<!--设置静态资源解析使用DefaultServlet处理,否则使用SpringDispatcherServlet解析的是根目录,也就是没有对应RequestMapping无法访问!-->
<mvc:default-servlet-handler/>
<!--开启注解驱动,否则使用View-Controller(视图控制器)时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:annotation-driven 否则会造成所有的@Controller注解无法解析,导致404错误-->
<mvc:view-controller path="/" view-name="index"/>
<!--配置文件解析器-->
<!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>-->
</beans>
阅读全文 »

SpringMVC注解配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.bookstore.config;

import com.bookstore.interceptor.FirstInterceptor;
import org.apache.logging.log4j.core.tools.picocli.CommandLine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import java.util.List;
import java.util.Properties;

/**
* @Author BaiYZ
* @Program BookStoreFrameWork
* @Description SpringMVC配置类
* 1、扫描组件
* 2、视图解析器
* 3、view-controller
* 4、default-servlet-handler
* 5、mvc注解驱动
* 6、文件上传解析器
* 7、异常处理
* 8、拦截器
* @Date 2022-01-03 13:41:45
*/
//配置类
@Configuration
//开启注解扫描
@ComponentScan(value = "com.bookstore", includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class)})
//开启mvc注解驱动
@EnableWebMvc
public class SpringMVCConfig implements WebMvcConfigurer {
// thymeleaf模板引擎参数

public final static String TEMPLATE_PREFIX = "/WEB-INF/templates/";
public final static String TEMPLATE_SUFFIX = ".html";
public final static String TEMPLATE_ENCODING = "UTF-8";
public final static Boolean TEMPLATE_CACHEABLE = false;
public final static String TEMPLATE_TYPE = "HTML5";
public final static Integer TEMPLATE_ORDER = 1;
//配置生成模板解析器

@Bean
public SpringResourceTemplateResolver getTemplateResolver() {
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
// ServletContextTemplateResolver,注入其中的webApplicationContext,可通过WebApplicationContext 的方法获得
SpringResourceTemplateResolver springResourceTemplateResolver = new SpringResourceTemplateResolver();
springResourceTemplateResolver.setApplicationContext(webApplicationContext);
//设置模板解析前缀
springResourceTemplateResolver.setPrefix(TEMPLATE_PREFIX);
//设置模板解析后缀
springResourceTemplateResolver.setSuffix(TEMPLATE_SUFFIX);
//设置文件编码
springResourceTemplateResolver.setCharacterEncoding(TEMPLATE_ENCODING);
//设置是否开启缓存
springResourceTemplateResolver.setCacheable(TEMPLATE_CACHEABLE);
//设置模板类型
springResourceTemplateResolver.setTemplateMode(TEMPLATE_TYPE);
//设置视图解析器优先级
springResourceTemplateResolver.setOrder(TEMPLATE_ORDER);
return springResourceTemplateResolver;
}
//生成模板引擎并为模板引擎注入模板解析器(使用接口接受子类模板解析器)

@Bean
public SpringTemplateEngine getTemplateEngine(ITemplateResolver templateResolver) {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
return templateEngine;
}
//生成视图解析器并未解析器注入模板引擎

@Bean
public ViewResolver getViewResolver(SpringTemplateEngine templateEngine) {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setTemplateEngine(templateEngine);
viewResolver.setOrder(1);
return viewResolver;
}
@Bean // 配置JSP视图解析器
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
resolver.setOrder(10);
return resolver;
}

//启用ViewController视图控制器
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}

//启用默认Servlet handler
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

//配置文件上传解析器

@Bean
public MultipartResolver getMultipartResolver(){
return new CommonsMultipartResolver();
}

//配置异常解析器
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
//配置默认异常处理解析器
DefaultHandlerExceptionResolver defaultHandlerExceptionResolver = new DefaultHandlerExceptionResolver();
resolvers.add(defaultHandlerExceptionResolver);

//配置自定义异常解析器
SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
Properties properties = new Properties();
//配置处理的异常信息
properties.setProperty("java.lang.ArithmeticException","error");//设置算数计算异常
simpleMappingExceptionResolver.setExceptionMappings(properties);
//配置输出异常
simpleMappingExceptionResolver.setExceptionAttribute("exception");
resolvers.add(simpleMappingExceptionResolver);
}

//添加拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
FirstInterceptor firstInterceptor = new FirstInterceptor();
registry.addInterceptor(firstInterceptor).addPathPatterns("/**");
}

}

阅读全文 »

Junit4测试框架与Spring集成测试

配置测试框架的测试基类,防止多次初始化Spring容器,测试类只需要继承基类即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//BaseTest.class
@RunWith(SpringJUnit4ClassRunner.class)//使用junit4驱动
//引入配置文件
@ContextConfiguration(value = {"classpath:SpringConfig.xml", "classpath:Spring-MVC.xml", "classpath:MyBatis.xml"})
@Transactional(transactionManager = "dataSourceTransactionManager")//配置事务管理器
@Rollback(value = true)//值为true测试后该操作会回滚(不影响数据库),值为false则与此相反
public class BaseTest {
@Rule//junit测试方法规则
public TestWatcher testWatcher = new TestWatcher() {

@Override
protected void starting(Description description) {//开始执行时进行
// System.out.println(description.getMethodName()+" Starting.......");//方法名称
System.out.println(">>>>>>>>>>> " + description.getDisplayName() + " Starting.......");//全类名
}

@Override
protected void succeeded(Description description) {//执行成功进行
System.out.println(">>>>>>>>>>> " + description.getDisplayName() + " Succeeded.......");//全类名
}

@Override
protected void failed(Throwable e, Description description) {
System.out.println(">>>>>>>>>>> " + description.getDisplayName() + " >>>>>> Failed <<<<<<");//全类名
}
};
}
阅读全文 »

Junit4 常用注解

一、Junit4常用注解

@Test

    Test注解应用于方法上,标注一个方法是可以进行运行测试,其方法必须是public void 类型!

@Ignore

    应用于方法或者类上面,可以用来忽略Junit运行的测试。

@BeforeClass

    在每一个测试类之前执行,标注于方法之上,一般用于初始化数据。

@AfterClass

    在每一个测试类之后执行,标注与方法之上,一般用于清理测试缓存以及关闭相关连接。

阅读全文 »