隨筆-72  評論-63  文章-0  trackbacks-0
          (本故事除了部分點(diǎn)明道姓并具有故事詳細(xì)發(fā)生的具體時間點(diǎn)地等部分情節(jié)以外,其它內(nèi)容純屬虛構(gòu),若有雷同,純屬巧合。)

            談到B/S,談到J2EE,特別涉及到Java Web的部分,我們就會想到三層或多層構(gòu)架,為什么要多層,肯定是因?yàn)橐粌蓪右呀?jīng)無法達(dá)到用戶的需求,技術(shù)進(jìn)步了,三層或多層很多時候其系統(tǒng)的健壯性、可維護(hù)性等都較之提升了很多。
            在本系列文章的前兩篇中,我們基本上沒有涉及到Spring技術(shù)的部分,因此,今天換換味,主要給出一個比較簡單、實(shí)用,也是最庸俗的spring示例。也即實(shí)現(xiàn)一個數(shù)據(jù)表的添刪改查操作,筆者采用struts+hibernate+spring黃金庸俗組合來實(shí)現(xiàn)。示例比較簡單,因此spring的高手及高高手可以忽略示例中的技術(shù)細(xì)節(jié)部分,當(dāng)然,也請有時間的前輩不吝賜教有關(guān)代碼的書寫方法及技術(shù)。
          ?
            示例程序運(yùn)行效果,先睹為快:http://www.easyjf.com/html/bbs/20060509/1571738935251539.htm?ejid=1139563413901115
          ?
            主演:Spring、系統(tǒng)設(shè)計(jì)師、數(shù)據(jù)庫開發(fā)工程師、Web開發(fā)工程師、主程序員、系統(tǒng)集成及部署工程師。

            配角:數(shù)據(jù)庫管理員、美工及頁面設(shè)計(jì)MM、html,JSP、Struts、My Sql、hibernate。
          ?
            下面就是直殺正題吧!
          ?
          第一步:系統(tǒng)設(shè)計(jì)

            現(xiàn)在你扮演的是系統(tǒng)設(shè)計(jì)師角色,系統(tǒng)設(shè)計(jì)師通常都不用管也不想知道你要用什么方法去實(shí)現(xiàn)他的東西,他只給你畫出草圖,制訂規(guī)矩、條款等,他甚至可以不用知道Java中還有Class這個東西存在。他也不用管你用什么方式存放數(shù)據(jù),用什么數(shù)據(jù)庫中間件來操作數(shù)據(jù)。
            但他站得高,看得遠(yuǎn),為了以后的可維護(hù)性、靈活性,考慮到會使用多種數(shù)據(jù)庫,以及多種數(shù)據(jù)庫中間件等,因此會先對系統(tǒng)設(shè)計(jì)建模,通過UML等方式來描述系統(tǒng)流程、組織結(jié)構(gòu)等。同時還會通過使用接口定義出相應(yīng)的商業(yè)邏輯。下面是本例子中的兩個業(yè)務(wù)邏輯層的接口。

          第一個是“用戶”這一實(shí)體的描述,IUser.java,代碼如下:
          package com.easyjf.example.business;
          public interface IUser {?
          ?public String getBirthday();//生日
          ?public void setBirthday(String birthday);
          ?public String getCid();//主鍵cid
          ?public void setCid(String cid);
          ?public String getEmail();//用戶郵件email
          ?public void setEmail(String email);
          ?public String getIntro();//用戶簡介intro
          ?public void setIntro(String intro);
          ?public String getPassword();//用戶密碼password
          ?public void setPassword(String password);
          ?public String getTel() ;//用戶電話tel
          ?public void setTel(String tel);
          ?public String getUserName();//用戶名userName
          ?public void setUserName(String userName);
          }
          第二個是圍繞“用戶”這一實(shí)體所提供的有關(guān)服務(wù)描述,IUserService.java,代碼如下:

          package com.easyjf.example.business;
          import java.util.Collection;
          import java.util.List;
          public interface IUserService {
          ?public IUser newUser();?//創(chuàng)建一個新的用戶
          ?public IUser read(String cid);//根據(jù)主鍵cid讀取一個用戶
          ?public List query(String scope,Collection paras);//用戶查詢
          ?public IUser readByName(String userName);//根據(jù)用戶名讀取一個用戶
          ?public IUser login(String userName,String password,String ip);//用戶登錄?
          ?public boolean save(IUser user);//保存用戶
          ?public boolean update(IUser user);//修改用戶
          ?public boolean del(IUser user);//刪除用戶
          }
          ?
            除了上面這些,系統(tǒng)設(shè)計(jì)師還會根據(jù)系統(tǒng)需求給出一些具體的設(shè)計(jì)方法,對系統(tǒng)各層之間的接口規(guī)范,Action層使用的框架或模式等。他的工作就算完成了,下面就該程序員、數(shù)據(jù)庫管理員以及頁面設(shè)計(jì)人員等表演了。這些角色在我們實(shí)際項(xiàng)目中很多時候是同步進(jìn)行的,可謂花開N朵。然而本文為了能讓新同學(xué)把這個示例能跑起來,所以就簡單分了一下步驟,以便新同學(xué)按步驟進(jìn)行。
          ?
          第二步:數(shù)據(jù)庫設(shè)計(jì)及建表(數(shù)據(jù)庫管理員)
            隨便選擇一個數(shù)據(jù)庫My SQL,MS SQL或Oracle均可,先建設(shè)一張表,表名叫User,字段如下:
          cid,代表主鍵,username-用戶名,password-密碼,email-電子郵件,tel-電話,birthday-生日,intro-簡介,為了演示方便,我們?nèi)孔侄卧O(shè)置成字符型。
            下面是在My SQL中的表結(jié)構(gòu)腳本:
          CREATE TABLE `user` (
          ? `cid` char(16) NOT NULL,
          ? `username` char(30) NOT NULL,
          ? `password` char(30) NOT NULL,
          ? `email` varchar(50) default NULL,
          ? `tel` varchar(50) default NULL,
          ? `birthday` varchar(20) default NULL,
          ? `intro` varchar(500) default NULL,
          ? PRIMARY KEY? (`cid`)
          ) ENGINE=InnoDB DEFAULT CHARSET=gbk;
          ?
          ?
          第三步:隨便選擇一種MVC框架寫Web部份(Web開發(fā)工程師)

            暈,類Class都沒看見一個就要寫Action了啊?
            新手別叫,這里演示的是一種現(xiàn)代、先進(jìn)、高雅、華麗、貴族式的編程及設(shè)計(jì)方法!(暈,不準(zhǔn)吐!),J2EE中提倡的是面向接口編程,所以,系統(tǒng)設(shè)計(jì)師都給了你UML圖,甚至連詳細(xì)的Java接口都給你了,已經(jīng)夠多了,快動手吧。
            本例MVC使用的Struts框架,因此整個開發(fā)流程大致有下面的幾個步驟。

          1、?先根據(jù)IUser接口,寫一個ActionForm,代碼如下:
          package com.easyjf.struts.form;
          import org.apache.struts.action.ActionForm;
          public class UserForm extends ActionForm {
          ?private String cid;
          ?private String userName;
          ?private String password;
          ?private String email;
          ?private String tel;
          ?private String birthday;
          ?private String intro;
          ?
          ?public String getCid() {
          ??return cid;
          ?}
          ?public void setCid(String cid) {
          ??this.cid = cid;
          ?}
          ?…
          ??? //其它的getter、stter方法在此略過。
          }
          ?
          2、寫Action

            根據(jù)用戶的需求說明及設(shè)計(jì)師的相關(guān)模式或規(guī)范建議等,寫一個處理“添刪改查”等操作的Action,本文全部寫到一個UserManageAction.java 中,代碼如下:
          package com.easyjf.struts.action;
          import java.util.ArrayList;
          import java.util.Collection;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import org.apache.struts.action.Action;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.ActionMapping;
          import org.springframework.web.context.WebApplicationContext;
          import org.springframework.web.context.support.WebApplicationContextUtils;
          import com.easyjf.example.business.IUser;
          import com.easyjf.example.business.IUserService;
          import com.easyjf.struts.form.UserForm;
          import com.easyjf.util.CommUtil;
          import com.easyjf.web.tools.IPageList;
          import com.easyjf.web.tools.ListQuery;
          import com.easyjf.web.tools.PageList;
          public class UserManageAction extends Action {
          ?private IUserService userService;?
          ?public IUserService getUserService() {
          ??return userService;
          ?}
          ?public void setUserService(IUserService userService)
          ?{
          ??this.userService=userService;
          ?}
          ?public ActionForward execute(ActionMapping mapping,
          ???ActionForm form,
          ???HttpServletRequest request,
          ???HttpServletResponse response) throws Exception {
          ??WebApplicationContext wac =WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServlet().getServletContext());
          ??this.userService = (IUserService) wac.getBean("userService");
          ??String command=request.getParameter("easyJWebCommand");
          ??if(command==null || "".equals(command)||"list".equals(command)||"query".equals(command))
          ??{
          ???return doQuery(mapping,form,request,response);
          ??}
          ??else if("new".equals(command))
          ??{
          ???return mapping.findForward("edit");
          ??}
          ??else if("add".equals(command))
          ??{
          ???return doAdd(mapping,form,request,response);
          ??}
          ??else if("edit".equals(command))
          ??{
          ???return doEdit(mapping,form,request,response);
          ??}
          ??else if("update".equals(command))
          ??{
          ???return doUpdate(mapping,form,request,response);
          ??}
          ??else if("del".equals(command))
          ??{
          ???return doDel(mapping,form,request,response);
          ??}??
          ??return super.execute(mapping,form,request,response);
          ?}
          ?public ActionForward doEdit(ActionMapping mapping,
          ???ActionForm form,
          ???HttpServletRequest request,
          ???HttpServletResponse response)
          ?{??
          ??? UserForm vo=(UserForm)form;????
          ???IUser user=null;
          ???if(vo.getCid()!=null && (!vo.getCid().equals("")))user=userService.read(vo.getCid());
          ???if(user==null)user=userService.newUser();
          ???? if(user!=null)
          ???? {???? ?
          ???? ? vo.setCid(user.getCid());
          ???? ? vo.setUserName(user.getUserName());
          ???? ? vo.setPassword(user.getPassword());
          ???? ? vo.setTel(user.getTel());
          ???? ? vo.setEmail(user.getEmail());
          ???? ? vo.setIntro(user.getIntro());
          ???? ? vo.setBirthday(user.getBirthday());
          ???? ? return mapping.findForward("edit");
          ???? }
          ???? else
          ???? {
          ???? ?request.setAttribute("msg","找不到數(shù)據(jù)!");?
          ???? ?return doQuery(mapping,form,request,response);
          ???? }
          ?}
          ?public ActionForward doUpdate(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
          ?{
          ??IUser? obj=(IUser)form2Po(form);
          ???? if(userService.update(obj))
          ???? {
          ???? ?request.setAttribute("msg","數(shù)據(jù)修改成功!");
          ???? ?return doQuery(mapping,form,request,response);
          ???? }
          ???? else
          ???? {
          ???? ?request.setAttribute("msg","數(shù)據(jù)修改失敗");?
          ???? ?return mapping.getInputForward();
          ???? }
          ?}
          ?public ActionForward doAdd(ActionMapping mapping,
          ???ActionForm form,
          ???HttpServletRequest request,
          ???HttpServletResponse response)
          ?{
          ??IUser? obj=form2Po(form);
          ??if(obj==null)
          ??{
          ???request.setAttribute("msg","無法創(chuàng)建要保存的對象,添加失敗!");
          ???return mapping.getInputForward();
          ??}????????
          ???? if(userService.save(obj))
          ???? {
          ???? ?request.setAttribute("msg","數(shù)據(jù)添加成功!");
          ???? ?return doQuery(mapping,form,request,response);
          ???? }
          ???? else
          ???? {
          ???? ?request.setAttribute("msg","數(shù)據(jù)添加失敗");?
          ???? ?return mapping.getInputForward();
          ???? }???
          ?}
          ?public ActionForward doQuery(ActionMapping mapping,
          ???ActionForm form,
          ???HttpServletRequest request,
          ???HttpServletResponse response)
          ?{
          ??int currentPage=CommUtil.null2Int(request.getParameter("page"));
          ??int pageSize=CommUtil.null2Int(request.getParameter("pageSize"));
          ??if(currentPage<1)currentPage=1;
          ??if(pageSize<1)pageSize=15;
          ??String scope="1=1";
          ??Collection paras=new ArrayList();??
          ??String orderType=CommUtil.null2String(request.getParameter("orderType"));
          ??String orderField=CommUtil.null2String(request.getParameter("orderField"));??
          ??String userName=CommUtil.null2String(request.getParameter("queryUserName"));
          ??String tel=CommUtil.null2String(request.getParameter("queryTel"));????
          ??if(!userName.equals(""))
          ??{
          ???scope+=" and userName like ?";
          ???paras.add("%"+userName+"%");
          ??}
          ??if(!tel.equals(""))
          ??{
          ???scope+=" and tel like ?";
          ???paras.add("%"+tel+"%");
          ??}??
          ??if(orderField.equals(""))//默認(rèn)按用戶名排序
          ??{
          ??orderField="userName";
          ??orderType="desc";????
          ??}?
          ??if(!orderField.equals(""))
          ??{
          ??scope +=" order by "+orderField;
          ??if(!orderType.equals(""))scope+=" "+orderType;
          ??}?
          ??IPageList pList=new PageList(new ListQuery(userService.query(scope,paras)));
          ??pList.doList(pageSize,currentPage,"","");
          ??if(pList!=null){
          ???request.setAttribute("list",pList.getResult());
          ???request.setAttribute("pages",new Integer(pList.getPages()));
          ???request.setAttribute("rows",new Integer(pList.getRowCount()));
          ???request.setAttribute("page",new Integer(pList.getCurrentPage()));??
          ???request.setAttribute("gotoPageHTML",CommUtil.showPageHtml(pList.getCurrentPage(),pList.getPages()));
          ???}
          ??request.setAttribute("orderType",orderType);
          ??request.setAttribute("orderField",orderField);
          ??return mapping.findForward("list");
          ?}
          ?public ActionForward doDel(ActionMapping mapping,
          ???ActionForm form,
          ???HttpServletRequest request,
          ???HttpServletResponse response)
          ?{
          ??UserForm vo=(UserForm)form;????
          ??IUser user=null;
          ??if(vo.getCid()!=null && (!vo.getCid().equals("")))user=userService.read(vo.getCid());?
          ???? if(user!=null && userService.del(user))
          ???? {
          ???? ?request.setAttribute("msg","數(shù)據(jù)刪除成功!");???? ?
          ???? }
          ???? else
          ???? {
          ???? ?request.setAttribute("msg","數(shù)據(jù)修改失敗");?
          ???? }????
          ???? return doQuery(mapping,form,request,response);
          ?}
          ?public IUser form2Po(ActionForm form) {
          ???? UserForm vo=(UserForm)form;????
          ??IUser user=null;
          ??if(vo.getCid()!=null && (!vo.getCid().equals("")))user=userService.read(vo.getCid());
          ??if(user==null)user=userService.newUser();
          ??user.setUserName(vo.getUserName());
          ??user.setBirthday(vo.getBirthday());
          ??user.setEmail(vo.getEmail());
          ??user.setTel(vo.getTel());
          ??user.setPassword(vo.getPassword());
          ??user.setIntro(vo.getIntro());
          ??return user;
          ?}??
          }
            注意了,Action中使用的都是IUser,IUsrService等接口,其中execute方法中有一句有點(diǎn)不一樣:
          ?WebApplicationContext wac =WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServlet().getServletContext());
          ??this.userService = (IUserService) wac.getBean("userService");
            這就是傳說中強(qiáng)大的Spring IOC的使用。因?yàn)橛辛怂?我們的Web開發(fā)人員也不用知道這個IUserService究竟是什么東西了,有時候也無法知道。你想想,你在做這一步的時候,可能另外分工做后臺商業(yè)邏輯層的伙伴還沒有把IUserService的實(shí)現(xiàn)寫出一個來呢。Web開發(fā)人員需要用的IUserService的時候,直接通過類似的方法找Spring取就是了。Spring一個角色是扮演IOC容器中間件嘛,他都沒有誰還有呢?
            本例中有關(guān)com.easyjf.web.tools、com.easyjf.util等包只是使用了EasyJWeb的分頁業(yè)務(wù)引擎及一些實(shí)用工具,沒有其它功能,否則代碼就會更多了。大家可以下載EasyJWeb的源碼看到詳細(xì)實(shí)現(xiàn)方式。
          ?
          3、美工及設(shè)計(jì)制作人員做界面,完成后交由我們Web開發(fā)人員加JSP代碼。

          ?
            JSP部分主要有兩個頁面,userEdit.jsp主要是一個用戶信息錄入表單。userList.jsp是用戶信息列表頁面。這一部分大家一定都輕車熟路了吧,由于篇幅、頁面、版面等都有限,就不把詳細(xì)代碼扔出來污染環(huán)境了。請新手直接到EasyJF開源團(tuán)隊(duì)官網(wǎng)下載完整的示例代碼,其中包括了這兩個JSP頁面。下載地址:
            
            http://www.easyjf.com/html/bbs/20060509/1571738935251539.htm?ejid=1139563413901115
          ?
          4、配置Struts配置文件struts-config.xml

          struts-config.xml的內(nèi)容如下:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
          <struts-config>
          ? <data-sources />
          ? <form-beans >
          ? <form-bean name="userForm" type="com.easyjf.struts.form.UserForm" />
          ? </form-beans>
          ? <action-mappings >
          ??? <action attribute="userForm" input="/userEdit.jsp" name="userForm"
          ????? path="/userManage"? scope="request"
          ????? type="com.easyjf.struts.action.UserManageAction">
          ????? <forward? name="edit" path="/userEdit.jsp" />
          ????? <forward? name="list" path="/userList.jsp" />
          ??? </action>???
          ? </action-mappings>
          ? <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
          ??<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
          ?</plug-in>?
          </struts-config>
          ?
          5、最后還差一步,就是配置web.xml文件。

          Web.xml文件內(nèi)容如下
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app>
          ?<context-param>
          ??<param-name>contextConfigLocation</param-name>
          ??<param-value>/WEB-INF/applicationContext.xml</param-value>
          ?</context-param>?
          ?<listener>
          ??<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          ?</listener>?
          ?<servlet>
          ??<servlet-name>action</servlet-name>??<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
          ??<init-param>
          ???<param-name>config</param-name>
          ???<param-value>/WEB-INF/struts-config.xml</param-value>
          ??</init-param>???
          ?</servlet>
          ?<servlet-mapping>
          ??<servlet-name>action</servlet-name>
          ??<url-pattern>*.do</url-pattern>
          ?</servlet-mapping>
          </web-app>
            工作都完了,自己在檢查檢查,可以作一些單元測試等工作,并把接口用說明文檔寫好交給老大吧。

            下面我們再看看其它角色的表演!
             
            詳情請看:玩玩Spring之struts+hibernate+spring添刪改查示例(二)
          posted on 2006-05-12 23:52 船長 閱讀(127) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 洱源县| 永靖县| 碌曲县| 泾阳县| 龙海市| 和顺县| 柘荣县| 苍梧县| 竹山县| 无棣县| 公安县| 临海市| 徐水县| 九龙城区| 西和县| 新余市| 绩溪县| 陇川县| 东明县| 镇安县| 保康县| 永宁县| 花垣县| 连山| 东阳市| 郧西县| 特克斯县| 射阳县| 万山特区| 凤城市| 华容县| 兰坪| 五常市| 江源县| 建瓯市| 朝阳县| 宁武县| 东丽区| 永寿县| 东安县| 景德镇市|