时间:2023-08-15 00:03:01 | 来源:网站运营
时间:2023-08-15 00:03:01 来源:网站运营
看看阿里13年后端开发大佬如何利用J2EE模式构建网站:一、前言public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { RegisterHelper rh=new RegisterHelper(request,response);//启动注册视图助手 Command command=rh.getCommand();//由视图助手中获得并初始化Command CustomerBean cb=rh.getCustomerBean();//由视图助手中获得并初始化值对象 request.setAttribute("customerbean",cb); String dispatcher=rh.getDispatcher();//由视图助手中获得并初始化分发者 request.setAttribute("type",rh.getType());//设置上下文属性 try { command.execute((Helper)rh);//执行业务代码 } catch(javax.ejb.DuplicateKeyException de) { request.setAttribute("errorbean",new ErrorBean("对不起,已经有人注册了该用户名!"));//注册重名处理 dispatch(request,response,dispatcher);//分发并移交控制权 return; } catch(Exception e) { request.setAttribute("errorbean",new ErrorBean("对不起,数据库出错!"));//出错处理 dispatch(request,response,dispatcher); return; } dispatch(request,response,dispatcher); }
优点:通过集中化决策点和控制,控制器有助于减少嵌入在JSP中Java代码(Scriptlet)的数量,保持View功能的纯洁性。它的位置如图5中Controller所示。public class RegisterHelper implements Helper { static String dispatcher = "RegisterDispatcher"; private CustomerBean customer = null; private String type = null; public RegisterHelper( javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) { setType(request); setCustomerBean(request); } /** * 定义页面类型:HTML or XML */ public void setType(javax.servlet.http.HttpServletRequest request) { type = request.getParameter("type"); } /** * 获取Command */ public Command getCommand() { RegisterCommand rc = new RegisterCommand(); return rc; } /** * 向值对象中填充数据 */ public void setCustomerBean(javax.servlet.http.HttpServletRequest request) { customer = new CustomerBean(); customer.setUsername(request.getParameter("username")); customer.setPassword(request.getParameter("password")); customer.setEmail(request.getParameter("email")); customer.setTruename(request.getParameter("truename")); customer.setId(request.getParameter("id")); customer.setService(this.setService(request)); } /** * 获取值对象 */ public CustomerBean getCustomerBean() { return this.customer; } /** * 获取分发者 */ public String getDispatcher() { return this.dispatcher; } /** * 获取类型 */ public String getType() { return type; }}
优点:在助手中而不是在视图中封装业务逻辑会增强应用程序的模块化,并且更有利于组件重用。助手有大量的责任,包括收集视图和控制需要的数据,以及存储中间模型。它的位置如图5中Helper所示。public void execute(Helper helper) throws Exception { RegisterHelper rh = (RegisterHelper) helper;//获取视图助手 CustomerBean cb = rh.getCustomerBean();//从视图助手中获取值对象 ServiceLocator sl = ServiceLocator.getInstance();//初始化服务定位器 CustomersHome ch = (CustomersHome) sl.getHome(ServiceLocator.Services.CUSTOMERS);//从服务定位器中获取Entity Bean本地接口 try { Customers customers = ch.create(cb);//将注册信息导入数据库 } catch(javax.ejb.DuplicateKeyException e) { throw new javax.ejb.DuplicateKeyException(); } catch(Exception e) { throw e; } }
(4)分发者模式public void performTask( javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { String type=(String)request.getAttribute("type");//获取页面类型 isError=(request.getAttribute("errorbean")!=null)?true:false;//判断是否出错 String file=selectType(type,isError,response);//根据页面类型和是否出错确定显示页面 getServletConfig().getServletContext().getRequestDispatcher(file).forward(request,response);//重定向到显示页面 } public String selectType(String str,boolean isError,javax.servlet.http.HttpServletResponse response) { if (str.equals("html")) {//HTML类型的页面 if (isError) {//成功 System.out.println("Some error happens!"); return "register_error.jsp"; } else {//出错 return "register_ok.jsp"; } } else {//WML手机页面 if (isError) {//成功 System.out.println("Some error happens!"); return "wml/register_error.jsp"; } else {//出错 response.setContentType("text/vnd.wap.wml;charSet=gb2312"); return "wml/register_ok.jsp"; } } }
(5)复合视图public class ServiceLocator { private static ServiceLocator me; InitialContext context = null; /** * 初始化上下文 */ public ServiceLocator() { try { context = new InitialContext(); } catch (NamingException e) { e.printStackTrace(); } } public class Services { //为EJB设定请求序号 final public static int CUSTOMERS=0; final public static int PARTNERS=1; final public static int ADMINISTRATORS=2; final public static int PERMITS=3; final public static int PAPERBROKER=4; final public static int CHECK=5; } final static Class CUSTOMERS_CLASS=CustomersHome.class; final static String CUSTOMERS_NAME="CustomersHome"; final static Class PARTNERS_CLASS=PartnersHome.class; final static String PARTNERS_NAME="PartnersHome"; final static Class ADMINISTRATORS_CLASS=AdministratorsHome.class; final static String ADMINISTRATORS_NAME="AdministratorsHome"; final static Class PERMITS_CLASS=PermitsHome.class; final static String PERMITS_NAME="PermitsHome"; final static Class PAPERBROKER_CLASS=PaperBrokerHome.class; final static String PAPERBROKER_NAME="PaperBrokerHome"; final static Class CHECK_CLASS=CheckHome.class; final static String CHECK_NAME="CheckHome"; public static ServiceLocator getInstance() {//单线程处理以节省资源 if (me == null) me = new ServiceLocator(); return me; } static private Class getServiceClass(int service) { switch(service) { case Services.CUSTOMERS: return CUSTOMERS_CLASS; case Services.PARTNERS: return PARTNERS_CLASS; case Services.ADMINISTRATORS: return ADMINISTRATORS_CLASS; case Services.PERMITS: return PERMITS_CLASS; case Services.PAPERBROKER: return PAPERBROKER_CLASS; case Services.CHECK: return CHECK_CLASS; } return null; } static private String getServiceName(int service) { switch(service) { case Services.CUSTOMERS: return CUSTOMERS_NAME; case Services.PARTNERS: return PARTNERS_NAME; case Services.ADMINISTRATORS: return ADMINISTRATORS_NAME; case Services.PERMITS: return PERMITS_NAME; case Services.PAPERBROKER: return PAPERBROKER_NAME; case Services.CHECK: return CHECK_NAME; } return null; } /** * 返回EJB本地接口 */ public EJBHome getHome(int s) { EJBHome home = null; try { Object objref = context.lookup(getServiceName(s)); home = (EJBHome) PortableRemoteObject.narrow(objref, getServiceClass(s)); } catch (NamingException e) { e.printStackTrace(); } return home; }}
缺点:如果增加新的EJB,需要修改服务定位器的代码。关键词:利用,模式