24 12月 2012

JSDT jQuery - Eclipse Plugin

在Eclipse中要編寫jQuery的程式通常要另開Browser以便查詢API,之前有jqueryWTP可以協助editor裡的autocomplete,但久未更新,換了新版的Eclipse也就無法使用了。

今天終於又看到了一個新的Plugin,而且還支援jQuery 1.8!!所以記錄一下,主要是安裝完後的設定不是那麼直覺。

JSDT jQuery,請參考http://code.google.com/a/eclipselabs.org/p/jsdt-jquery/

第一步,在Eclipse Marketplace中以jquery尋找plugin,就可以找到JSDT jQuery 

Jquery01

第二步,安裝後選取要使用的project的properties,在JavaScript -> Include Path裡按下 "Add JavaScript Library" 

Jquery02

第三步,選取jQuery Library,這選項是Plugin加上去的

Jquery03

第四步,就是選取jQuery的版本,最新的1.8已經有支援了!

Jquery04

 

接下來在Properties裡就能看到多了一個jQuery的Library

Jquery05

 

無論是在JSP或是JS檔案裡,都可以輕鬆看到jQuery的API囉!

Jquery06

26 7月 2012

Query By Example In Spring-Data-JPA

Query By Example (QBE) 是個常用的查詢模式,Hibernate有Example Query來實踐,但JPA沒有,這對我常用的開發模式而言是個不小的困擾,所以基本上我都把JPA放在一邊,直接用Hibernate。
其實QBE的概念並不難實現,只要分析傳入的Domain Object,哪些property有值,加入查詢條件即可, 趁著覆習Spring Data的同時,就做個簡單的實作好了。

先建個ExpressionParam來儲存分析Domain Object Class後的結果,readMethod就是用來取值,而attribue則是用來取得Criteria的Path

public class ExpressionParam<T> {
	private String name;
	private Method readMethod;
	private SingularAttribute<T, ?> attribute;
	
	public ExpressionParam(String name, Method readMethod, SingularAttribute<T, ?> attribute) {
		super();
		this.name = name;
		this.readMethod = readMethod;
		this.attribute = attribute;
	}
	.....
}

再來就是配合Spring-Data的Specification Query,實作一個ExampleSpecification,其中作個簡單的Cache機制,以免同樣的Domain Object Class要一直重覆分析有哪些ReadMethod。

public class ExampleSpecification<T> implements Specification<T> {
	private static final Logger logger = LoggerFactory.getLogger(ExampleSpecification.class);
	protected static final Map<Class<?>, List<ExpressionParam<?>>> classCache = Collections.synchronizedMap(new WeakHashMap<Class<?>, List<ExpressionParam<?>>>());
	
	EntityManager entityManager;
	T example;
	
	public ExampleSpecification(final EntityManager entityManager, final T example) {
		this.entityManager = entityManager;
		this.example = example;
	}
	
	@Override
	public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
			CriteriaBuilder cb) {
		List<Predicate> predicates = new ArrayList<Predicate>();
		
		EntityType<T> entity = entityManager.getMetamodel().entity((Class<T>)example.getClass());
		List<ExpressionParam<?>> params = parseReadMethod(entity);
		for (ExpressionParam<?> param : params) {
			try {
				Object value = param.getReadMethod().invoke(example);
				if (null != value && StringUtils.isNotEmpty(value.toString())) {
					predicates.add(cb.equal(root.get((SingularAttribute<T, ?>)param.getAttribute()), value));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return predicates.isEmpty()?cb.conjunction() : cb.and(predicates.toArray(new Predicate[predicates.size()]));
	}

	protected List<ExpressionParam<?>> parseReadMethod(EntityType<T> entityType) {
		Class<T> clazz = (Class<T>) entityType.getClass();
		if (classCache.containsKey(clazz)) {
			return classCache.get(clazz);
		}
		logger.info("First Parsing Read Method for Class[{}]", clazz);
		
		List<ExpressionParam<?>> methods = new ArrayList<ExpressionParam<?>>();
		classCache.put(clazz, methods);
		PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(example.getClass());
		
		Set<SingularAttribute<T, ?>> atts = entityType.getDeclaredSingularAttributes();
		for (SingularAttribute<T,?> sat : atts) {
			if (PersistentAttributeType.MANY_TO_ONE == sat.getPersistentAttributeType()
					|| PersistentAttributeType.ONE_TO_ONE == sat.getPersistentAttributeType()) {
				continue;
			}
			String name = sat.getName();
			Method readMethod = null;
			for (PropertyDescriptor pd : pds) {
				if (pd.getName().equals(name)) {
					readMethod = pd.getReadMethod();
					break;
				}
			}
			
			logger.debug("Property {} - Method {}", name, readMethod);
			if (null != readMethod) {
				methods.add(new ExpressionParam<T>(name, readMethod, sat));
			}
		}
		
		return methods;
	}
}

由於Spring Data的Repository產生機制不容易修改(其實是我還沒找到好的切入點....)所以只好在Service Layer來達到QBE的作用,可以參考下面UserService的做法。

@Service
@Transactional(readOnly=true)
public class UserService {
	@PersistenceContext
	private EntityManager entityManager;
	
	@Autowired
	private UserJpaDao userDao;
	
	public List findByExample(User example) {
		ExampleSpecification es = new ExampleSpecification(entityManager, example);
		return userDao.findAll(es);
	}
}

再來看一下Test的實際應用

public class UserJpaDaoTest {
	@Autowired
	private UserService userService;
	@Test
	public void test() {
		User example = new User();
		example.setName("Bob");
		
		this.userService.findByExample(example);
	}

}

順便提一下,不才小弟又要找新工作了,若有覺得適合小弟的可以聯絡交流一下。

01 2月 2012

Install Sun/Oracle JDK in Ubuntu 11.10

在Oracle買下Sun之後,Ubuntu上要安裝sun/oracle的JDK是愈來愈不容易...
還是做個筆記囉

安裝Sun JDK6

sudo add-apt-repository ppa:flexiondotorg/java 
sudo apt-get update
sudo apt-get install sun-java6-jdk sun-java6-plugin sun-java6-fonts

安裝Oracle JDK7
這部份主要是參考Intalling Java 7 on Ubuntu Natty (11.04)

  1. 請至Oracle http://www.oracle.com/technetwork/java/javase/downloads/index.html Download最新的檔案,要注意配合作業系統。下列的例子以x64為主。
  2. 解壓縮後移到/usr/lib/jvm裡,並重新命名為java-7-oracle-1.7.0_02,再修改owner為root,最後建一個名為java-7-oracle的link,這樣以後若有新的版本,就只要修改link即可。
      tar -xzf jdk-7u2-linux-x64.tar.gz
      sudo mv jdk1.7.0_02 /usr/lib/jvm/java-7-oracle-1.7.0_02
      cd /usr/lib/jvm
      sudo chown -R root:root java-7-oracle-1.7.0_02
      sudo ln -s java-7-oracle-1.7.0_02 java-7-oracle
    
  3. 以root的身份執行下列的指令,若不想一直下sudo,就先執行sudo -sH來切換身份吧
    update-alternatives --quiet --install /usr/lib/xulrunner-addons/plugins/libjavaplugin.so xulrunner-1.9-javaplugin.so /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libnpjp2.so 63
    update-alternatives --quiet --install /usr/lib/mozilla/plugins/libjavaplugin.so mozilla-javaplugin.so /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libnpjp2.so 63
    update-alternatives --quiet --install /usr/bin/appletviewer appletviewer /usr/lib/jvm/java-7-oracle/bin/appletviewer 63 --slave /usr/share/man/man1/appletviewer.1 appletviewer.1 /usr/lib/jvm/java-7-oracle/man/man1/appletviewer.1
    update-alternatives --quiet --install /usr/bin/apt apt /usr/lib/jvm/java-7-oracle/bin/apt 63 --slave /usr/share/man/man1/apt.1 apt.1 /usr/lib/jvm/java-7-oracle/man/man1/apt.1
    update-alternatives --quiet --install /usr/bin/extcheck extcheck /usr/lib/jvm/java-7-oracle/bin/extcheck 63 --slave /usr/share/man/man1/extcheck.1 extcheck.1 /usr/lib/jvm/java-7-oracle/man/man1/extcheck.1
    update-alternatives --quiet --install /usr/bin/idlj idlj /usr/lib/jvm/java-7-oracle/bin/idlj 63 --slave /usr/share/man/man1/idlj.1 idlj.1 /usr/lib/jvm/java-7-oracle/man/man1/idlj.1
    update-alternatives --quiet --install /usr/bin/jar jar /usr/lib/jvm/java-7-oracle/bin/jar 63 --slave /usr/share/man/man1/jar.1 jar.1 /usr/lib/jvm/java-7-oracle/man/man1/jar.1
    update-alternatives --quiet --install /usr/bin/jarsigner jarsigner /usr/lib/jvm/java-7-oracle/bin/jarsigner 63 --slave /usr/share/man/man1/jarsigner.1 jarsigner.1 /usr/lib/jvm/java-7-oracle/man/man1/jarsigner.1
    update-alternatives --quiet --install /usr/bin/javac javac /usr/lib/jvm/java-7-oracle/bin/javac 63 --slave /usr/share/man/man1/javac.1 javac.1 /usr/lib/jvm/java-7-oracle/man/man1/javac.1
    update-alternatives --quiet --install /usr/bin/javadoc javadoc /usr/lib/jvm/java-7-oracle/bin/javadoc 63 --slave /usr/share/man/man1/javadoc.1 javadoc.1 /usr/lib/jvm/java-7-oracle/man/man1/javadoc.1
    update-alternatives --quiet --install /usr/bin/javah javah /usr/lib/jvm/java-7-oracle/bin/javah 63 --slave /usr/share/man/man1/javah.1 javah.1 /usr/lib/jvm/java-7-oracle/man/man1/javah.1
    update-alternatives --quiet --install /usr/bin/javap javap /usr/lib/jvm/java-7-oracle/bin/javap 63 --slave /usr/share/man/man1/javap.1 javap.1 /usr/lib/jvm/java-7-oracle/man/man1/javap.1
    update-alternatives --quiet --install /usr/bin/jconsole jconsole /usr/lib/jvm/java-7-oracle/bin/jconsole 63 --slave /usr/share/man/man1/jconsole.1 jconsole.1 /usr/lib/jvm/java-7-oracle/man/man1/jconsole.1
    update-alternatives --quiet --install /usr/bin/jdb jdb /usr/lib/jvm/java-7-oracle/bin/jdb 63 --slave /usr/share/man/man1/jdb.1 jdb.1 /usr/lib/jvm/java-7-oracle/man/man1/jdb.1
    update-alternatives --quiet --install /usr/bin/jhat jhat /usr/lib/jvm/java-7-oracle/bin/jhat 63 --slave /usr/share/man/man1/jhat.1 jhat.1 /usr/lib/jvm/java-7-oracle/man/man1/jhat.1
    update-alternatives --quiet --install /usr/bin/jinfo jinfo /usr/lib/jvm/java-7-oracle/bin/jinfo 63 --slave /usr/share/man/man1/jinfo.1 jinfo.1 /usr/lib/jvm/java-7-oracle/man/man1/jinfo.1
    update-alternatives --quiet --install /usr/bin/jmap jmap /usr/lib/jvm/java-7-oracle/bin/jmap 63 --slave /usr/share/man/man1/jmap.1 jmap.1 /usr/lib/jvm/java-7-oracle/man/man1/jmap.1
    update-alternatives --quiet --install /usr/bin/jps jps /usr/lib/jvm/java-7-oracle/bin/jps 63 --slave /usr/share/man/man1/jps.1 jps.1 /usr/lib/jvm/java-7-oracle/man/man1/jps.1
    update-alternatives --quiet --install /usr/bin/jrunscript jrunscript /usr/lib/jvm/java-7-oracle/bin/jrunscript 63 --slave /usr/share/man/man1/jrunscript.1 jrunscript.1 /usr/lib/jvm/java-7-oracle/man/man1/jrunscript.1
    update-alternatives --quiet --install /usr/bin/jsadebugd jsadebugd /usr/lib/jvm/java-7-oracle/bin/jsadebugd 63 --slave /usr/share/man/man1/jsadebugd.1 jsadebugd.1 /usr/lib/jvm/java-7-oracle/man/man1/jsadebugd.1
    update-alternatives --quiet --install /usr/bin/jstack jstack /usr/lib/jvm/java-7-oracle/bin/jstack 63 --slave /usr/share/man/man1/jstack.1 jstack.1 /usr/lib/jvm/java-7-oracle/man/man1/jstack.1
    update-alternatives --quiet --install /usr/bin/jstat jstat /usr/lib/jvm/java-7-oracle/bin/jstat 63 --slave /usr/share/man/man1/jstat.1 jstat.1 /usr/lib/jvm/java-7-oracle/man/man1/jstat.1
    update-alternatives --quiet --install /usr/bin/jstatd jstatd /usr/lib/jvm/java-7-oracle/bin/jstatd 63 --slave /usr/share/man/man1/jstatd.1 jstatd.1 /usr/lib/jvm/java-7-oracle/man/man1/jstatd.1
    update-alternatives --quiet --install /usr/bin/native2ascii native2ascii /usr/lib/jvm/java-7-oracle/bin/native2ascii 63 --slave /usr/share/man/man1/native2ascii.1 native2ascii.1 /usr/lib/jvm/java-7-oracle/man/man1/native2ascii.1
    update-alternatives --quiet --install /usr/bin/rmic rmic /usr/lib/jvm/java-7-oracle/bin/rmic 63 --slave /usr/share/man/man1/rmic.1 rmic.1 /usr/lib/jvm/java-7-oracle/man/man1/rmic.1
    update-alternatives --quiet --install /usr/bin/schemagen schemagen /usr/lib/jvm/java-7-oracle/bin/schemagen 63 --slave /usr/share/man/man1/schemagen.1 schemagen.1 /usr/lib/jvm/java-7-oracle/man/man1/schemagen.1
    update-alternatives --quiet --install /usr/bin/serialver serialver /usr/lib/jvm/java-7-oracle/bin/serialver 63 --slave /usr/share/man/man1/serialver.1 serialver.1 /usr/lib/jvm/java-7-oracle/man/man1/serialver.1
    update-alternatives --quiet --install /usr/bin/wsgen wsgen /usr/lib/jvm/java-7-oracle/bin/wsgen 63 --slave /usr/share/man/man1/wsgen.1 wsgen.1 /usr/lib/jvm/java-7-oracle/man/man1/wsgen.1
    update-alternatives --quiet --install /usr/bin/wsimport wsimport /usr/lib/jvm/java-7-oracle/bin/wsimport 63 --slave /usr/share/man/man1/wsimport.1 wsimport.1 /usr/lib/jvm/java-7-oracle/man/man1/wsimport.1
    update-alternatives --quiet --install /usr/bin/xjc xjc /usr/lib/jvm/java-7-oracle/bin/xjc 63 --slave /usr/share/man/man1/xjc.1 xjc.1 /usr/lib/jvm/java-7-oracle/man/man1/xjc.1
    update-alternatives --quiet --install /usr/bin/java-rmi.cgi java-rmi.cgi /usr/lib/jvm/java-7-oracle/bin/java-rmi.cgi 63
    update-alternatives --quiet --install /usr/bin/ControlPanel ControlPanel /usr/lib/jvm/java-7-oracle/jre/bin/ControlPanel 63
    update-alternatives --quiet --install /usr/bin/java java /usr/lib/jvm/java-7-oracle/jre/bin/java 63
    update-alternatives --quiet --install /usr/bin/java_vm java_vm /usr/lib/jvm/java-7-oracle/jre/bin/java_vm 63
    update-alternatives --quiet --install /usr/bin/javaws javaws /usr/lib/jvm/java-7-oracle/jre/bin/javaws 63
    update-alternatives --quiet --install /usr/bin/jcontrol jcontrol /usr/lib/jvm/java-7-oracle/jre/bin/jcontrol 63
    update-alternatives --quiet --install /usr/bin/keytool keytool /usr/lib/jvm/java-7-oracle/jre/bin/keytool 63
    update-alternatives --quiet --install /usr/bin/pack200 pack200 /usr/lib/jvm/java-7-oracle/jre/bin/pack200 63
    update-alternatives --quiet --install /usr/bin/policytool policytool /usr/lib/jvm/java-7-oracle/jre/bin/policytool 63
    update-alternatives --quiet --install /usr/bin/rmid rmid /usr/lib/jvm/java-7-oracle/jre/bin/rmid 63
    update-alternatives --quiet --install /usr/bin/rmiregistry rmiregistry /usr/lib/jvm/java-7-oracle/jre/bin/rmiregistry 63
    update-alternatives --quiet --install /usr/bin/unpack200 unpack200 /usr/lib/jvm/java-7-oracle/jre/bin/unpack200 63
    update-alternatives --quiet --install /usr/bin/orbd orbd /usr/lib/jvm/java-7-oracle/jre/bin/orbd 63
    update-alternatives --quiet --install /usr/bin/servertool servertool /usr/lib/jvm/java-7-oracle/jre/bin/servertool 63
    update-alternatives --quiet --install /usr/bin/tnameserv tnameserv /usr/lib/jvm/java-7-oracle/jre/bin/tnameserv 63
    update-alternatives --quiet --install /usr/bin/jexec jexec /usr/lib/jvm/java-7-oracle/jre/lib/jexec 63
    
  4. 在/usr/lib/jvm建一份.java-7-oracle.jinfo的清單,請注意這是個隱藏檔,所以檔名是有個'.'
    sudo vim /usr/lib/jvm/.java-7-oracle.jinfo
    
    內容則如下
    name=java-7-oracle
    alias=java-7-oracle
    priority=63
    section=non-free
    
    jre ControlPanel /usr/lib/jvm/java-7-oracle/jre/bin/ControlPanel
    jre java /usr/lib/jvm/java-7-oracle/jre/bin/java
    jre java_vm /usr/lib/jvm/java-7-oracle/jre/bin/java_vm
    jre javaws /usr/lib/jvm/java-7-oracle/jre/bin/javaws
    jre jcontrol /usr/lib/jvm/java-7-oracle/jre/bin/jcontrol
    jre keytool /usr/lib/jvm/java-7-oracle/jre/bin/keytool
    jre pack200 /usr/lib/jvm/java-7-oracle/jre/bin/pack200
    jre policytool /usr/lib/jvm/java-7-oracle/jre/bin/policytool
    jre rmid /usr/lib/jvm/java-7-oracle/jre/bin/rmid
    jre rmiregistry /usr/lib/jvm/java-7-oracle/jre/bin/rmiregistry
    jre unpack200 /usr/lib/jvm/java-7-oracle/jre/bin/unpack200
    jre orbd /usr/lib/jvm/java-7-oracle/jre/bin/orbd
    jre servertool /usr/lib/jvm/java-7-oracle/jre/bin/servertool
    jre tnameserv /usr/lib/jvm/java-7-oracle/jre/bin/tnameserv
    jre jexec /usr/lib/jvm/java-7-oracle/jre/lib/jexec
    jdk appletviewer /usr/lib/jvm/java-7-oracle/bin/appletviewer
    jdk apt /usr/lib/jvm/java-7-oracle/bin/apt
    jdk extcheck /usr/lib/jvm/java-7-oracle/bin/extcheck
    jdk idlj /usr/lib/jvm/java-7-oracle/bin/idlj
    jdk jar /usr/lib/jvm/java-7-oracle/bin/jar
    jdk jarsigner /usr/lib/jvm/java-7-oracle/bin/jarsigner
    jdk java-rmi.cgi /usr/lib/jvm/java-7-oracle/bin/java-rmi.cgi
    jdk javac /usr/lib/jvm/java-7-oracle/bin/javac
    jdk javadoc /usr/lib/jvm/java-7-oracle/bin/javadoc
    jdk javah /usr/lib/jvm/java-7-oracle/bin/javah
    jdk javap /usr/lib/jvm/java-7-oracle/bin/javap
    jdk jconsole /usr/lib/jvm/java-7-oracle/bin/jconsole
    jdk jdb /usr/lib/jvm/java-7-oracle/bin/jdb
    jdk jhat /usr/lib/jvm/java-7-oracle/bin/jhat
    jdk jinfo /usr/lib/jvm/java-7-oracle/bin/jinfo
    jdk jmap /usr/lib/jvm/java-7-oracle/bin/jmap
    jdk jps /usr/lib/jvm/java-7-oracle/bin/jps
    jdk jrunscript /usr/lib/jvm/java-7-oracle/bin/jrunscript
    jdk jsadebugd /usr/lib/jvm/java-7-oracle/bin/jsadebugd
    jdk jstack /usr/lib/jvm/java-7-oracle/bin/jstack
    jdk jstat /usr/lib/jvm/java-7-oracle/bin/jstat
    jdk jstatd /usr/lib/jvm/java-7-oracle/bin/jstatd
    jdk native2ascii /usr/lib/jvm/java-7-oracle/bin/native2ascii
    jdk rmic /usr/lib/jvm/java-7-oracle/bin/rmic
    jdk schemagen /usr/lib/jvm/java-7-oracle/bin/schemagen
    jdk serialver /usr/lib/jvm/java-7-oracle/bin/serialver
    jdk wsgen /usr/lib/jvm/java-7-oracle/bin/wsgen
    jdk wsimport /usr/lib/jvm/java-7-oracle/bin/wsimport
    jdk xjc /usr/lib/jvm/java-7-oracle/bin/xjc
    plugin xulrunner-1.9-javaplugin.so /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libnpjp2.so
    plugin mozilla-javaplugin.so /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libnpjp2.so
    
  5. update java alternatives 這時準備工作就完成了,可以開始設定ubuntu預設使用的jdk了 先執行下列指令
    sudo update-alternatives --config java
    
    正常的話可以看到下列的提示
    There are 2 choices for the alternative java (providing /usr/bin/java).
    
      Selection    Path                                     Priority   Status
    ------------------------------------------------------------
    * 0            /usr/lib/jvm/java-6-sun/jre/bin/java      63        auto mode
      1            /usr/lib/jvm/java-6-sun/jre/bin/java      63        manual mode
      2            /usr/lib/jvm/java-7-oracle/jre/bin/java   63        manual mode
    
    此刻再輸入號碼2,選擇jdk7做為預設的java版本
    當然也可以直接執行下列指令來改改
    sudo update-java-alternatives --set java-7-oracle
    

31 1月 2012

Git over Apache2 http server in Ubuntu 11.10

由於手邊的MBP年事已高(五歲多囉),而且RAM最大僅能加到3G,開了IDE與Browser後已經沒什麼多餘的能力來支撐一個VM了。所以把另一台NB裝上Ubuntu 11.10,並將常用的服務裝上去,順便做些更新的筆記。
先前也曾經有個安裝筆記GIT + Apache2 on Ubuntu 8.10 (1),但現在的Ubuntu安裝又更簡單些;主要是因為現在提供了git-http-backend,設定上可以更簡化了。

 

  1. 安裝apache 與 git
    sudo apt-get install apache2 git
    
    後期的Ubuntu 已將git-core更名為git,但是用git-core也沒問題啦。
  2. 建立相關人員帳號密碼,-c代表要建新檔,-m代表密碼要加密
    sudo htpasswd -cm /etc/apache2/passwd $username
    第二位成員起就不需要加-c,只要用-m即可
    sudo htpasswd -m /etc/apache2/passwd $username
  3. 建立git repositories目錄,依習慣,一定要先有repository root。
    sudo mkdir /srv/git
    再建要用的repository,並修改owner為www-data,$reponame代表repository的名稱,.git雖然可加可不加,但建議是要有,.git不會加到clone回來的目錄名稱上
    sudo git --bare init /srv/git/$reponame.git
    sudo chown -R www-data:www-data /srv/git/$reponame.git
  4. 建立dav_git.conf到/etc/apache2/mods-available,並link到/etc/apache2/mods-enabled,檔名可以隨意,但副檔名一定要是conf.
    cd /etc/apache2/mods-available
    sudo vim dav_git.conf

    編輯內容如下

    #若無設定GIT_HTTP_EXPORT_ALL,則在每個repository下必需有git-daemon-export-ok這個檔案才能被外部存取。
    SetEnv GIT_PROJECT_ROOT /srv/git
    SetEnv GIT_HTTP_EXPORT_ALL
    ScriptAlias /git/ /usr/lib/git-core/git-http-backend/
    #用Location的話代表讀寫都要認證,如果希望開放給anonymous讀取,請改用LocationMatch即可
    <Location /git>
            AuthType Basic
            AuthName "Git"
            Require valid-user
            AuthUserFile /etc/apache2/passwd
    </Location>
    
    建立link到/etc/apache2/mods-enabled
    cd /etc/apache2/mods-enabled
    sudo ln -s ../mods-available/dav_git.conf dav_git.conf
  5. 重起apache2即可

 

 

02 1月 2012

jQuery Plugin : DataTables (6)

前接jQuery Plugin : DataTables (5)

再來就是DataTables比較費工的地方。
大部份遇到的案子,Table/Grid裡除了顯示,通常還要可以加上checkbox, radio, button, link等操作,以提供多個選取刪除,單選編輯等功能。
這也是我覺得使用了DataTables,程式碼實際不會少多少,開發效率提昇待議的部份,但也可能是我能力的問題,若有大德能指引明燈,小弟會非常感激。

以下以在table裡加上checkbok與button的操作為例,
DataTables裡要多加Column就必需在aoColumns裡以mDataProp標示,由於資料並非自JSON Object裡取得,所以就必需設定為null.
而要顯示自訂的資料,就必需提供"fnRender"這個callback method,其中僅有一個參數oObj,oObj其中的aData就是目前這個Row的JSON Object,所以就可取得你要想的資料。

所以修改一下javascript的aoColumns:

"aoColumns": [
				{ "mDataProp": null, "fnRender": function(oObj){ return "<input type='checkbox' name='id' value='"+oObj.aData.id+"'>"}, "sWidth":"5%", "bSortable":false },
				{ "mDataProp": "id" },
				{ "mDataProp": "name" },
				{ "mDataProp": "phone" },
				{ "mDataProp": null, "fnRender": function(oObj){ return "<input type='button' value='Edit'>"}, "sWidth":"5%", "bSortable":false }
			]
當然對應的Table也要改一下Column的設定

完整的程式碼如下:

	<script type="text/javascript">
		var disTable;
		var gaiSelected = [];
		$().ready(function() {
			disTable = $("#sample").dataTable({
				"sPaginationType":"full_numbers",
				"bProcessing": true,
				"bServerSide":true,
				"sAjaxSource": '/jquery/DataTableServlet02',
				"fnServerParams": function ( aoData ) {
					$.merge(aoData, $("#myform").serializeArray());},
				"aoColumns": [
							  { "mDataProp": null, 
							  	"fnRender": function(oObj){ 
							  		return "<input type='checkbox' name='id' value='"+oObj.aData.id+"'>"}, 
							  	"sWidth":"5%", 
							  	"bSortable":false },
							  { "mDataProp": "id" },
				              { "mDataProp": "name" },
				              { "mDataProp": "phone" },
				              { "mDataProp": null, 
				              	"fnRender": function(oObj){ 
				              		return "<input type='button' value='Edit'>"}, 
				              	"sWidth":"5%", 
				              	"bSortable":false }
				          ]
			});
		});
	</script>
	<form id="myform">
	<table>
		<tr><td>NAME:</td><td><input type="text" name="name" /></td></tr>
		<tr><td>ID:</td><td><input type="text" name="id" /></td></tr>
		<tr><td>PHONE:</td><td><input type="text" name="phone" /></td></tr>
		<tr><td colspan="2"><input id="btnSearch" type="button" value="Search" onclick="disTable.fnDraw();"/></td></tr>
	</table>
	</form>
	<table id="sample" class="display">
		<thead>
			<tr>
				<th>Select</th>
				<th>ID</th>
				<th>Name</th>
				<th>Phone</th>
				<th>Function</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>

產生的畫面就像下列這樣:

add checkbox and button

看起來似乎還不錯?不過大家實際遇到的例子應會比這更複雜吧,例如依不同的tag顯示不同的圖片,不同的狀態顯示不同的Button,這些全要寫在script中耶....