<!-- 支持jsp --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <version>9.0.0.M15</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> 复制代码
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp 复制代码
直接编写cotroller及路由,便可以访问对应的jsp页面
@Configuration public class FastJsonConfigInfo implements WebMvcConfigurer{ public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); converter.setFastJsonConfig(fastJsonConfig); converters.add(converter); } } 复制代码
public class CustomerInterceptor implements HandlerInterceptor { private final Logger logger = LoggerFactory.getLogger(CustomerInterceptor.class); /** * 针对那些情况做拦截: * false则不处理,直接返回。 * true 交给对应的handler处理 * @param request * @param response * @param handler * @return * @throws Exception */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String path = request.getServletPath(); logger.info("request of path is {}",path); if("/indexJson".equals(path)){ return true; } return false; } /** * handler 执行之前处理,数据校验等 * @param request * @param response * @param handler * @param modelAndView * @throws Exception */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { Map<String,String[]> params = request.getParameterMap(); logger.info("request params info is {}",JSONObject.toJSONString(params)); logger.info("{}",handler.getClass().getName()); } /** * hanler 处理完成之后 * @param request * @param response * @param handler * @param ex * @throws Exception */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { logger.error("the request is exception,the exception info is",ex); } } 复制代码
@Configuration public class FastJsonConfigInfo implements WebMvcConfigurer{ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); converter.setFastJsonConfig(fastJsonConfig); converters.add(converter); } /** 将自定义的拦截器注册到容器中 ***/ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new CustomerInterceptor()).addPathPatterns("/**"); } } 复制代码