posts - 495,comments - 227,trackbacks - 0
          <2006年8月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(46)

          隨筆分類(476)

          隨筆檔案(495)

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 1396814
          • 排名 - 16

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

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

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

          第一個(gè)是“用戶”這一實(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();//用戶簡(jiǎn)介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);
          }
          第二個(gè)是圍繞“用戶”這一實(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)建一個(gè)新的用戶
          ?public IUser read(String cid);//根據(jù)主鍵cid讀取一個(gè)用戶
          ?public List query(String scope,Collection paras);//用戶查詢
          ?public IUser readByName(String userName);//根據(jù)用戶名讀取一個(gè)用戶
          ?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ì)師還會(huì)根據(jù)系統(tǒng)需求給出一些具體的設(shè)計(jì)方法,對(duì)系統(tǒng)各層之間的接口規(guī)范,Action層使用的框架或模式等。他的工作就算完成了,下面就該程序員、數(shù)據(jù)庫(kù)管理員以及頁(yè)面設(shè)計(jì)人員等表演了。這些角色在我們實(shí)際項(xiàng)目中很多時(shí)候是同步進(jìn)行的,可謂花開(kāi)N朵。然而本文為了能讓新同學(xué)把這個(gè)示例能跑起來(lái),所以就簡(jiǎn)單分了一下步驟,以便新同學(xué)按步驟進(jìn)行。
          ?
          第二步:數(shù)據(jù)庫(kù)設(shè)計(jì)及建表(數(shù)據(jù)庫(kù)管理員)
            隨便選擇一個(gè)數(shù)據(jù)庫(kù)My SQL,MS SQL或Oracle均可,先建設(shè)一張表,表名叫User,字段如下:
          cid,代表主鍵,username-用戶名,password-密碼,email-電子郵件,tel-電話,birthday-生日,intro-簡(jiǎn)介,為了演示方便,我們?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框架寫(xiě)Web部份(Web開(kāi)發(fā)工程師)

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

          1、?先根據(jù)IUser接口,寫(xiě)一個(gè)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方法在此略過(guò)。
          }
          ?
          2、寫(xiě)Action

            根據(jù)用戶的需求說(shuō)明及設(shè)計(jì)師的相關(guān)模式或規(guī)范建議等,寫(xiě)一個(gè)處理“添刪改查”等操作的Action,本文全部寫(xiě)到一個(gè)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","無(wú)法創(chuàng)建要保存的對(duì)象,添加失敗!");
          ???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");
            這就是傳說(shuō)中強(qiáng)大的Spring IOC的使用。因?yàn)橛辛怂?我們的Web開(kāi)發(fā)人員也不用知道這個(gè)IUserService究竟是什么東西了,有時(shí)候也無(wú)法知道。你想想,你在做這一步的時(shí)候,可能另外分工做后臺(tái)商業(yè)邏輯層的伙伴還沒(méi)有把IUserService的實(shí)現(xiàn)寫(xiě)出一個(gè)來(lái)呢。Web開(kāi)發(fā)人員需要用的IUserService的時(shí)候,直接通過(guò)類似的方法找Spring取就是了。Spring一個(gè)角色是扮演IOC容器中間件嘛,他都沒(méi)有誰(shuí)還有呢?
            本例中有關(guān)com.easyjf.web.tools、com.easyjf.util等包只是使用了EasyJWeb的分頁(yè)業(yè)務(wù)引擎及一些實(shí)用工具,沒(méi)有其它功能,否則代碼就會(huì)更多了。大家可以下載EasyJWeb的源碼看到詳細(xì)實(shí)現(xiàn)方式。
          ?
          3、美工及設(shè)計(jì)制作人員做界面,完成后交由我們Web開(kāi)發(fā)人員加JSP代碼。

          ?
            JSP部分主要有兩個(gè)頁(yè)面,userEdit.jsp主要是一個(gè)用戶信息錄入表單。userList.jsp是用戶信息列表頁(yè)面。這一部分大家一定都輕車(chē)熟路了吧,由于篇幅、頁(yè)面、版面等都有限,就不把詳細(xì)代碼扔出來(lái)污染環(huán)境了。請(qǐng)新手直接到EasyJF開(kāi)源團(tuán)隊(duì)官網(wǎng)下載完整的示例代碼,其中包括了這兩個(gè)JSP頁(yè)面。下載地址:
            
            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" "<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>
            工作都完了,自己在檢查檢查,可以作一些單元測(cè)試等工作,并把接口用說(shuō)明文檔寫(xiě)好交給老大吧。

            下面我們?cè)倏纯雌渌巧谋硌荩?/div>
             
            詳情請(qǐng)看:玩玩Spring之struts+hibernate+spring添刪改查示例(二)
          posted on 2006-08-07 14:30 SIMONE 閱讀(263) 評(píng)論(0)  編輯  收藏 所屬分類: JSP
          主站蜘蛛池模板: 房产| 莱西市| 寿阳县| 韶山市| 合水县| 贵州省| 连江县| 长治县| 肥西县| 黔西县| 怀柔区| 东海县| 青岛市| 隆子县| 彩票| 威远县| 吐鲁番市| 尉氏县| 博野县| 顺昌县| 屯留县| 普洱| 遵化市| 叶城县| 九江县| 福建省| 汉源县| 格尔木市| 大渡口区| 通化市| 灵宝市| 巴南区| 沾化县| 大同县| 游戏| 融水| 治多县| 天柱县| 庆云县| 宜良县| 长乐市|