2008年10月29日 星期三

Dynamic double combo implementation by jQuery, JSON and Java

About the "Dynamic Double Combo", I wrote it before by using DWR. And now I don't think using DWR is a good idea. If your project is combined by frameworks, like Struts, Spring ...etc. To maintain the all xml configuration files will cost lots of time.

In this example, I use jQuery, and the jQuery plugin jQuery - Select box manipulation, JSONObject, and the basic Java Servlet.

In web.xml, add these period of codes.


HelloServlet
HelloServlet


HelloServlet
/Hello



About the JSP file, remember to make the encoding consistent with the servlet.
And we have to include two javascript files, "jquery-1.2.6.min.js" and "jquery.selectboxes.pack.js". Please ignore the ".".

<%@ page language="java" pageEncoding="UTF-8"%>

< html>
< head>
< title>Dynamic double combo< /title>
< meta http-equiv=content-type content="text/html; charset=UTF-8">
< script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.3.2.min.js">< /script>
< script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.selectboxes.pack.js">< /script>
< script type="text/javascript">
$(document).ready(function(){
$("#select1").change(function(){
$("#select2").empty();
$("#select2").addOption("0","------");
$("#select2").ajaxAddOption("<%=request.getContextPath()%>/Hello",
{selVal:$("select[name='select1'] option[selected]").val()},false);
});
});
< /script>
< /head>
< body>
< select id="select1" name="select1">
< option value="A">A< /option>
< option value="B">B< /option>
< option value="C">C< /option>
< /select>
< select id="select2" name="select2">
< option value="0"> --- < /option>
< /select>
< /body>
< /html>


HelloServlet.java

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;

public class HelloServlet extends HttpServlet{

public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{

try{
String val = request.getParameter("selVal");
JSONObject obj = new JSONObject();
if(val.equals("A")){
obj.put("A-1", "A-1");
}else if(val.equals("B")){
obj.put("B-1", "B-1");
}else if(val.equals("C")){
obj.put("C-1", "C-1");
}
response.setContentType("text/html;charset=UTF-8");
response.getWriter().print(obj.toString());
return;
}catch(Exception e){
e.printStackTrace();
}
}
}

2008年10月25日 星期六

「海角七號」觀後感

電影剛出時,實在是沒什麼感覺,印象中的國片,不是沒內容,就是高深到看不懂。

不過隨著口碑傳出,一面倒的讚揚聲響起,票房屢創新高,卻激起了我的好奇心,到底是什麼片子啊,誇張的是,連網路論壇都禁止會員分享,這下可好,百年難得一見,就當是我湊熱鬧,三個星期前的星期五就去信義威秀排隊買票,我下午六點到售票窗口,想買七點的場次,小姐居然跟我說,九點四十才有座位,我當場傻眼,雖然很不情願,還是放棄,不過心裏默想,你好樣的,老子看賣座洋片都沒遇到過這種情況。

三個禮拜後又去威秀,看來人潮已逐漸散去,因為沒看過的人在台灣已算是少數了。

電影要賣座,媒體的強勢露出已不是萬靈丹,口耳相傳類似老鼠會的感染,才是會造成票房的一飛沖天。

來談談電影的內容吧,"真實"是我第一個感想,很貼近生活,感覺就像是發生在週遭的故事,"驚喜",一般的市井小民,像是警察、賣酒的業務甚至是修機車的黑手,還有不可不提的郵差國寶,居然都是道道地地的音樂人。

我最感動的一幕,是在演唱海角七號(國境之南)的時候,中間阿嘉停了下來,勞馬將孔雀之珠給了友子,她戴上時,眼中泛著淚光,那種眼神,就是訴說著"我愛你",此幕的穿透力極盡強勁之能事,我想我深深地愛上友子了。

2008年10月17日 星期五

jQuery plugin : Taiwanese ID Checker 身分證字號檢查器


/*
Program: Taiwanese ID Checker
Author: Austin, Cheng-Chun Chang
Blog: http://austinccc.blogspot.com
*/
jQuery.fn.checkId = function(){
var moduleCnt = 10;
var inputId = $.trim($(this).val()).toUpperCase();
if(inputId.length != moduleCnt){ //If the length is not 10, return false
return false;
}else{
var str = new Array(moduleCnt);
switch(inputId.substring(0,1)){
case 'A':
str[0] = "1";
str[1] = "0";
break;
case 'B':
str[0] = "1";
str[1] = "1";
break;
case 'C':
str[0] = "1";
str[1] = "2";
break;
case 'D':
str[0] = "1";
str[1] = "3";
break;
case 'E':
str[0] = "1";
str[1] = "4";
break;
case 'F':
str[0] = "1";
str[1] = "5";
break;
case 'G':
str[0] = "1";
str[1] = "6";
break;
case 'H':
str[0] = "1";
str[1] = "7";
break;
case 'I':
str[0] = "3";
str[1] = "4";
break;
case 'J':
str[0] = "1";
str[1] = "8";
break;
case 'K':
str[0] = "1";
str[1] = "9";
break;
case 'L':
str[0] = "2";
str[1] = "0";
break;
case 'M':
str[0] = "2";
str[1] = "1";
break;
case 'N':
str[0] = "2";
str[1] = "2";
break;
case 'O':
str[0] = "3";
str[1] = "5";
break;
case 'P':
str[0] = "2";
str[1] = "3";
break;
case 'Q':
str[0] = "2";
str[1] = "4";
break;
case 'R':
str[0] = "2";
str[1] = "5";
break;
case 'S':
str[0] = "2";
str[1] = "6";
break;
case 'T':
str[0] = "2";
str[1] = "7";
break;
case 'U':
str[0] = "2";
str[1] = "8";
break;
case 'V':
str[0] = "2";
str[1] = "9";
break;
case 'W':
str[0] = "3";
str[1] = "2";
break;
case 'X':
str[0] = "3";
str[1] = "0";
break;
case 'Y':
str[0] = "3";
str[1] = "1";
break;
case 'Z':
str[0] = "3";
str[1] = "3";
break;
default:
//not characters, return false
return false;
}
for(var i = 2; i < moduleCnt; i++){
str[i] = inputId.substring(i-1,i);
}
var sum = parseInt(str[0]) * 1;
for(var i = 9; i > 0; i--){
sum = sum + (i * parseInt(str[moduleCnt - i]));
}
if((moduleCnt - (sum % moduleCnt)) == parseInt(inputId.substring(moduleCnt-1,moduleCnt)) ||
(sum % moduleCnt) == parseInt(inputId.substring(moduleCnt - 1, moduleCnt))){
return true;
}else{
return false;
}
}
}

How to use:

$(document).ready(function(){
$("#hi").click(function(){
if($("#custId").checkId()){
alert("ID is correct!!");
}else{
alert("ID is NOT Correct!!");
}
});
});

If you would like to understand the rule, you could visit
身份證字號規則

2008年9月17日 星期三

jQuery Tips


  • select


  • //Empty options
    $("#select1").empty();

    //Get Text
    $("select[name='select1'] option[selected]").text(); //1.3 +
    $("select[@name='select1'] option[@selected]").text(); //1.2.6 -
    $("#select1 :selected").text();
    $("#select1").text(); //This will get all the options

    //Get Value
    $("select[name='select1'] option[selected]").val(); //1.3 +
    $("select[name='@select1'] option[@selected]").val(); //1.2.6-
    $("#select1").val();
    $("#select1 :selected").val();

    //Set Value -> By value (If value is 0)
    $("#select1").attr("value","0");

    //Set Value -> By index -> the second element
    $("#select1")[0].selectedIndex = 1;

    //Count the options of select
    alert($("select[name='select1'] option").length); //1.3 +
    alert($("select[@name='select1'] option").length); //1.2.6 -


  • radio button


  • //Get Value
    $("input[name='radio1'][checked]").val(); //1.3 +
    $("input[name='@radio1'][@checked]").val(); //1.2.6 -

    //Set Value -> By value (If value is radio_0)
    $.each($("input[name='radio1']"),function(){ //1.2.6 - => @name
    if($(this).val() == "radio_0"){
    $(this).get(0).checked = true;
    }
    });

    //Set Value -> By index -> the second element
    $("input[name='radio1']").get(1).checked = true; //1.2.6 - => @name


  • checkbox


  • //select All
    $(":checkbox[name='checkbox_1']").attr("checked","checked"); //1.2.6 - => @name

    $.each($("input[name='checkbox_1']"),function(){ //1.2.6 - => @name
    $(this).attr("checked",true);
    });

    //unselect All
    $(":checkbox[name='checkbox_1']").attr("checked",false); //1.2.6 - => @name

    $.each($("input[name='checkbox_1']"),function(){ //1.2.6 - => @name
    $(this).attr("checked",false);
    });

2008年3月4日 星期二

恐懼戰馬頒獎典禮

現在頒發恐懼戰馬獎。
入圍的有:時間迴旋齊克果阿盾
得獎的是...時間迴旋
啪啪啪,啪啪,啪啪啪啪.....
現在請時間迴旋上台領獎...

首先,我要感謝父母的不小心,女朋友的貼心,以及<光明頂>的熱心。
曾幾何時,就在我接到術士任務那一剎那,那是我人生中最黑暗的時刻,在虛擬世界中,由解任務一點一滴累積的自信,正慢慢流失,我一度想放棄,不過想起曖昧同事說的,「只要逃一次,就會一直逃下去」,經過了足足兩天的沉澱,我重新拿起滑鼠,再次開啟讓人又愛又恨的WOW,先在拍賣場備齊所有材料,接著是蓮皇的鼎力相助,紅血之月的鼓勵,齊克果資金的挹注,還有隊友,哇哈哈大王夢語骨牌隊長,願意帶我連闖兩個副本,以暴力的打法殺遍所有敵人,比起藍波IV是有過之而無不及,在解完任務當晚,腦中縈繞著通靈、西厄,以及廝殺後的快感,躺在床上,輾轉反側無法入眠,我想是興奮與感激使然吧。

感謝<光明頂>,感謝巴哈!!!

2008年2月23日 星期六

Java & MySQL on Fedora 8


  • Fedora 8

  • Check the default JRE environment,

    [austin@localhost ~]$ java -version

    If the version is 1.7.0, remove it.

    [root@localhost ~]$ rpm -qa | grep java
    java-1.5.0-gcj-1.5.0.0-l7.fc8
    java-1.7.0-icedtea-1.7.0.0-0.l9.b21-snapshot.fc8

    [root@localhost ~]$ rpm -e java-1.7.0-icedtea-1.7.0.0

  • Java

  • Go to Sun's website, you could find two linux jdk files:

    • jdk-1_5_0_14-linux-i586-rpm.bin

    • jdk-1_5_0_14-linux-i586.bin

    • For installing one of above,

      [austin@localhost ~]$ chmod +x jdk-1_5_0_14-linux-i586~.bin
      [austin@localhost ~]$ ./jdk-1_5_0_14-linux-i586~.bin

      If you install non-rpm file, just follow the instructions. The jdk folder will install on your current location.
      On the other hand, if you choose the rpm file, you will get jdk-1.5.0_14-linux-i586.rpm.

      [root@localhost ~]$ rpm -ivh jdk-15.0_14-linux-i568.rpm


      The JDK will install on /usr/java/jdk1.5.0_14


      Next, we will edit the environment variables.

      [austin@localhost ~]$ vi .bashrc


      In .bashrc, increase three lines below:


      export JAVA_HOME=/usr/java/jdk1.5.0_14
      export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
      export PATH=$PATH:$JAVA_HOME/bin\


      Save it and re-login.

      [austin@localhost ~]$ export | more


      Check if there are "JAVA_HOME", "CLASSPATH" and "PATH" existed.


  • Eclipse

  • Download the eclipse-jee-europa-fall2-linux-gtk.tar.gz from Eclipse official Site. Just extract it and execute eclipse/eclipse.

  • MyEclipse

  • Download MyEclipse_6_0_1GA_E3_3_1_Installer.bin from MyEclipse.


    [root@localhost ~]$ chmod +x MyEclipse_6_0_1GA_E3_3_1_Installer.bin
    [root@localhost ~]$ ./MyEclipse_6_0_1GA_E3_3_1_Installer.bin


    And then error occured.

    java: xcb_xlib.c:50: xcb_xlib_unlock: Assertion `c->xlib.lock' failed
    Aborted.

    To solve it,

    [root@localhost ~]$ yum --enablerepo=development update libxcb
    [root@localhost ~]$ export LIBXCB_ALLOW_SLOPPY_LOCK=true
    [root@localhost ~]$ ./MyEclipse_6_0_1GA_E3_3_1_Installer.bin



  • MySQL


    1. mysql-libs-5.0.45-4.fc8.i386.rpm -> Fedora 8 DVD

    2. perl-DBI-1.58-2.fc8.i368.rpm -> Fedora 8 DVD

    3. mysql-5.0.45-4.fc8.i386.rpm -> Fedora 8 DVD

    4. perl-DBD-MySQL-4.005-2.fc8.i386.rpm -> Fedora 8 DVD

    5. mysql-server-5.0.45-4.fc8.i368.rpm -> Fedora 8 DVD



    [root@localhost ~]$ /etc/rc.d/init.d/mysqld start

    We could use "setup" to config system services and let mysql start as booting.

    [root@localhost ~]$ setup

    Download mysql-gui-tools-5.0r12-fc5-i386.tar.gz from MySQL.com
    Extract it.

    1. mysql-gui-tools-5.0r12-1fc5.i386.rpm
    2. mysql-administrator-5.0r12-1fc5.i386.rpm
    3. gtkhtml3-3.16.1-1.fc8.rpm -> from rpm.pbone.net
    4. mysql-query-browser-5.0r12-1fc5.i386.rpm


    If you need to connect MySQL from other hosts, remember to open the firewall on Port 3306.

2008年1月21日 星期一

搭便車?!

前情提要:
話說97.01.20(Sun)是我們校友團團練的日子,也就是我展現翩翩二胡英姿的風光日,我之前的交通方式,是從汐止坐火車至台北車站,換捷運到景美站,然後再走路到世新,自從前兩個禮拜買新摩托車,就盤算著來個萬里長征。

  • 12:10

  • 整裝出發,菊花享受定速巡行的灼熱感,身旁阿伯的野狼呼嘯而過,望塵莫及。

  • 12:30

  • 佇足在市政府的紅綠燈口,非刻意的看一下後照鏡,有位妙齡小姐在照鏡子,她胸前的黑線實在是太明顯了,讓我忍不住多看了十多秒,最後是被萬聲齊鳴的喇叭打斷。

  • 12:45

  • 直行基隆路,改變既定路線,於辛亥路口待轉。

  • 12:50

  • 馳騁在辛亥路上,突然感覺到後輪有飄移的現象,第一個念頭,該不會輪胎沒氣吧,新車都還沒過試用期,就給我漏氣,起初不以為意,不過晃動的程度越來越大,演變成以前後輪為直線,成扇形15度左右搖擺,此時正經過殯儀館門口,我一度想停下車來檢查,不過還是打消此念頭,繼續往前騎。

  • 12:55

  • 進入辛亥隧道,感覺速度突然慢了下來,油門催不太動,但不管三七二十一,勇往直前。

  • 17:30

  • 團練結束,說什麼都不走辛亥路,決定走羅斯福路,一路上倒也平安無事。

隔天摩托車剛好到三百公里,是既定進廠日,我就問老闆,是什麼原因使得摩托車會晃,老闆說,胎壓不足,等會兒會幫你檢查,結果老闆檢查完畢說,前後輪都不足。

只是奇怪的是,就只有那段是這樣...