标签:
所谓工具包,是指把页面的功能划分出来,放到另外一个包里面。方面工程管理、结构清晰、团队协作等。
上面听起来稍微比较复杂的,但是我感觉还是可以理解的。
代码如下;
package com.myweb.tool; public interface NavBar { //导航栏接口 public String getBarContent();//获取导航栏 }
package com.myweb.tool; public class TopBar implements NavBar { @Override public String getBarContent() { return " <a href=‘‘>首页</a> | <a href=‘‘>新闻</a> | <a href=‘‘>用户注册</a> "; } }
package com.myweb.tool; public class BottomBar implements NavBar{ @Override public String getBarContent() { return " <a href=‘‘>首页</a> | <a href=‘‘>关于我们</a> | <a href=‘‘>招聘信息</a> "; } }
package com.myweb.tool; public class BarFactory { public static NavBar CreateBar(String type) { NavBar nv; switch(type) { case "top": nv = new TopBar(); break; case "bottom": nv = new BottomBar(); break; default: nv = new TopBar(); break; } return nv; } }
@Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { ModelAndView mv = new ModelAndView(view); /* TopBar topNav = new TopBar(); mv.addObject("top_nav", topNav.getBarContent()); BottomBar bottomNav = new BottomBar(); mv.addObject("bottom_nav", bottomNav.getBarContent());*/ NavBar topNav = BarFactory.CreateBar("top"); NavBar bottomNav = BarFactory.CreateBar("bottom"); mv.addObject("top_nav", topNav.getBarContent()); mv.addObject("bottom_nav", bottomNav.getBarContent()); return mv; }
<!DOCTYPE html> <!-- [ published at 2015-11-13 12:30:50 ] --> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <title>新阳</title> </head> <body> <div style="width:100%;height:95px;border-bottom:solid 1px gray"> ${top_nav} </div> <div > body content </div> <div style="width:100%;height:95px;border-bottom:solid 1px gray;margin-top:500px"> ${bottom_nav} © 2015 新阳 </div> </body> </html>
标签:
原文地址:http://www.cnblogs.com/SimonGao/p/4961999.html