java - SpringMVC如何有没有中间件之类的东西 ?
问题描述
@RequestMapping('/admin') public String index(ModelMap modelMap,HttpServletRequest req){String scheme = req.getScheme();String serverName = req.getServerName();int serverPort = req.getServerPort();String path = req.getContextPath();String basePath = scheme+'://'+serverName+':'+serverPort+path+'/';modelMap.put('basePath',basePath);modelMap.put('adminPath', basePath+'admin/');modelMap.put('staticPath', basePath+'static/admin/common');return 'admin/index'; } @RequestMapping('/admin/login') public String login(ModelMap modelMap,HttpServletRequest req){String scheme = req.getScheme();String serverName = req.getServerName();int serverPort = req.getServerPort();String path = req.getContextPath();String basePath = scheme+'://'+serverName+':'+serverPort+path+'/';modelMap.put('basePath',basePath);modelMap.put('adminPath', basePath+'admin/');modelMap.put('staticPath', basePath+'static/admin/common');return 'admin/login'; }
获取路径的代码写了两份 ,感觉好臃肿啊,怎么让只写一份,然后给共用?
问题解答
回答1:1.首先中间件概念你没弄清楚,不能乱用2.回到你的问题,就是一个方法抽取的场景,建议看一下<<代码重构>>这本书
回答2:写到filter当中,或者用动态代理
回答3:代码随便重构一下就好看多了
public String index(ModelMap modelMap,HttpServletRequest req){ String basePath = getBasePath(req); modelMap.put('basePath',basePath); modelMap.put('adminPath', basePath+'admin/'); modelMap.put('staticPath', basePath+'static/admin/common'); return 'admin/index';}private String getBasePath(HttpServletRequest req) { String scheme = req.getScheme(); String serverName = req.getServerName(); int serverPort = req.getServerPort(); String path = req.getContextPath(); String basePath = scheme+'://'+serverName+':'+serverPort+path+'/'; return basePath;}
相关文章:
1. debian - docker依赖的aufs-tools源码哪里可以找到啊?2. dockerfile - [docker build image失败- npm install]3. docker images显示的镜像过多,狗眼被亮瞎了,怎么办?4. docker绑定了nginx端口 外部访问不到5. 如何判断数组写入数据库有重复的值6. boot2docker无法启动7. docker start -a dockername 老是卡住,什么情况?8. macos - mac下docker如何设置代理9. angular.js - angularjs移动端项目需要引入外部的PC网页,iframe宽高设定100%,显示的内容依然是PC那种效果,不是缩放成适配屏10. mac连接阿里云docker集群,已经卡了2天了,求问?