時(shí)間:2023-08-15 00:03:01 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-08-15 00:03:01 來(lái)源:網(wǎng)站運(yùn)營(yíng)
看看阿里13年后端開(kāi)發(fā)大佬如何利用J2EE模式構(gòu)建網(wǎng)站:一、前言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);//啟動(dòng)注冊(cè)視圖助手 Command command=rh.getCommand();//由視圖助手中獲得并初始化Command CustomerBean cb=rh.getCustomerBean();//由視圖助手中獲得并初始化值對(duì)象 request.setAttribute("customerbean",cb); String dispatcher=rh.getDispatcher();//由視圖助手中獲得并初始化分發(fā)者 request.setAttribute("type",rh.getType());//設(shè)置上下文屬性 try { command.execute((Helper)rh);//執(zhí)行業(yè)務(wù)代碼 } catch(javax.ejb.DuplicateKeyException de) { request.setAttribute("errorbean",new ErrorBean("對(duì)不起,已經(jīng)有人注冊(cè)了該用戶名!"));//注冊(cè)重名處理 dispatch(request,response,dispatcher);//分發(fā)并移交控制權(quán) return; } catch(Exception e) { request.setAttribute("errorbean",new ErrorBean("對(duì)不起,數(shù)據(jù)庫(kù)出錯(cuò)!"));//出錯(cuò)處理 dispatch(request,response,dispatcher); return; } dispatch(request,response,dispatcher); }
優(yōu)點(diǎn):通過(guò)集中化決策點(diǎn)和控制,控制器有助于減少嵌入在JSP中Java代碼(Scriptlet)的數(shù)量,保持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); } /** * 定義頁(yè)面類型: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; } /** * 向值對(duì)象中填充數(shù)據(jù) */ 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)); } /** * 獲取值對(duì)象 */ public CustomerBean getCustomerBean() { return this.customer; } /** * 獲取分發(fā)者 */ public String getDispatcher() { return this.dispatcher; } /** * 獲取類型 */ public String getType() { return type; }}
優(yōu)點(diǎn):在助手中而不是在視圖中封裝業(yè)務(wù)邏輯會(huì)增強(qiáng)應(yīng)用程序的模塊化,并且更有利于組件重用。助手有大量的責(zé)任,包括收集視圖和控制需要的數(shù)據(jù),以及存儲(chǔ)中間模型。它的位置如圖5中Helper所示。public void execute(Helper helper) throws Exception { RegisterHelper rh = (RegisterHelper) helper;//獲取視圖助手 CustomerBean cb = rh.getCustomerBean();//從視圖助手中獲取值對(duì)象 ServiceLocator sl = ServiceLocator.getInstance();//初始化服務(wù)定位器 CustomersHome ch = (CustomersHome) sl.getHome(ServiceLocator.Services.CUSTOMERS);//從服務(wù)定位器中獲取Entity Bean本地接口 try { Customers customers = ch.create(cb);//將注冊(cè)信息導(dǎo)入數(shù)據(jù)庫(kù) } catch(javax.ejb.DuplicateKeyException e) { throw new javax.ejb.DuplicateKeyException(); } catch(Exception e) { throw e; } }
(4)分發(fā)者模式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");//獲取頁(yè)面類型 isError=(request.getAttribute("errorbean")!=null)?true:false;//判斷是否出錯(cuò) String file=selectType(type,isError,response);//根據(jù)頁(yè)面類型和是否出錯(cuò)確定顯示頁(yè)面 getServletConfig().getServletContext().getRequestDispatcher(file).forward(request,response);//重定向到顯示頁(yè)面 } public String selectType(String str,boolean isError,javax.servlet.http.HttpServletResponse response) { if (str.equals("html")) {//HTML類型的頁(yè)面 if (isError) {//成功 System.out.println("Some error happens!"); return "register_error.jsp"; } else {//出錯(cuò) return "register_ok.jsp"; } } else {//WML手機(jī)頁(yè)面 if (isError) {//成功 System.out.println("Some error happens!"); return "wml/register_error.jsp"; } else {//出錯(cuò) response.setContentType("text/vnd.wap.wml;charSet=gb2312"); return "wml/register_ok.jsp"; } } }
(5)復(fù)合視圖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設(shè)定請(qǐng)求序號(hào) 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() {//單線程處理以節(jié)省資源 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; }}
缺點(diǎn):如果增加新的EJB,需要修改服務(wù)定位器的代碼。關(guān)鍵詞:利用,模式
客戶&案例
營(yíng)銷資訊
關(guān)于我們
客戶&案例
營(yíng)銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。