SpringMVC配置文件.xml 发表于 2021-02-17 更新于 2024-09-24 分类于 开发 && 学习 , Java , SpringMVC 阅读次数: Artalk: 本文字数: 3.4k 阅读时长 ≈ 3 分钟 SpringMVC配置文件.xml123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566<?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注解配置 发表于 2021-02-17 更新于 2024-09-24 分类于 开发 && 学习 , Java , SpringMVC 阅读次数: Artalk: 本文字数: 5.7k 阅读时长 ≈ 5 分钟 SpringMVC注解配置123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153package 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注解驱动@EnableWebMvcpublic 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集成测试 发表于 2021-02-13 更新于 2024-09-24 分类于 开发 && 学习 , Java , Junit 阅读次数: Artalk: 本文字数: 1.3k 阅读时长 ≈ 1 分钟 Junit4测试框架与Spring集成测试配置测试框架的测试基类,防止多次初始化Spring容器,测试类只需要继承基类即可 123456789101112131415161718192021222324252627//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 常用注解 发表于 2021-02-11 更新于 2024-09-24 分类于 开发 && 学习 , Java , Junit 阅读次数: Artalk: 本文字数: 5.7k 阅读时长 ≈ 5 分钟 Junit4 常用注解一、Junit4常用注解@Test Test注解应用于方法上,标注一个方法是可以进行运行测试,其方法必须是public void 类型! @Ignore 应用于方法或者类上面,可以用来忽略Junit运行的测试。 @BeforeClass 在每一个测试类之前执行,标注于方法之上,一般用于初始化数据。 @AfterClass 在每一个测试类之后执行,标注与方法之上,一般用于清理测试缓存以及关闭相关连接。 阅读全文 »