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. (JAVA)最近在做到支付宝的一码多付支付接口,发现没有异步回调通知,需要自己轮询查询订单状态,这个需要怎么实现?2. mysql索引的疑问3. python小白的基础问题 关于while循环的嵌套4. python - celery beat 调度如何运行期间动态添加任务?5. mysql sql where id in(25,12,87) 结果集如何用按照 25 12 87排序?6. python - django 项目的 migrations 目录是否应该提交到 git7. python - 多态调用方法时却显示bound method...8. 取不出SQL得到的当前页9. css - 这些字体是怎么弄的?10. spring - mysql中事务是互斥的么?

网公网安备