public abstract class BaseAction<T> extends ActionSupport {
private Page page = new Page();
private T t;
public abstract IBaseService<T> getBaseService();
public abstract String getUrl();
public String findPage(){
getBaseService().getPage(getPage());
getPage().setUrl(getUrl());
return Action.SUCCESS;
}
public String add(){
getBaseService().add(t);
return Action.SUCCESS;
}
public String findById(){
t = getBaseService().getById(getEntityId());
return Action.SUCCESS;
}
public String update(){
getBaseService().update(t);
return Action.SUCCESS;
}
public String delete(){
getBaseService().delete(getEntityId());
return Action.SUCCESS;
}
private int getEntityId() {
try {
Field fieldName = t.getClass().getDeclaredField("id");
fieldName.setAccessible(true);
return Integer.parseInt(fieldName.get(t).toString());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return 0;
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
}