静态页面:引入的jsp文件的path路径有问题的解决方案
例如
处理完后的效果
引入工具类 注意工具中静态页也面的生产位置
FMutil.java
package cn.taxue.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class FMutil {
/**
*
* @param ftlName
* :模板名称
* @param fileName
* :生成的页面名称
* @param map
* :数据
* @throws Exception
*/
public void ouputFile(String ftlName, String fileName,
Map<String, Object> map) throws Exception {
// 创建fm的配置
Configuration config = new Configuration();
// 指定默认编码格式
config.setDefaultEncoding("UTF-8");
// 设置模板的包路径,包的路径使用/来分隔
config.setClassForTemplateLoading(this.getClass(), "/ftl");//这里也要改 例如"/com/taxue/ft1"
// 获得包的模板
Template template = config.getTemplate(ftlName);
// 指定文件输出的路径
String path = "D:/fm";//注意这是静态页面生产的位置
// 定义输出流,注意的必须指定编码
Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(new File(path + "/" + fileName)), "UTF-8"));
// 生成模板
template.process(map, writer);
}
}
生成静态页面 一般实现是在serverImpl实现 用到工具类工具类 思路是 通过前台也页面的产品id从后台查询所有有关产品的所有的特性
例如:serviceImpl 方法类
public void generStaticPage(Long itemId) throws Exception {
EbItem item = itemDao.getProductDetail(itemId);
Map<String, Object> map = new HashMap<String, Object>();
map.put("item", item);
FMutil fm = new FMutil();
fm.ouputFile("productDetail.ftl", itemId+".html", map);//注意:里面的参数
}
测试类
public class EbBrandServiceTest {
ApplicationContext ctx;
@Before
public void setUp() throws Exception {
ctx = new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void testSelectBrandAll() throws Exception {
EbItemService service = (EbItemService) ctx.getBean("ebItemServiceImpl");
service.generStaticPage(3676l);
}
}
一般前台页面出现的问题有
1.产品价格:可能会三位三位的分 解决方案有到模板中把产品中 有()括起来 加个问号,
例如:<a href="javascript:void(0);" class="here" skuId={skuPrice=$(sku.skuPrice)?c}</a>
2.前台数据需要少量的校验:
例如:产品的数量需要校验
解决方案是考虑利用ajax的的异步请求的方式