需要一个模板temp.html,一个提交页面index.jsp, 一个actionform, 一个action,一个时间bean
具体如下:
1 temp.html
#title#
2 index.jsp<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="" prefix="bean"%>
<%@ taglib uri="" prefix="html"%>
JSP for CreateForm form
title :
content :
3:
actionform
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package cn.edu.nankai.tecpenguin.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts
* Creation date: 11-03-2007
*
* XDoclet definition:
* @struts.form name="createForm"
*/
public class CreateForm extends ActionForm {
/*
* Generated fields
*/
/** title property */
private String title;
/** content property */
private String content;
/*
* Generated Methods
*/
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
/**
* Returns the title.
* @return String
*/
public String getTitle() {
return title;
}
/**
* Set the title.
* @param title The title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* Returns the content.
* @return String
*/
public String getContent() {
return content;
}
/**
* Set the content.
* @param content The content to set
*/
public void setContent(String content) {
this.content = content;
}
}
4 action
(实现部分)
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package cn.edu.nankai.tecpenguin.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import cn.edu.nankai.tecpenguin.struts.form.CreateForm;
import cn.edu.sdau.jiahaolin.time.*;
import java.io.*;
/**
* MyEclipse Struts
* Creation date: 11-03-2007
*
* XDoclet definition:
* @struts.action path="/create" name="createForm" input="/index.jsp" scope="request" validate="true"
*/
public class CreateAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
CreateForm createForm = (CreateForm) form;// TODO Auto-generated method stub
ServerTime time=new ServerTime();
String path=time.date();
String title=createForm.getTitle();
String content=createForm.getContent();
String filePath = request.getRealPath("/");
filePath=filePath+path;
File addpath=new File(filePath);
if(addpath.exists()){
}else{
addpath.mkdir();
}
String tem_path = request.getRealPath("/")+"temp.html";
String templateContent=null;
//ServerTime time=new ServerTime();
try{
FileInputStream fileinputstream = new FileInputStream(tem_path);
int lenght = fileinputstream.available(); //available() 返回可以不受阻塞地从此文件输入流中读取的字节数。
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes); //read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个字节数组中。
fileinputstream.close();
templateContent = new String(bytes);
templateContent=templateContent.replaceAll("#title#",title);
//templateContent=templateContent.replaceAll("#author#",editer);//替换掉模块中相应的地方
templateContent=templateContent.replaceAll("#content#",content);
// templateContent = new String(bytes);
String a=null;
a=Long.toString(time.second())+".html";
String fileame = filePath+"/"+a;//生成的html文件保存路径
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
//return null;
return mapping.findForward("success");
}catch(Exception e){
//return null;
e.printStackTrace();
return mapping.findForward("error");
}
}
}
5
package cn.edu.sdau.jiahaolin.time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class ServerTime
{
public long second()
{
Date date = new java.util.Date();
long second = date.getTime() / 1000L;
return second;
}
public static String date()throws Exception
{
String date = null ;
try{Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
date = formatter.format(cal.getTime());}
catch(Exception e){}
return date;
}
public static String datetime()throws Exception
{ String date=null;
try{Calendar cal = Calendar.getInstance();
//SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
date = formatter.format(cal.getTime());}
catch(Exception e){}
return date;
}
public String returndate()throws Exception
{
String returndate = date();
return returndate;
}
}