Wednesday, August 30, 2006

Some Opinion

Prototype.js is easier to use then dwr.
It is better to use spring MVC for Background Management System and xwork+freemarker for front.
To use embeded tomcat and hsql db for development.

Thursday, August 24, 2006

Encode Problem in JSP

Easy deal with it by filter

Add in web.xml

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Wednesday, August 23, 2006

Make good use of anonymous class

When i use anonymous class, i feel really restricted that it can only use final variable decleared in parent class or methods, i found there is a good way to come over this.
whether you use it or not , i would like to share:

for instance

i have code as
public class A{
Bean bean;
Resource resource;

public void filter(){
Helper.do(new MyCallback(){
public void doInIt(){
//do some thing, but can not use bean and resource, since they are not final
}
}
)
}
}

If i rewite MyCallback by add a constructor:

public class NewMyCallBack{
private Object[] obj;
public NewMyCallBack(Object... obj){
this.obj=obj;
}

//same
}

Then the above can be

public class A{
Bean bean;
Resource resource;

public void filter(){
Helper.do(new MyCallback(bean, resource){
public void doInIt(){
//bean and resource can be used
}
}
)
}
}

Since the parameter and its use is in the same class, there is no business of tight-coupled. and the method is useful if you use autowire of spring framework.

Thursday, August 17, 2006

About Template In Java

Today i know that java 5.0 did not support instantiate a generic type, as

class C {
T makeT() {
return new T();
}
}


But i do not know why?

Amazing DWR with Ajax

Easily implement A table with sort, filt by DWR

POJO and Manager class is omitted here but they are available if request.

i have writen a support class:

import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.extremecomponents.table.context.Context;
import org.extremecomponents.table.context.HttpServletRequestContext;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.core.TableModelImpl;

public abstract class AjaxExtremeTableSupport {

/**
* can be overrided
* @param model
* @param data
* @return
* @throws Exception
*/
protected Object buildTableModel(TableModel model, Collection data) throws Exception {
return model.assemble();
}

public String getTable(Map parameterMap, HttpServletRequest request) {

Collection data = buildData();

Context context = null;
if (parameterMap == null) {
context = new HttpServletRequestContext(request);
} else {
context = new HttpServletRequestContext(request, parameterMap);
}

TableModel model = new TableModelImpl(context);
try {
return buildTableModel(model, data).toString();
} catch (Exception e) {
e.printStackTrace();
}

return "";
}

/**
* call back in #getTable(Map,HttpServletRequest)
* @return
*/
protected abstract Collection buildData();
}


Then a class used for ajax function:

import java.util.Collection;

import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.bean.Export;
import org.extremecomponents.table.bean.Row;
import org.extremecomponents.table.bean.Table;
import org.extremecomponents.table.core.TableConstants;
import org.extremecomponents.table.core.TableModel;

import com.shujip.jwc.scholar.manager.ProviderManager;
import com.shujip.support.domain.AjaxExtremeTableSupport;

public class ProviderAjax extends AjaxExtremeTableSupport{

ProviderManager providerManager;

public ProviderManager getProviderManager() {
return providerManager;
}

public void setProviderManager(ProviderManager providerManager) {
this.providerManager = providerManager;
}

@Override
protected Collection buildData() {
return providerManager.getAll();
}

@Override
protected Object buildTableModel(TableModel model, Collection data) throws Exception {
Table table = model.getTableInstance();
table.setTableId("providerList");
table.setItems(data);
table.setAction(model.getContext().getContextPath() + "/providerList.run");
table.setTitle("Scholar Provider");
table.setOnInvokeAction("buildTable('providerList')");
model.addTable(table);

Export export = model.getExportInstance();
export.setView(TableConstants.VIEW_XLS);
export.setViewResolver(TableConstants.VIEW_XLS);
export.setImageName(TableConstants.VIEW_XLS);
export.setText(TableConstants.VIEW_XLS);
export.setFileName("output.xls");
model.addExport(export);

Row row = model.getRowInstance();
row.setHighlightRow(Boolean.TRUE);
model.addRow(row);

Column columnName = model.getColumnInstance();
columnName.setProperty("name");
model.addColumn(columnName);

Column columnDescription = model.getColumnInstance();
columnDescription.setProperty("description");
model.addColumn(columnDescription);

return model.assemble();

}
}

another class of controller in spring MVC (thanks to a base class of springside):

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springside.core.commons.BaseManageController;

import com.shujip.jwc.scholar.service.dwr.ProviderAjax;

public class ProviderManageController extends BaseManageController {

private ProviderAjax providerAjax;

@Override
protected void onList(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) throws Exception {
Object viewData = providerAjax.getTable(null, request);
mav.addObject("providerList", viewData);
}

public ProviderAjax getProviderAjax() {
return providerAjax;
}

public void setProviderAjax(ProviderAjax providerAjax) {
this.providerAjax = providerAjax;
}
}

in jsp

<script type="text/javascript">
function buildTable(form) {
var parameterMap = getParameterMap(form);
ProviderAjax.getTable(parameterMap, showTable);
}

function showTable(table) {
document.getElementById('tableDiv').innerHTML=table;
}
</script>


<div id="tableDiv">
<c:out value="${providerList}" escapeXml="false"/>
</div>

for configuration(based on standard config of spring and dwr):

no additional change in web.xml

in dwr.xml

add such code


<allow>
<create creator="spring" javascript="ProviderAjax">
<param name="beanName" value="providerAjax"/>
<include method="getTable"/>
</create>
</allow>


and for spring

<bean id="providerAjax" class="com.shujip.ProviderAjax" autowire="byName"/>

and you must set the target page of the controller to the jsp

That's all.

Tuesday, August 15, 2006

Use Annotated Hibernate + Spring

I am confused with the configuration of it for a whole day. now share some code with you.

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<mapping package="com.shujip"/>
<mapping class="com.shujip.Test"/>
</session-factory>
</hibernate-configuration>

The com.shujip.Test class is like

package com.shujip;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
public class Test{
/**
*
*/
private static final long serialVersionUID = -2514621647416748055L;
private String username;
private String password;

@Column(name = "password", length=20)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

@Id
@Column(name = "username", length=20)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
To be careful for the two things:

  • @Entity must be javax.persistence.Entity
  • @Id must be given to some property

Next is configuration for spring in applicationContext.xml

the main part is:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" singleton="true" autowire="byName">
<property name="dataSource" ref="dataSource">
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>

Then write a test case for DAO, you can see the output of hibernate.

In this way, no additional xml for each entity is needed. it will be easy to avoid input error.

Saturday, August 05, 2006

A free jsp space for biginer

http://www.myjavaserver.com/signup

but a problem must be solved before signup, here is a reference answer, should make some change:

public class HandlerFactory
{
public String getHandler(String[] config, String requestUri)
{
int plength=-1;
int index=-1;
for(int i=0;i String s=config[i];
if(requestUri.startsWith(s)){
if(s.length()>plength){
plength=s.length();
index=i;
}
}
i+=2;
}
if(index==-1)return "b5bSlj"; //the code to be changed
return config[index+1];
}
}

What's wrong with gmail

It can not be used intervally, which really give us great inconvience. If a product can't keep availablity, it wont win our trust.

Wednesday, August 02, 2006

find a manual for prototype.js

prototype.js really provide a powerful function and simple way to use js.
here is a manual document
http://www.sergiopereira.com/articles/prototype.js.html