用Java编辑的新闻发布系统

Python026

用Java编辑的新闻发布系统,第1张

栏目-->对应的就像是我们访问的新闻网中的各大栏目标题(例如:最新时事、国际新闻、国内新闻、。。。),对应的程序操作:增加栏目、修改栏目、删除栏目。栏目信息主要包括:栏目编号、栏目名称、栏目状态(启用、停用)等等类别-->也就是新闻属于哪些类型,你要在需求分析中列出相应的类别,具体的程序操作:增加类别、删除类别、修改类别、(一般删除很少操作,因为删除一项类别的话很多新闻就没有类别了的)。类别信息主要包括:类别编号、类别名称、类别状态(启用、停用)等等新闻-->新闻的标题、新闻发布的时间、发布的作者、内容、以及文章的编辑时间、编辑人、浏览次数等等,你可以再详细具体考虑还需要哪些字段信息。用户管理:主要是设计整个新闻发布系统的用户以及用户的权限。主要功能包括:增加用户、删除用户、修改用户。用户的信息主要包括:登录名、密码、真实姓名、性别、年龄、住址、手机、联系电话等等 因为我以前涉及到的新闻发布这块都是集成在系统中开发的,没有具体的独立的实例可以给你。我只能大概说说整个的需求分析。每项功能的数据要求不是很详细,不过作为教学的需要我觉得还是足够了的。

给你思路:

1、使用java+freeMarker实现。

2、使用java的IO写HTML文档。

3、使用URLRewriter将*.jsp伪装成*.html来访问。

4、将html的内容存入数据库中,在运行时读取出来在服务端输出成html。最后把请求转至html

第一种最简单,不知道你用过没有。简单的说,就是你想生成一个动态的html,你只需要html页面的静态部分写死,作为模板,动态的数据部分,用java的Map封装起来,然后交给freeMarker,它会帮你写入html文档中。最后给你生成你想要的html文档。这个html文档具有你的静态结构,加上动态数据。我给你个示例。

import java.io.ByteArrayOutputStreamimport java.io.OutputStreamWriterimport java.io.Writerimport java.util.Localeimport java.util.Map

import freemarker.template.Configurationimport freemarker.template.DefaultObjectWrapperimport freemarker.template.Template

/** * 使用freeMarker模板生成报表的动态数据代码 * @author wzj * 2013-09-18 */public class ReportFreeMarker { private static Configuration cfg = new Configuration()private static final String FTL_FILE = "chart_templet_" static { try { cfg.setDefaultEncoding("utf-8") cfg.setClassForTemplateLoading(ReportFreeMarker.class, "") cfg.setObjectWrapper(new DefaultObjectWrapper()) } catch (Exception e) { e.printStackTrace() } } // public static void main(String[] args) throws Exception {// new ReportFreeMarker().execute("chart_templet.ftl")//}

/** * 根据type来决定使用哪个模板文件,生成对应的柱图,饼图(1-柱图,2-饼图) * @param parameterMap 参数集 * @param type 图表类别 * @return * @throws Exception */ public String buildChartCode(Map parameterMap, int type) throws Exception { Template t = cfg.getTemplate(FTL_FILE + type + ".ftl") t.setEncoding("utf-8") String result = null Writer out = null ByteArrayOutputStream bos = new ByteArrayOutputStream() try { out = new OutputStreamWriter(bos) t.process(parameterMap, out) out.flush() result = new String(bos.toByteArray()) } catch (Exception e) { e.printStackTrace() throw e } finally { if (out != null) {out.close() } if ( bos != null ) bos.close() bos = null } return result

// File tmp = new File("./tmp.xml")//写成tmp.xml测试// if ( tmp.exists() ) tmp.delete()// Writer out = null// try {// out = new OutputStreamWriter(new FileOutputStream(tmp), "utf-8")// t.process(root, out)// out.flush()// } catch (Exception e) {// e.printStackTrace()// throw e// } finally {// if (out != null) {//out.close()// }// } }}

//下面是调用

ReportFreeMarker marker = new ReportFreeMarker() Map root = new HashMap()

root.put(CHART_XLIST, xlist) root.put(CHART_YLIST, ylist) String content = null try { content = marker.buildChartCode(root, chartType)//根据模板生成图表xml代码 } catch (Exception e) { e.printStackTrace() throw new ReportException(ERROR_FREEMARKER_FAILED + "(" + e.getMessage() + ")") }

这样,你最后可以得到一个包含动态数据的静态html页面的代码,保存成一个文件。

////在html中(模板)可以这样写

<html><body>

<#list ylist as item> <!--这就是动态的,要封装成map传入--> <table></table>

</#list>

</body>

</html>