ZK 初用感受

   还是半年以前,我们的经理介绍了ZK项目,那时候我还什么都不是很清楚,只是感觉这个东西比较漂亮,组件也比较多,官网也有用例,所以就稍微留了点心。

   最近一个星期才开始搞,写了一个用户的注册功能,在这过程中发现其实页面布局挺费时的,代码倒是挺好调试的。虽然代码和标记在一个文件中(可以用MVC模式),但是毕竟是分开了,看起来还是比较清爽的。

  在测试的过程发现了两个问题,不知道究竟问题出在哪里,现在正在找..................

1.当textbox设置了constraints,后台又通过其id 设置它的值为空时

2.第二个问题是这样的,用户在填完注册信息后,点击注册按钮(我把注册成功后转向的语句注释掉了,但是页面中有检查用户重复功能)后,页面一直停留在当前,当多次点该按钮,容易出现不可预知的问题,有时候是数据库中没有插入相应数据,有时候是数据库不一致.....比较郁闷。还是出代码吧,大家可以试试看:

1.Package:

    com.zk.finacial.dbservice //提供ibatis 的实例sqlMap

      ....DBService.java;

------------------------------------------------------------------------------

package com.zk.finacial.dbservice;
import java.io.Reader;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class DBService {
  private static  SqlMapClient sqlMap;
  static{
   try{
    String resource="com/zk/finacial/dbservice/SqlMapConfig.xml";
    Reader reader=Resources.getResourceAsReader(resource);
    sqlMap=SqlMapClientBuilder.buildSqlMapClient(reader);
   }catch(Exception e)
   {
    e.printStackTrace();
    throw new RuntimeException("Error Initializing MyAppSqlConfig class.Cause :"+e);
   
   }
  }
  public static SqlMapClient getSqlMapInstance(){
   return sqlMap;
  }
}
--------------------------------------------------------------------

      ....Sql.xml

---------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap namespace="Sql">

 <select id="getUserbyName"
         parameterClass="java.lang.String"
         resultClass="java.util.HashMap">
         SELECT USERID,EMAIL,USERPS FROM USER WHERE USERNM=#username#
 </select>
 
 <select id="getCountByName"
         parameterClass="java.lang.String"
         resultClass="java.lang.Integer">
        SELECT COUNT(*) CNT FROM USER WHERE USERNM=#USERNAME#
 </select>
 
  <select id="getCountByEmail"
         parameterClass="java.lang.String"
         resultClass="java.lang.Integer">
        SELECT COUNT(*) CNT FROM USER WHERE Email=#EMAIL#
 </select>
 
  <select id="getCountByEmail_Name"
         parameterClass="java.util.HashMap"
         resultClass="java.lang.Integer">
        SELECT COUNT(*) CNT FROM USER WHERE Email=#EMAIL#  AND USERNM=#USERNAME#;
 </select>
 
 <insert  id="createNewUser" parameterClass="java.util.HashMap" >
     INSERT INTO USER(USERNM,EMAIL,USERPS,ACTIVE,CREATE_DATE) VALUES
      (#USERNAME#,#EMAIL#,#USERPS#,#ACTIVE#,#CREATE_DATE#)   
 </insert>
</sqlMap>         

-----------------------------------------------------------------------------------------------------------

      ....SqlMapConfig.xml

-----------------------------------------------------------------------------

   <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings
    cacheModelsEnabled="true"
    enhancementEnabled="true"
    lazyLoadingEnabled="true"
    maxRequests="32"
    maxSessions="10"
    maxTransactions="5"
    useStatementNamespaces="false"
/>
 <!--
<transactionManager type="JDBC" >
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
      <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost/test"/>
      <property name="JDBC.Username" value="test"/>
      <property name="JDBC.Password" value="test"/>
    </dataSource>
</transactionManager>
-->

<transactionManager type="JDBC" >
<dataSource type="JNDI">
<property name="DataSource"
value="java:comp/env/jdbc/ZK_Finacial"/>
</dataSource>
</transactionManager>
 
<sqlMap resource="com/zk/finacial/dbservice/Sql.xml" />
</sqlMapConfig>
----------------------------------------------------------------------------------------------------

 

 

2.DB Table:user

    sql:

---------------------------------------------------------------------------------------------

  DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `userId` int(11) NOT NULL auto_increment,
  `email` varchar(30) NOT NULL,
  `userNm` varchar(20) NOT NULL,
  `userPs` varchar(20) NOT NULL,
  `active` varchar(4) NOT NULL,
  `create_date` datetime NOT NULL,
  PRIMARY KEY  (`userId`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

---------------------------------------------------------------------------------------

 

 

3.Page :regist.zul

-------------------------------------------------------------------------------------

  <?page title="Regist as a new user." contentType="text/html;charset=UTF-8"?>
<zk>
 <zscript>
<![CDATA[
 import org.zkoss.zul.Captcha;
 import com.zk.finacial.dbservice.*;
 import com.ibatis.sqlmap.client.SqlMapClient;
 import java.lang.Integer;
 import java.util.Date;
 import java.text.*;
 import com.zk.finacial.entity.*;
 //Functions
 public void checkIsUsed(String componentValue, String componentName,
   String targetId, String DisplayID) {
  //here to check whether the name has been used. 
  SqlMapClient sqlMap = DBService.getSqlMapInstance();

  if (componentName.equals("Email")) {
   try {
    Integer count = (Integer) sqlMap.queryForObject(
      "getCountByEmail", componentValue);
    if (0 < count.intValue()) {
     Label lb = (Label) regist_win.getFellow(DisplayID);
     lb.setValue(componentValue + " already exist.");
     lb.setStyle("color: red");
     Textbox tb = (Textbox) regist_win.getFellow(targetId);
     tb.setStyle("color: red");
     tb.setFocus(true);
    } else {
     Label lb = (Label) regist_win.getFellow(DisplayID);
     lb.setValue("");
     Textbox tb = (Textbox) regist_win.getFellow(targetId);
     tb.setStyle("color: black");
    }

   } catch (Exception e) {
    e.printStackTrace();
   }

  }
  if (componentName.equals("Name")) {
   try {
    Integer count = (Integer) sqlMap.queryForObject(
      "getCountByName", componentValue);
    if (0 < count.intValue()) {
     Label lb = (Label) regist_win.getFellow(DisplayID);
     lb.setValue(componentValue + " already exist.");
     lb.setStyle("color: red");
     Textbox tb = (Textbox) regist_win.getFellow(targetId);
     tb.setStyle("color: red");
     tb.setFocus(true);
    } else {
     Textbox tb = (Textbox) regist_win.getFellow(targetId);
     tb.setStyle("color: black");
     Label lb = (Label) regist_win.getFellow(DisplayID);
     lb.setValue("");
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }

 }
 public void captcha_change() {
  Captcha cap = (Captcha) regist_win.getFellow("cap");
  String randomValue = (String) cap.randomValue();
  cap.setValue(randomValue);
 }
 public void registNow() {
  Textbox tbUserName = (Textbox) regist_win.getFellow("userName");
  Textbox tbUserEmail = (Textbox) regist_win.getFellow("userEmail");
  Textbox tbPassWord = (Textbox) regist_win.getFellow("passWord");
  Textbox tbRePassWord = (Textbox) regist_win.getFellow("rePassWord");
  Textbox tbRandomValue = (Textbox) regist_win.getFellow("random_value");
  Captcha cpt = (Captcha) regist_win.getFellow("cap");

  if ("".equals(tbUserName.getValue())) {
   alert("Please input userName");
   tbUserName.setFocus(true);
   return;
  }

  if ("".equals(tbUserEmail.getValue())) {
   alert("Pease input Email");
   tbUserEmail.setFocus(true);
   return;
  }

  if ("".equals(tbPassWord.getValue())) {
   alert("Pease input passWord");
   tbPassWord.setFocus(true);
   return;
  }

  if ("".equals(tbRePassWord.getValue())) {
   alert("Pease input repassword");
   tbRePassWord.setFocus(true);
   return;
  }
  if (!tbRePassWord.getValue().toString().equals(
    tbPassWord.getValue().toString())) {
   alert("Pease input repassword not equals password,pease reinput.");
   tbRePassWord.setValue("");
   tbRePassWord.setFocus(true);
   return;
  }
  if ("".equals(tbRandomValue.getValue())) {
   alert("Pease input randomValue.");
   cpt.setValue(cpt.randomValue());
   tbRandomValue.setFocus(true);
   return;
  } else {
   if (!cpt.getValue().toString().equalsIgnoreCase(
     tbRandomValue.getValue().toString())) {
    alert("validate number is incorrect.please re-input.");
    cpt.setValue(cpt.randomValue());
    return;
   }
  }

  //pass validate and insert information into DB
  Date dt = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  //System.out.println(sdf.format(dt));
  try {
   SqlMapClient sqlMap = DBService.getSqlMapInstance();
   Integer cntE=(Integer)sqlMap.queryForObject("getCountByEmail",tbUserEmail.getValue());
   Integer cntN=(Integer)sqlMap.queryForObject("getCountByName",tbUserName.getValue());
   //System.out.println(cnt);
   if(0<cntN.intValue())
   {
    alert("User already Exist.");
    //tbUserName.setValue("");
    tbUserName.setFocus(true);      
    return ;
   }
   if(0<cntE.intValue())
   {
    alert("Email already Exist.");
    //tbUserEmail.setValue("");
    tbUserEmail.setFocus(true);    
    return ;
   }
   
    HashMap hm = new HashMap();  
    hm.put("USERNAME", tbUserName.getValue());
    hm.put("EMAIL", tbUserEmail.getValue());
    hm.put("USERNAME", tbUserName.getValue());
    hm.put("EMAIL", tbUserEmail.getValue());
    hm.put("USERPS", tbPassWord.getValue());
    hm.put("ACTIVE", "Y");
    hm.put("CREATE_DATE", sdf.format(dt));
    
    sqlMap.startTransaction();
    sqlMap.insert("createNewUser",hm);    
    sqlMap.commitTransaction();
    alert("Regist successfully!");    
    User user=new User();
    user.setEmail(tbUserEmail.getValue());
    user.setUserNm(tbUserName.getValue());
    session.setAttribute("USER",user);
     //Executions.sendRedirect("/welcome.zul");
    //if above code is used,there would be no problems.
   
    
   
  } catch (Exception e) {
   e.printStackTrace();
  }

 }
]]>
</zscript>

 <div align="center">
  <vbox height="730px" align="center">
   <window width="1000px" height="80px"
    style="background:#aaeeff" minimizable="false" border="normal">
   </window>
   <window title="see you" border="normal" width="1000px"
    id="regist_win" height="650px">
    <columnlayout>
     <columnchildren width="362px">
      <panel>
       <panelchildren>
        <window height="600px" width="350px"
         border="normal">
        </window>
       </panelchildren>
      </panel>
     </columnchildren>
     <columnchildren width="630px">
      <panel>
       <panelchildren>
        <div align="center" width="600px">
         <portallayout>
          <portalchildren width="590px">
           <panel title="panel1"
            height="350px" border="normal" collapsible="true">
            <panelchildren>
             <div width="590px"
              align="center">
              <vbox>
               <separator />
               <separator />

               <grid>
                <columns>
                 <column
                  align="right" width="150px" />
                 <column
                  width="200px" />
                 <column
                  width="100px" />

                </columns>
                <rows>
                 <row
                  height="40px">
                  用户名:
                  <textbox
                   width="180px" height="20px" id="userName" name="Name"
                   onBlur="checkIsUsed(userName.getValue().toString(),self.getName().toString(),self.getId().toString(),userNameLabel.getId().toString())"
                   constraint="no empty:Please input username." />

                  <label
                   id="userNameLabel" />
                 </row>

                 <row
                  height="40px">
                  邮
                  箱:
                  <textbox
                   width="180px" height="20px" id="userEmail"
                   name="Email"
                   onBlur="checkIsUsed(userEmail.getValue().toString(),self.getName().toString(),self.getId().toString(),userEmailLabel.getId().toString())"
                   constraint="/.+@.+/.[a-z]+/: Please enter an e-mail address" />
                  <label
                   id="userEmailLabel" />
                 </row>

                 <row
                  height="40px">
                  密
                  码:
                  <textbox
                   width="180px" height="20px" id="passWord"
                   type="password"
                   constraint="no empty:please input pass word." />
                  <label
                   id="passWordLabel" />
                 </row>

                 <row
                  height="40px">
                  再次输入密码:
                  <textbox
                   width="180px" height="20px" id="rePassWord"
                   type="password"
                   constraint="no empty:please input pass word again." />
                  <label
                   id="rePassWordLabel" />
                 </row>

                 <row
                  height="40px">
                  请输入图中的字母:
                  <hbox
                   align="center">
                   <textbox
                    width="80px" height="20px" id="random_value" />
                   <captcha
                    id="cap" length="4" width="100px" height="35px" />
                  </hbox>
                  <button
                   label="看不清,换一张" onClick="captcha_change()" />
                 </row>

                 <row
                  spans="3" align="center" height="40px">
                  <vbox
                   width="100%" align="center">
                   <button
                    label="现在注册" onClick="registNow()">
                   </button>
                  </vbox>
                 </row>

                </rows>
               </grid>
              </vbox>
             </div>
            </panelchildren>
           </panel>
           <panel title="panel2"
            height="250px" border="normal" collapsible="true">
            <panelchildren>
             <div
              width="590px">
             </div>
            </panelchildren>
           </panel>
          </portalchildren>
         </portallayout>
        </div>
       </panelchildren>
      </panel>
     </columnchildren>
    </columnlayout>

   </window>
  </vbox>
 </div>
</zk>

--------------------------------------------------------------------------------

4.tomcat JNDI:

---------------------------------------

    <Resource name="jdbc/ZK_Finacial"
           auth="Container"
     type="javax.sql.DataSource"
              maxActive="100"
     maxIdle="30"
     maxWait="10000"
     username="test"
     password="test"
     driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost/finacial"/>
   <ResourceParams name="jdbc/ZK_Finacial"/>
    ===========================================

5.配置需要:

    1.ibatis 相关jar

    2.zk相关jar

    3.mysql-connection .jar

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值