From d8025e3dffce37ef1eea0df1515896054738df59 Mon Sep 17 00:00:00 2001 From: wangbing Date: Wed, 1 May 2019 01:59:29 +0800 Subject: [PATCH] =?UTF-8?q?Home=E9=A1=B5=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manger/callable/SpringBootCallable.java | 6 +- .../java/action/GlobalController.ftl | 16 +-- .../SpringBoot/java/config/WebMvcConfig.java | 67 ++++++++- .../springmvc/GlobalHandlerInterceptor.java | 65 --------- .../java/frame/utils/LocalData.java | 4 +- .../SpringBoot/resources/application-dev.ftl | 6 +- .../SpringBoot/resources/application-prod.ftl | 6 +- .../resources/templates/screen/login.ftl | 134 ++++++++++++++++++ .../modules/SpringBoot/test/TestConfig.ftl | 8 -- 9 files changed, 208 insertions(+), 104 deletions(-) delete mode 100644 src/main/resources/modules/SpringBoot/java/frame/springmvc/GlobalHandlerInterceptor.java create mode 100644 src/main/resources/modules/SpringBoot/resources/templates/screen/login.ftl diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/SpringBootCallable.java b/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/SpringBootCallable.java index e96a3ddc..6eacfdf9 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/SpringBootCallable.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/SpringBootCallable.java @@ -493,8 +493,6 @@ public class SpringBootCallable implements Callable { base.mkdirs(); File freemarker = new File(root.getAbsolutePath(), "freemarker"); freemarker.mkdirs(); - File springmvc = new File(root.getAbsolutePath(), "springmvc"); - springmvc.mkdirs(); File utils = new File(root.getAbsolutePath(), "utils"); utils.mkdirs(); @@ -520,9 +518,6 @@ public class SpringBootCallable implements Callable { freeMarkerManager.outputTemp(new File(freemarker.getAbsolutePath(), "Layout.java"), option + "/java/frame/freemarker/Layout.java", ctx); freeMarkerManager.outputTemp(new File(freemarker.getAbsolutePath(), "Uri.java"), option + "/java/frame/freemarker/Uri.java", ctx); - //springmvc - freeMarkerManager.outputTemp(new File(springmvc.getAbsolutePath(), "GlobalHandlerInterceptor.java"), option + "/java/frame/springmvc/GlobalHandlerInterceptor.java", ctx); - //uitls freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "Base64Util.java"), option + "/java/frame/utils/Base64Util.java", ctx); freeMarkerManager.outputTemp(new File(utils.getAbsolutePath(), "AESUtil.java"), option + "/java/frame/utils/AESUtil.java", ctx); @@ -728,6 +723,7 @@ public class SpringBootCallable implements Callable { Tool.outputResource(option + "/resources/templates/screen/demo.ftl", new File(screen.getAbsolutePath(), "demo.ftl")); Tool.outputResource(option + "/resources/templates/screen/index.ftl", new File(screen.getAbsolutePath(), "index.ftl")); Tool.outputResource(option + "/resources/templates/screen/home.ftl", new File(screen.getAbsolutePath(), "home.ftl")); + Tool.outputResource(option + "/resources/templates/screen/login.ftl", new File(screen.getAbsolutePath(), "login.ftl")); Tool.outputResource(option + "/resources/templates/403.ftl", new File(templates.getAbsolutePath(), "403.ftl")); Tool.outputResource(option + "/resources/templates/404.ftl", new File(templates.getAbsolutePath(), "404.ftl")); Tool.outputResource(option + "/resources/templates/500.ftl", new File(templates.getAbsolutePath(), "500.ftl")); diff --git a/src/main/resources/modules/SpringBoot/java/action/GlobalController.ftl b/src/main/resources/modules/SpringBoot/java/action/GlobalController.ftl index 9d71d1dd..42b177ca 100644 --- a/src/main/resources/modules/SpringBoot/java/action/GlobalController.ftl +++ b/src/main/resources/modules/SpringBoot/java/action/GlobalController.ftl @@ -103,22 +103,12 @@ public class GlobalController implements ErrorController { public void hold(HttpServletRequest request, Model model) { HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); - // 处理根Url - String servletPath = request.getServletPath(); - if (servletPath.matches("(.+)\\.htm")) { - Pattern compile = Pattern.compile("(.+)\\.htm"); - Matcher matcher = compile.matcher(servletPath); - if (matcher.find()){ - servletPath = matcher.group(1); - } - } - LocalData.setTarget(servletPath); - // 尝试执行Target Screen执行器(服务器渲染),不存在则直接返回视图模板(Ajax渲染) Screen screenExec = null; try { - servletPath = servletPath.replaceAll("/", ".").toLowerCase(); - screenExec = LocalData.getApplicationContext().getBean(ActionConfig.SCREEN_PREFIX + servletPath, Screen.class); + String target = LocalData.getTarget(); + target = target.replaceAll("/", ".").toLowerCase(); + screenExec = LocalData.getApplicationContext().getBean(ActionConfig.SCREEN_PREFIX + target, Screen.class); screenExec.exec(model, request, response); } catch (BeansException e) { diff --git a/src/main/resources/modules/SpringBoot/java/config/WebMvcConfig.java b/src/main/resources/modules/SpringBoot/java/config/WebMvcConfig.java index e0121427..94d66f92 100644 --- a/src/main/resources/modules/SpringBoot/java/config/WebMvcConfig.java +++ b/src/main/resources/modules/SpringBoot/java/config/WebMvcConfig.java @@ -3,22 +3,31 @@ package ${basePackage}.config; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; -import ${basePackage}.frame.springmvc.GlobalHandlerInterceptor; -import org.springframework.beans.factory.annotation.Autowired; +import ${basePackage}.frame.base.Token; +import ${basePackage}.frame.utils.LocalData; +import ${basePackage}.frame.utils.LogUtil; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.List; @Configuration public class WebMvcConfig implements WebMvcConfigurer { - @Autowired - private GlobalHandlerInterceptor globalHandlerInterceptor; + @Value("${r'${web.welcome.page}'}") + private String homePage; /** * 增加全局拦截器,可用于 @@ -27,7 +36,53 @@ public class WebMvcConfig implements WebMvcConfigurer { */ @Override public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(globalHandlerInterceptor).addPathPatterns("/**").excludePathPatterns("/static/**"); + registry.addInterceptor(new HandlerInterceptorAdapter() { + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + // 处理根Url + String servletPath = request.getServletPath(); + if (servletPath.matches("(.+)\\.htm")) { + Pattern compile = Pattern.compile("(.+)\\.htm"); + Matcher matcher = compile.matcher(servletPath); + if (matcher.find()) { + servletPath = matcher.group(1); + } + } + LocalData.setTarget(servletPath); + return super.preHandle(request, response, handler); + } + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { + super.postHandle(request, response, handler, modelAndView); + + //当请求为@ResponseBody,modelAndView为null,不处理 + if (modelAndView == null) { + return; + } + + //获取当前用户信息 + Token token = LocalData.getToken(); + modelAndView.addObject("token", token); + + //主页 + modelAndView.addObject("homePath", homePage); + + //获取项目路径,在项目非根路径下用于拼接URL + String contextPath = request.getContextPath(); + modelAndView.addObject("contextPath", contextPath); + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + super.afterCompletion(request, response, handler, ex); + + if (ex != null) { + LogUtil.dumpException(ex); + } + } + + }).addPathPatterns("/**/*.htm").order(-1); } /** diff --git a/src/main/resources/modules/SpringBoot/java/frame/springmvc/GlobalHandlerInterceptor.java b/src/main/resources/modules/SpringBoot/java/frame/springmvc/GlobalHandlerInterceptor.java deleted file mode 100644 index 7762ea9a..00000000 --- a/src/main/resources/modules/SpringBoot/java/frame/springmvc/GlobalHandlerInterceptor.java +++ /dev/null @@ -1,65 +0,0 @@ -package ${basePackage}.frame.springmvc; - -import ${basePackage}.frame.utils.LocalData; -import ${basePackage}.frame.base.Token; -import ${basePackage}.frame.utils.LogUtil; -import org.springframework.web.servlet.ModelAndView; -import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -/** - * SpringMVC 全局拦截器,日志记录 - * - * @author wangbing - * @version 0.0.1 - * @since 2017-01-01 - */ -@Component -public class GlobalHandlerInterceptor extends HandlerInterceptorAdapter { - - @Value("${r"${web.welcome.page}"}") - private String homePage; - - @Override - public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - return super.preHandle(request, response, handler); - } - - @Override - public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { - super.postHandle(request, response, handler, modelAndView); - - //当请求为@ResponseBody,modelAndView为null,不处理 - if (modelAndView == null){ - return; - } - - //获取当前用户信息 - Token token = LocalData.getToken(); - modelAndView.addObject("token", token); - - //主页 - modelAndView.addObject("homePath", homePage); - - //获取项目路径,在项目非根路径下用于拼接URL - String contextPath = request.getContextPath(); - modelAndView.addObject("contextPath", contextPath); - } - - @Override - public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { - super.afterCompletion(request, response, handler, ex); - - if (ex != null) { - LogUtil.dumpException(ex); - } - } - - @Override - public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - super.afterConcurrentHandlingStarted(request, response, handler); - } -} diff --git a/src/main/resources/modules/SpringBoot/java/frame/utils/LocalData.java b/src/main/resources/modules/SpringBoot/java/frame/utils/LocalData.java index 94e40dc7..2bdc94d7 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/utils/LocalData.java +++ b/src/main/resources/modules/SpringBoot/java/frame/utils/LocalData.java @@ -17,7 +17,6 @@ import javax.servlet.http.HttpServletResponse; * @since 2017-01-01 */ public class LocalData { - private static final long serialVersionUID = 1L; private static Token temp = null; @@ -28,8 +27,11 @@ public class LocalData { temp.setId(-1); temp.setUserId(-1); temp.setUserName("游客"); + temp.putResource("/"); temp.putResource("/ajax"); temp.putResource("/upload"); + temp.putResource("/index.htm"); + temp.putResource("/home.htm"); temp.putResource("ajax.example.example"); system = new Token(); system.setId(0); diff --git a/src/main/resources/modules/SpringBoot/resources/application-dev.ftl b/src/main/resources/modules/SpringBoot/resources/application-dev.ftl index a036b114..6423a537 100644 --- a/src/main/resources/modules/SpringBoot/resources/application-dev.ftl +++ b/src/main/resources/modules/SpringBoot/resources/application-dev.ftl @@ -14,10 +14,10 @@ spring.http.encoding.enabled=true server.tomcat.uri-encoding=UTF-8 # 根路径、欢迎页 web.welcome.page = /index.htm -# 需要验证授权 +# 需要验证授权, 既访问时组装Token web.url.auth.included = /** -# 不需要验证授权 -web.url.auth.excluded = /favicon.ico,/static/**,/open/**,/api,/index.htm,/home.htm,/,/login.htm,/app.htm +# 不需要验证授权, 或该请求有自己的验证机制 +web.url.auth.excluded = /favicon.ico,/static/**,/open/**,/api,/login.htm # 默认的登录URL web.url.login = /login.htm # 日志配置 diff --git a/src/main/resources/modules/SpringBoot/resources/application-prod.ftl b/src/main/resources/modules/SpringBoot/resources/application-prod.ftl index 8e6eeee5..ea0b0a80 100644 --- a/src/main/resources/modules/SpringBoot/resources/application-prod.ftl +++ b/src/main/resources/modules/SpringBoot/resources/application-prod.ftl @@ -15,10 +15,10 @@ spring.http.encoding.enabled=true server.tomcat.uri-encoding=UTF-8 # 根路径、欢迎页 web.welcome.page = /index.htm -# 需要验证授权 +# 需要验证授权, 既访问时组装Token web.url.auth.included = /** -# 不需要验证授权 -web.url.auth.excluded = /favicon.ico,/static/**,/open/**,/api,/index.htm,/home.htm,/,/login.htm,/app.htm +# 不需要验证授权, 或该请求有自己的验证机制 +web.url.auth.excluded = /favicon.ico,/static/**,/open/**,/api,/login.htm # 默认的登录URL web.url.login = /login.htm # 日志配置 diff --git a/src/main/resources/modules/SpringBoot/resources/templates/screen/login.ftl b/src/main/resources/modules/SpringBoot/resources/templates/screen/login.ftl new file mode 100644 index 00000000..adb8367d --- /dev/null +++ b/src/main/resources/modules/SpringBoot/resources/templates/screen/login.ftl @@ -0,0 +1,134 @@ +
+
+ +
+ +
+ + + diff --git a/src/main/resources/modules/SpringBoot/test/TestConfig.ftl b/src/main/resources/modules/SpringBoot/test/TestConfig.ftl index 43fc9c3b..01001473 100644 --- a/src/main/resources/modules/SpringBoot/test/TestConfig.ftl +++ b/src/main/resources/modules/SpringBoot/test/TestConfig.ftl @@ -1,16 +1,8 @@ package ${basePackage}.config; import ${basePackage}.frame.base.Token; -import ${basePackage}.frame.springmvc.GlobalHandlerInterceptor; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.web.filter.CharacterEncodingFilter; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; - @Configuration public class TestConfig {