模板式生成 JSP页面步骤

来源:互联网 发布:阿里大数据平台 编辑:程序博客网 时间:2024/06/08 08:53
一。准备工作

1.项目中必须有  spring-webmvc-3.2.2.RELEASE.jar  架包。因为等会会用到 里面的 FreeMarkerConfigurer 类。
2.需要在  spring-mvc.xml 文件中配置如下信息:
<bean id="freemarkerConfig"
  class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  <property name="templateLoaderPath" value="/WEB-INF/" />
  <property name="freemarkerSettings">
   <props>
    <prop key="template_update_delay">0</prop>
    <prop key="defaultEncoding">UTF-8</prop>
    <prop key="url_escaping_charset">UTF-8</prop>
    <prop key="locale">zh_CN</prop>
    <prop key="boolean_format">true,false</prop>
    <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
    <prop key="date_format">yyyy-MM-dd</prop>
    <prop key="time_format">HH:mm:ss</prop>
    <prop key="number_format">0.######</prop>
    <prop key="whitespace_stripping">true</prop>
   </props>
  </property>
 </bean>

3.创建html模板。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>个人信息</title>
 
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  </head>
 
  <body>
     <div>
      姓名:${name}
     </div>
     <div>
      昵称:${nickname}
     </div>
      <div>
      地区:${area}
     </div>
      <div>
      性别:${sex}
     </div>
     <div>
      年龄:${age}
     </div>
  </body>
</html>


二。代码编写
1.设置响应的格式
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");

2.获取PrintWriter流,(用来在客户端输出)
PrintWriter writer = null;
writer = response.getWriter();

3.创建一个模板对象
Template tpl2 = null;

4.创建一个 StringWriter 类 ,代表一个字符流,可以用其回收在字符串缓冲区中的输出,来构造字符串。
 StringWriter out = null;

5.运用freemarkerConfig 类 制定模板路径生成模板,并赋值给模板对象 Template tpl2
Configuration config = freemarkerConfig.getConfiguration(); 
此处需要在上方导入在spring-mvc.xml 中配置好的类。
@Autowired
 protected FreeMarkerConfigurer freemarkerConfig;

tpl2 = config.getTemplate("/tpl/MyHtml.html");

6.创建Map 用来放入模板中需要的值,此处自己按需求灵活处理。
Map<String, Object> map = new HashMap<String, Object>();
   map.put("name", "***");
   map.put("nickname", "Hunter");
   map.put("area", "上海");
   map.put("sex", "男");
   map.put("age", 25);
   Map<String, Object> rootMap = new HashMap<String, Object>();
   rootMap.putAll(map);

7.新建StringWriter类 ,代表一个字符流,可以用其回收再字符串缓冲区中的输出,来构造字符串。
out = new StringWriter();

8.将数据模型和模板合并。
tpl2.process(rootMap, out);

9.往页面返回 构建的模板字符串
writer.write(out.toString());

此致就能在页面灵活输出你想要的页面了。

简易代码如下:

package cn.com.hunter.controller;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import freemarker.template.Configuration;
import freemarker.template.Template;
@Controller
@RequestMapping(value="/Template")
public class TemplateController {
 
 @Autowired
 protected FreeMarkerConfigurer freemarkerConfig;
 
 @RequestMapping(value="/Template.action")
 public void TemplateWtite(HttpServletRequest request,HttpServletResponse response){
  PrintWriter writer = null;
  try {
   response.setCharacterEncoding("utf-8");
   response.setContentType("text/html; charset=utf-8");
   writer = response.getWriter();
   Template tpl2 = null;
   StringWriter out = null;
   Configuration config = freemarkerConfig.getConfiguration();
   Map<String, Object> map = new HashMap<String, Object>();
   map.put("name", "红涛");
   map.put("nickname", "Hunter");
   map.put("area", "上海");
   map.put("sex", "男");
   map.put("age", 25);
   Map<String, Object> rootMap = new HashMap<String, Object>();
   rootMap.putAll(map);
   out = new StringWriter();
   tpl2 = config.getTemplate("/tpl/MyHtml.html");
   tpl2.process(rootMap, out);
   
   writer.write(out.toString());
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   if (writer != null)
    writer.close();
  }
 }
}





1 0
原创粉丝点击