public class Template implements EntryPoint {
private String BASE_URL = GWT.getModuleBaseURL();
public void onModuleLoad() {
......
displayTemplate()
}
/**
* Display template.
*/
private void displayTemplate(){
templateService.sendToTemplateServer(new AsyncCallback<List<TemplateModel>>(){
@Override
public void onFailure(Throwable caught) {
Window.alert(caught.toString());
}
@Override
public void onSuccess(List<TemplateModel> templateModels) {
for (TemplateModel templateModel : templateModels) {
final String url = BASE_URL
+"readfile?templateName="+templateModel.getName().toString();
HTML template = new HTML("<a href='"+url+"'>"
+templateModel.getName()+"</a>"+"<br/>");
//把模板添加到已存在的Panl中
app.getTreePanel().add(template);
}
app.getNavigationPanel().add(app.getTreePanel(),"Template");
}
});
}
servlet 中进行处理
public class OpenPageFile extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
if ("".equals(req.getParameter("pageId"))) {
throw new NullPointerException("page id shoult not null.");
}
int pageId = Integer.parseInt(req.getParameter("pageId"));
Page page = new Page();
page.setId(pageId);
IContextMenuService serivce = (IContextMenuService)SpringBeanFactory.getBean("contextMenuService");
Page pageObject = serivce.getPageById(page.getId());
String pageContentName = pageObject.getPageContentName();
if (pageContentName!= null && !"".equals(pageContentName)) {
if (pageContentName.endsWith("xls")) {
resp.setContentType("application/vnd.ms-excel");
}else if (pageContentName.endsWith("xlsx")) {
resp.setContentType("application/vnd.ms-excel");
}else if (pageContentName.endsWith("pdf")) {
resp.setContentType("application/pdf");
}else if (pageContentName.endsWith("doc")) {
resp.setContentType("application/msword");
}else if (pageContentName.endsWith("docx")) {
resp.setContentType("application/msword");
}
}else{
resp.setContentType("application/unknown");
}
ServletOutputStream op = resp.getOutputStream();
byte[] bytes = new byte[1024];
if (null ==pageObject.getPageContent()) {
String msg = "page file not exist";
bytes = msg.getBytes();
}else{
bytes = pageObject.getPageContent();
}
op.write(bytes);
op.close();
resp.flushBuffer();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doPost(req, resp);
}
}