模板式生成 JSP页面步骤

这篇博客介绍了如何利用FreeMarker模板引擎生成JSP页面。首先,项目需要包含spring-webmvc-3.2.2.RELEASE.jar,并在spring-mvc.xml配置FreeMarker设置。接着,创建HTML模板文件,包含姓名、昵称、地区、性别和年龄等字段。然后,通过编写Java代码设置响应格式,获取PrintWriter流,创建模板对象并填充数据模型。最后,将数据模型与模板合并,返回生成的页面内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一。准备工作

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();
  }
 }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值