订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
JSP探讨

数据操作通用框架的使用

举例如下,以网上商店中的“商品”为数据对象,看看使用本框架系统是如何被方便地使用?

  3.1 数据建模:

  首先是数据建模,这是最重要的起点,确定商品的数据结构,以建立商品数据模型,如下:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Product implements Model {
  private String productId; //商品ID
  private String catId; //商品目录ID
  private String name; //商品名称
  private String description; //商品描述
  private Collection items = new ArrayList(); //商品细目
 
  public Product(){ }
  //构造方法
  public Product(String productId, String name, String description){
    this.productId = productId;
    this.name = name;
    this.description = description;
  }
 
  //set或get方法
  public String getProductId() { return productId; }
  public void setProductId(String productId) {
    this.productId = productId;
  }
  ……
}





  其次,建立商品数据表,如下:


1
2
3
4
5
6
7
CREATE TABLE product(
  productId char(10) NOT NULL,
  name varchar(100) DEFAULT '' , #产品名称
  imagePath varchar(200) DEFAULT '' , #图片
  description text DEFAULT '' , #详细描述 可html
  PRIMARY KEY (productId)
)TYPE=InnoDB; 




  3.2 EJB开发配置
  第一:创建Session Bean ProductManager,接口如下:
  
1
2
3
4
5
6
7
8
9
10
11
12
public interface ProductManagerLocal extends javax.ejb.EJBLocalObject {
    //商品数据增加
    public void createProduct(EventModel em) throws Exception; 
    //商品数据修改
    public void updateProduct(EventModel em) throws Exception;
    //商品数据删除
    public void deleteProduct(EventModel em) throws Exception;
    //商品查询
    public Product getProductById(String Id);
 
    。。。。
  }


   第二:EJB配置,如果使用JBuilder图形化开发,这个过程是自动的)

  3.3 Web模板式开发
   首选是开发商品数据模型的“处理器”,专门实现EJB和前台界面之间的操作传递,这些传递功能的代码可以拷贝,不同数据模型,这个处理器功能代码基本相同,主要完成下面两个功能:
  1. 根据主键从EJB中获得存在的数据模型。
  2. 接受用户界面的操作,调用EJB的的新增、修改、删除等功能。

  下面以Product模型为实例:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class ProductHandler implements ModelHandler {
  private final static String module = ProductHandler.class.getName();
 
  private final static ServiceServerFactory sf = ServiceServerFactory.getInstance();
  protected final static CacheFactory cacheFactory = CacheFactory.getInstance();
 
  //初始化ModelForm实例
  public ModelForm initForm(HttpServletRequest request) throws Exception{
    ProductForm form = new ProductForm();
    return form;
  }
 
   //通过EJB从数据库中根据主键获得商品数据
  public Model findModelByKey(String keyValue, HttpServletRequest request) throws   
   Exception{
    ProductManagerLocal productManager = (ProductManagerLocal) sf.getService(
     FrameworkServices.ProductEJB, request);
    return productManager.getProductById(keyValue);
  }


   //接受用户界面的数据操作
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void serviceAction(EventModel em, HttpServletRequest request) throws
  java.lang.Exception {
    ProductManagerLocal productManager = (ProductManagerLocal) sf.getService(
      FrameworkServices.ProductEJB, request);
    try {
      switch (em.getActionType()) {
        case Event.CREATE:
          productManager.createProduct(em);
          break;
        case Event.EDIT:
          productManager.updateProduct(em);
          break;
        case Event.DELETE:
          productManager.deleteProduct(em);
          break;
      }
    } catch (Exception ex) {
        throw new Exception("System operation Error:" + ex);
    }
  }





  程序开发完成后,需要在web.xml中配置对EJB的JNDI调用,如下:
  
1
2
3
4
5
6
7
<ejb-local-ref>
    <ejb-ref-name>ejb/ProductManager</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <local-home>com.jdon.estore.catalog.ProductManagerLocalHome</local-home>
    <local>com.jdon.estore.catalog.ProductManagerLocal</local>
    <ejb-link>ProductManager</ejb-link>
  </ejb-local-ref>


  以Item模型为实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class ItemHandler extends ModelHandler {
  private final static String module = ItemHandler.class.getName();
  private final static ServiceServerFactory sf = ServiceServerFactory.
    getInstance();
 
  public ModelForm initForm(HttpServletRequest request) throws Exception {
    ItemForm form = new ItemForm();
 
    String productId = request.getParameter("productId");
    if ( (productId == null) || (productId.length() == 0))
      throw new Exception("productId is null");
    form.setProductId(productId);
    return form;
  }
 
  public Model findModelByKey(String keyValue, HttpServletRequest request) throws
    Exception {
    ProductManagerLocal productManager = (ProductManagerLocal) sf.getService(
    FrameworkServices.ProductEJB, request);
    return productManager.getItemById(keyValue);
  }
 
  public void serviceAction(EventModel em, HttpServletRequest request) throws
  java.lang.Exception {
 
    ProductManagerLocal productManager = (ProductManagerLocal) sf.getService(
      FrameworkServices.ProductEJB, request);
 
    try {
        switch (em.getActionType()) {
        case Event.CREATE:
          productManager.createItem(em);
          break;
        case Event.EDIT:
          productManager.updateItem(em);
          break;
        case Event.DELETE:
          productManager.deleteItem(em);
          break;
        }
    } catch (Exception ex) {
        throw new Exception("System operation Error:" + ex);
    }
}
 
}






对比ItemHandler和ProductHandler,两者代码几乎一致,这种模板式开发减少了琐碎工作的差错,节省开发时间。无需遵循Struts标准实现很多Action类。



  3.4 Struts-config.xml和modelmapping.xml配置
  struts-config.xml配置为struts的标准配置文件,每个数据模型需要三行配置:
  1. ActionForm的配置
  2. 界面输出配置
  3. 接受界面输入
  
   这三行配置对于不同数据模型都是相同的,代码可以拷贝,不同的就是相应的ActionForm值。

1
2
3
4
5
6
7
<struts-config>
  <form-beans>
   <form-bean name="productForm" type="com.jdon.estore.web.catalog.ProductForm" />
 
   <form-bean name="productForm" type="com.jdon.estore.web.catalog.Product2Form" />
  </form-beans>
  <action-mappings>

      <!-- 本action实现界面输出,不同数据模型,可以拷贝这段代码,只要修改
      productForm为新的数据模型的ModelForm,以及Jsp文件名修改 -->
      
1
2
3
4
5
6
7
8
<action attribute="productForm" 
        type="com.jdon.strutsutil.ModelViewAction" 
        validate="false" 
        scope="request" 
        path="/admin/productAction">
      <forward name="create" path="/admin/product.jsp" />
      <forward name="edit" path="/admin/product.jsp" />
      </action>


      <!-- 本action接受界面输入,不同数据模型,可以拷贝这段代码,只要修改
      productForm为新的数据模型的ModelForm以及Jsp文件名修改 -->

      
1
2
3
4
5
6
7
8
9
10
<action name="productForm" 
        type="com.jdon.strutsutil.ModelSaveAction" 
        input="/admin/product.jsp" 
        scope="request" 
        path="/admin/saveProductAction">
      <forward name="success" path="/admin/productOk.jsp" />
      <forward name="failure" path="/admin/productOk.jsp" />
      </action>
  </action-mappings>
  </struts-config>



struts-config.xml的配置也是模板化,拷贝粘贴就可以,不同的是ActionForm和Jsp名称不一样。



  为了能使用Web层的增删改查框架,需要实现modelmapping.xml配置,如下:
  
1
2
3
4
5
6
<modelmappings>
    <modelmapping formName = "productForm"
            key="productId"
            model="com.jdon.estore.model.Product"
            handler = "com.jdon.estore.web.catalog.ProductHandler" />
  </modelmappings>


   其中的formName是struts-config.xml的ActionForm名称。
    key是数据模型的主键。
    Model是数据模型类,本类在前面已经列出。
    Handler是数据模型处理器,本类已经在前面列出。

  3.5 Jsp的编写
   Jsp是纯Html语法编写,适合非专业人员,如美工设计等。

   商品数据的增删改查界面是在一个JSP里实现,如product.jsp:
  
1
2
3
4
5
6
7
8
9
10
<html:form action="/admin/saveProductAction.do" method="POST" >
      <html:hidden property="action" />
      <!- - 本行是关键,表明是新增还是删除或修改 -- >
 
      <html:hidden property="productId"/>
      商品名称:<html:text property="name" size="20" />
      商品说明:<html:textarea rows="4" cols="32" property="description"/>
      <html:submit property="submit" value="保存"/>
      <html:reset value ="复位"/>
   </html:form>



优点
  1. 实际开发过程中,数据模型字段不断变化,使用本框架,只要轻松更改三次就可实现全部修改:数据库字段修改;Product数据模型字段修改;Jsp显示字段修改。这三种修改工作非常容易和轻松。

  2. 最大化省略了数据增删改查等琐碎开发工作,如果一个系统数据模型达到数百之多,使用本框架效率和质量就非常明显。

  3. 由于降低了琐碎工作,因此可以将更多精力集中在Jsp页面显示,客户要求的显示或打印格式可是五花八门,有本框架在后面做支撑,那些又苦又累的活变成了轻松、富有创造力的配置工作了。

  4. 可以快速开发出系统原型,由于数据增删改查是软件系统最基础的功能,使用本框架快速开发完成后,基本就完成了系统的原型,在短时间内能够给客户看到真正的互动的原型界面(不是Html做的假界面噢!),从而能真正摸清客户所需要,再也不怕客户反反复复地修改了。

  5. 客户需求变化有多快,我就有多快!

平均得分
(1 次评分)





文章来自: jdon
标签: 框架 EJB 数据操作 
评论: 7 | 查看次数: 802
  • 共有 7 条评论
游客 [2008-12-05 13:28:35]
游客 [2008-11-05 15:17:05]
cheap wow power leveling Beyond knowing your limitations. HDRO gold it's important to be able to size up opponents lecteur mp3 at a glance. level wow Knowing their class is the first step, understanding. lord of rings online gold your places in a rock-paper-scissors scenario. lotro gold More important, however, is the estimation .lotro gold of gear. mp3 As much as skill and class
balance . mp3 mp4 player plays a part in the outcome . mp3 mp4 player of combat, gear is a major differentiator that makes up for shortcomings in other areas. mp3 mp4 player In fact, with . mp3 player kaufen the introduction of Resilience, gear more than mp4 ever plays a more substantial part in PvP. power level In an Arena match, the very first thing we . power level scope out is gear. wow level Through quick tab-selection viewing .wow leveling of character portraits, we generally wow lvl have a good idea of the classes we're up against if they keep their helm graphic on. wow lvl If they are wearing Season 3 shoulders, then we know . wow lvl 60 their relative experience. wow lvl 70 This is why the visual i.wow power leveling mpact of Arena shoulders is so important.wow powerlevel It immediately gives you a general idea of how tough the match will be. wow powerlevel Players in full S3 will likely have over 10k hp and over. wow powerleveling 400 Resilience, depending on the class and spec. A full S3 SL/SL Warlock, for example, will easily have. about 12-13k hp and over 400 Resilience. Identifying weapons is slightly more . difficult but will also give a general idea of an enemy's strength. Season 1 and 2 weapons share the same graphics, so it's harder to identify. Season 3 weapons, on the other hand, are distinctive and share models with Black Temple and Mount Hyjal weapons. which 最新免费网络游戏 have relatively the same power. A review of Brutal Gladiator weapons will come in handy because these will be the most common way to identify opponents of relative skill. With the new mechanics. in place for Arenas, Season 4 will more or less weed out the chaff from the grain.
游客 [2008-11-05 15:13:06]
4GB MP3 PLAYER My wife is a shrewd little fox. Bluetooth Headset She knows just how much I love the fact. Bluetooth Headsets that she plays the game with me so sometimes, when we have our little domestic arguments, she makes sure. cell phone accessories to cancel her WoW account just to drive home a point. des po wow Of course, it doesn't mean much since we're both paid up for the next few months, but the message is clear -- "we make up (or you see things my way). digital camcorder or I'm quitting the game!" Of course, we don't reconcile merely because I'll be losing my . digital camcorders favorite playing partner, but I have to confess that it doesn't make . dvd players me happy one bit. free online games For parents, World of Warcraft can be a . free online war games useful bargaining chip for their kids with the parental . gold wow controls feature. gold wow It's easy enough to control WoW time if kids aren't doing their homework, floundering in school, or simply not. mp3 mp4 player doing their chores. mp3 player Conversely, a friend . mp3 player accessories of mine gave his son a WoW subscription when . mp3 player accessory he did well in school. mp3 player kaufen World of Warcraft can be so. online games much fun and addicting that it's . play war games often used as a social tool, and it's often upsetting when our. po wow friends quit playing the game. portable dvd player How many of us have . wow europe had friends whose significant others have "allowed" them to. wow geld play the game after, say, a wonderful date. wow gold verkaufen I'm not sure if it only applies to me, but . wow level service because I play the game with many of my RL friends . wow leveling service and my family, I use the lure of WoW . to full effect. I once had my brother do a specific task for . the promise of an upgrade to The Burning Crusade. A little before he finished what I asked him to do, I secretly upgraded his account. so he could免费网络游戏 finally make his Blood Elf Priest. Kind of manipulative, I know, but we did end up having . a lot of fun leveling our alts together. How about you. How much a part of your life is WoW and has it .最新网游 ever been used as a bargaining . chip in your social life.
游客 [2008-11-05 15:12:13]
mp3 8GB MP3 PLAYER A couple of people . apple ipod have posted about the Shattered Sun Peacekeepers slacking off on their jobs lately. buy cheap wow gold quite possibly thanks . canon digital camera to something Blizzard fed them . cheap world of warcraft gold in PatchIn their reports, they complain about Peacekeepers. digital camera attacking them for no reason, sometimes not even in retaliation for attacking (or defending yourself against). digital cameras a member of the opposing faction. dvd player I can actually empathize with this as. eve isk I encountered the ill-placed wrath of the Peacekeepers myself when I rezzed my wife's toon in front of . ipod the Staging Area. ipod nano Without having done anything other. ipod shuffle than rezz, the Peacekeepers promptly charged . ipod touch and made short work of me. ipods In my experience, I have found 网游 that the Peacekeepers around . mp3 the Shattered Sun Staging Area have . mp3 player been slacking off. mp3 players In fact, my wife's toon was ganked right in front . mp4 of the building and the so-called Peacekeepers . portable dvd players did absolutely nothing. world of warcraft buy gold Sensing a bug, my wife . wow wrote a ticket and got a somewhat rude e-mail response saying that -- you guessed it -- it was working as intended. wow gold An Alliance . wow gold guild on my server seemed to be aware of the fact and exploited it to full effect, killing solo players who could seek no refuge under the apathetic -- or even hostile -- Peacekeepers. wow leveling According to reports, it's a known bug -- one player even . [url=http://www.gamesavo
游客 [2008-11-05 15:07:23]
2GB MP3 PLAYER Remember the WoW account hacking. 4GB MP3 PLAYER If you're a WoW player. buy wow gold you couldn't have missed . buying gold world of warcraft any of the articles posting this issue. cell phones Well, as it turns out, an article posted by . phones cell grimwell says that someone might have found the problem behind all the . cheap cell phones account hacking, stealing and selling even 5 year's worth items. cheap wow gold A piece of the BBC news article says:"Analysis [. cheap wow gold cheap wow gold cheapest wow gold ] showed that. eve isk it lay dormant on a victims . mp3 players machine until . portable mp3 player they ran World of Warcraft (WoW) at which point it captured login data and sent it to. portable mp3 players the hacking group. sell wow gold The group's enthusiastic use of the cursor flaw suggests it is trying to . world of warcraft gold do the same again. wow The online fantasy game . wow gold now has more than eight million active players around the world. wow gold Research by security firm Symantec . wow gold suggests that the raw value of a WoW account is now higher. wow gold than a credit card and its associated verification data. wow gold "Normally, as a WoW player, you'd know about the risks involved when creating an online account, yet some were . wow gold lazy enough not to check on their accounts every once in a while, thus resulting in their items . wow gold kaufen and WoW currency being stolen. wow gold kaufen Everyone knows that . stuff like that happen, some. even managed to hack the Superbowl website and use it . to host code for spyware, so monitor the hell out of your computer!In an article I wrote yesterday. about hackers seeing 游戏 console users as the new target, there is a comment 魔兽 coming from Stefana Muller.
游客 [2008-09-08 16:46:31]
游客 [2008-01-08 16:12:51]
up
  • 共有 7 条评论
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启