本博客介绍,指定条件下生成静态html页面,生成后,网站不再访问jsp页面,而是访问html.
举例:一个商场购物网站,商品上架时候,发送商品id到activeMQ,生成静态页面服务器监听activeMQ消息,并生成静态页面,后续多个服务器访问产品详情时候,不再需要进jsp,只需要跳到对应的html即可。
另freemark简单使用方法:导入freemarker-2.3.16.jar,见附件:http://download.csdn.net/download/w20228396/10262877
下面结合具体商城案例使用:
1、制作静态模板
对于原产品详情的jsp页面进行改写,去掉c标签,部分标签换为freemarker的标签,如:遍历用
<#list persons as person>
${person_index} ${person} <br/>
</#list>
引入其他页面,如上下左右等边角页面用
<#include "commons/footer.html" />
加入UTF-8
<meta charset="UTF-8">
2、编写freemarker.xml配置文件,放在生成静态页面的项目中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--静态化的服务 -->
<bean id="staticPageService" class="cn.dapeng.core.service.staticpage.StaticPageServiceImpl">
<!-- 驱springmvc提供的freemarkerConfigurer -->
<!-- 可以把bean写到property里一层,也可以通过ref引入 -->
<property name="freeMarkerConfigurer">
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- 设置模板所在文件夹 -->
<property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
<!-- 设置默认编码 -->
<property name="defaultEncoding" value="UTF-8"/>
</bean>
</property>
</bean>
</beans>
3、产品上架时,需要将产品的ID群发给各个服务器,这里产品的service配置改为主题
<bean id="jmsTempalte" class="org.springframework.jms.core.JmsTemplate">
<!-- 注入Spring的工厂 -->
<property name="connectionFactory" ref="connectionFactory"/>
<!-- JmsTempalte操作某个通道,把通道名称给它,即配置默认目标:商品id 如果用其他目标则jmsTemplate.send(destination, messageCreator)-->
<property name="defaultDestinationName" value="productId"/>
<!--默认不设置是队列 ; 设置就是发送主题 -->
<property name="pubSubDomain" value="true"/>
</bean>
4、生成静态页面的项目中,需要监听器,监听activeMQ发送过来的产品ID,编写mq.xml,配置监听类,配置接收消息为主题
<!-- 消息处理类 -->
<bean id="customMessageListener" class="cn.dapeng.core.mq.CustomMessageListener"/>
<!--Spring监听MQ 没人需要调它,没人需要注入他,所以不要id -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<!--1、连接Mq进行监听 -->
<property name="connectionFactory" ref="connectionFactory"/>
<!--2、监听的目标地点 -->
<property name="destinationName" value="productId"/>
<!--3、接收消息 -->
<property name="messageListener" ref="customMessageListener"/>
<!--4、默认不设置是队列 ; 设置就是发送主题 -->
<property name="pubSubDomain" value="true"/>
</bean>
5、生成静态页面的项目中,监听类CustomMessageListener,监听得到产品ID,并且根据id查出详情列表需要的信息,然后放到root中。
public class CustomMessageListener implements MessageListener {
@Autowired
private StaticPageService staticPageService;
@Autowired
private CmsService cmsService;
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
ActiveMQTextMessage aMessage = (ActiveMQTextMessage) message;
try {
String productId = aMessage.getText();
System.out.println("cms:"+productId);
//数据模型
Map<String, Object> root = new HashMap<>();
// 查询商品
Product product = cmsService.selectProductById(Long.parseLong(productId));
// 查询库存
List<Sku> skus = cmsService.selectSkuListByProductId(Long.parseLong(productId));
// 去掉颜色重复
Set<Color> colors = new HashSet<Color>();
for (Sku sku : skus) {
colors.add(sku.getColor());
}
root.put("product", product);
root.put("skus", skus);
root.put("colors", colors);
staticPageService.productStaticPage(root, productId);
//solrService.
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
6、staticPageService的实现类中生成对应产品的静态页面
* 静态化服务类
* @author Administrator
*
*/
public class StaticPageServiceImpl implements StaticPageService,ServletContextAware{
//声明(注入 freeMarkerConfigurer 得到 conf)
private Configuration conf;
//private FreeMarkerConfigurer freeMarkerConfigurer;
public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {
this.conf = freeMarkerConfigurer.getConfiguration();
}
//静态化程序
@Override
public void productStaticPage(Map<String, Object> root, String id) {
//获取的绝对路径E:\eclipse-micservice\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\
// wtpwebapps\babasport-service-cms/html/product/12131313.html
String path = getPath("/html/product/"+id+".html");
File f = new File(path);
File parentFile = f.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
//加载目录下指定的模板文件
Writer out = null;
try {
//读取模板(UTF-8)
Template template = conf.getTemplate("productDetail.html");
//输入流 写UTF-8
out = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
// 处理process
template.process(root, out);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if (null != out) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 获取项目应用的路径
public String getPath(String name) {
return servletContext.getRealPath(name);
}
//声明
private ServletContext servletContext;
@Override
public void setServletContext(ServletContext servletContext){
this.servletContext = servletContext;
}
}
7、静态页面生成后,修改链接地址,点击详情页面跳转到对应的静态页面
<div class="p-name p-name-type-2">
<a title="满129立减10,199减20优惠券,支持货到付款" href="javascript:;" onclick="window.open('http://localhost:8084/html/product/${product.id}.html')">
<em>${product.name }</em>
</a>
</div>
8、结束,启动项目去验证