静态页面生成那些事
2012-04-26 16:04
169 查看
相信在互联网企业或多或少都有一些需要生成静态页面的需求,下面就来谈谈我在项目里面遇到的生成静态页面的问题:
最初使用HttpUrlConnection指定URL向服务器发起一个连接请求,请求成功后从Connection对象获取输入流,然后将输入流内容写入指定的文件,开发阶段发布到测试服务器(内网)没有任何问题,但是发布到正式环境(公网)时则无法发布且不报任何错误,开始以为linux服务器文件写权限问题,但后面经过调试发现是获取网络输入流失败,估计是和网络设置有关,由于上线时间紧迫,就改为方式2!
方式1关键代码:
URL url = new URL(httpUrl);
connection = (HttpURLConnection) url
.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0");
connection.connect();
in = connection.getInputStream();
BufferedReader breader = new BufferedReader(
new InputStreamReader(in, "utf-8"));
String currentLine;
while ((currentLine = breader.readLine()) != null) {
htmlCode += currentLine + System.getProperty("line.separator");
}
方式2:还记得上家公司老大经常说我们,”有现成的东西不用,在那瞎折腾“顿时想起spring提供了专门的模版引擎,就果断拿来用之,写了个小demo放到正式环境测试完全OK,果断替换之 主要代码如下 大家都懂得:
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="resourceLoaderPath" value="/WEB-INF/"/>
<property name="velocityProperties">
<props>
<prop key="velocimacro.library"></prop>
<prop key="default.contentType">text/html; charset=utf-8</prop>
<prop key="output.encoding">utf-8</prop>
<prop key="input.encoding">utf-8</prop>
</props>
</property>
</bean>
<bean id="templateEngine" class="com.jia.schain.commons.util.TemplateEngine">
<property name="velocityEngine" ref="velocityEngine"/>
</bean>
public class TemplateEngine {
private VelocityEngine velocityEngine;
public void setVelocityEngine(VelocityEngine velocityEngine) {
this.velocityEngine = velocityEngine;
}
/**
*
* @param pathname 路径名
* @param templatefile 模版文件名
* @param model 模版数据
* @return
*/
public String build(String pathname, String templatefile, Map model){
StringWriter writer = new StringWriter();
try {
VelocityEngineUtils.mergeTemplate(velocityEngine, templatefile, "UTF-8", model, writer);
} catch (VelocityException e) {
e.printStackTrace();
}
String htmlStr = writer.toString();
FileUtils.write(pathname, htmlStr);
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
return htmlStr;
}
}
最初使用HttpUrlConnection指定URL向服务器发起一个连接请求,请求成功后从Connection对象获取输入流,然后将输入流内容写入指定的文件,开发阶段发布到测试服务器(内网)没有任何问题,但是发布到正式环境(公网)时则无法发布且不报任何错误,开始以为linux服务器文件写权限问题,但后面经过调试发现是获取网络输入流失败,估计是和网络设置有关,由于上线时间紧迫,就改为方式2!
方式1关键代码:
URL url = new URL(httpUrl);
connection = (HttpURLConnection) url
.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0");
connection.connect();
in = connection.getInputStream();
BufferedReader breader = new BufferedReader(
new InputStreamReader(in, "utf-8"));
String currentLine;
while ((currentLine = breader.readLine()) != null) {
htmlCode += currentLine + System.getProperty("line.separator");
}
方式2:还记得上家公司老大经常说我们,”有现成的东西不用,在那瞎折腾“顿时想起spring提供了专门的模版引擎,就果断拿来用之,写了个小demo放到正式环境测试完全OK,果断替换之 主要代码如下 大家都懂得:
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="resourceLoaderPath" value="/WEB-INF/"/>
<property name="velocityProperties">
<props>
<prop key="velocimacro.library"></prop>
<prop key="default.contentType">text/html; charset=utf-8</prop>
<prop key="output.encoding">utf-8</prop>
<prop key="input.encoding">utf-8</prop>
</props>
</property>
</bean>
<bean id="templateEngine" class="com.jia.schain.commons.util.TemplateEngine">
<property name="velocityEngine" ref="velocityEngine"/>
</bean>
public class TemplateEngine {
private VelocityEngine velocityEngine;
public void setVelocityEngine(VelocityEngine velocityEngine) {
this.velocityEngine = velocityEngine;
}
/**
*
* @param pathname 路径名
* @param templatefile 模版文件名
* @param model 模版数据
* @return
*/
public String build(String pathname, String templatefile, Map model){
StringWriter writer = new StringWriter();
try {
VelocityEngineUtils.mergeTemplate(velocityEngine, templatefile, "UTF-8", model, writer);
} catch (VelocityException e) {
e.printStackTrace();
}
String htmlStr = writer.toString();
FileUtils.write(pathname, htmlStr);
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
return htmlStr;
}
}
相关文章推荐
- 生成真静态页面
- 动态创建服务器控件,获取HTML输出(用于生成静态文件,简化页面,简化编程等)
- 几种静态页面生成的代码,包括带内容分页的生成静态
- 动态页面生成静态页面的原理
- 利用Java生成静态HMTL页面
- 静态页面生成错误处理机制
- (转载)静态页面的生成
- ASP新闻分页,将一篇过长的文章分页,生成静态页面
- 生成静态页面——C#+asp.net
- php生成静态html页面的简单实现
- 使用ASP.NET模板生成HTML静态页面的五种方案
- Url地址重写,利用HttpHander手工编译页面并按需生成静态HTML文件
- ASP.NET 生成HTML静态页面实例
- Url地址重写,利用HttpHander手工编译页面并按需生成静态HTML文件
- 把php生成静态(html)页面程序代码
- 用XML和XSLT来生成静态的HTML页面
- java静态页面生成技术-批量数据生成
- 动态网站页面的静态生成方法
- Asp.net如何生成html静态页面
- DEDECMS织梦系统生成静态页面提示“模板文件不存在,无法解析文档”