Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 301|回复: 0

[servlet学习]两种方法组合Servlet + Freemarker

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-10 05:59:29 | 显示全部楼层 |阅读模式
    Servlet的轻巧高效,freemarker的强大简便,两者结合将是超轻的组合,即可避免丑陋的java代码和HTML代码杂揉,又可高效基于模板的站点开发。闲话少说,项目需要: freemarker-2.3.13.jar 定义两个Servlet: HelloAction.java 对应 /hello,借助Freemarker硬编码输出 public class HelloAction extends HttpServlet {
         private static final long serialVersionUID = -6082007726831320176L;      private Configuration configuration;
         public void init() throws ServletException {
             configuration = new Configuration();
             configuration.setServletContextForTemplateLoading(getServletContext(), "WEB-INF/pages");
             configuration.setEncoding(Locale.CHINA, "UTF-8");
         }     @SuppressWarnings("unchecked")
         public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
             // 填充数据类型
             Map map = new HashMap();
             map.put("userName", "小敏");   
             Template template = configuration.getTemplate("hello.html");
             response.setContentType("text/html; charset=" + template.getEncoding());
             Writer out = response.getWriter();
             try{
                 template.process(map, out);
             }catch (TemplateException e) {
                 e.printStackTrace();
             }
         }      public void destroy() {
             super.destroy();
             if(configuration != null){
                 configuration = null;
             }
         }
    } 对应模板: <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>使用Freemarker渲染2</title>
    </head>
    <body>
    你好, $ {userName!} !
    </body>
    </html> HiAction.java 对应 /hi ,借助Freemrker Servlet的拦截功能,如以往写代码方式,感觉不到Freemarker的存在。 public class HiAction extends HttpServlet {
         private static final long serialVersionUID = 518767483952153077L;      public void doGet(HttpServletRequest request, HttpServletResponse response)
                 throws ServletException, IOException {          request.setAttribute("thename", "小敏");
             request.getRequestDispatcher("/WEB-INF/pages/hi.html").forward(request, response);
         }
    } 对应的模板: <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>使用Freemarker渲染</title>
    </head>
    <body>
    hi $ {thename!}~<br />
    </body>
    </html> 但需要在web.xml 配置文件中定义如下: <servlet>
         <servlet-name>freemarker</servlet-name>
         <servlet-class>
             freemarker.ext.servlet.FreemarkerServlet
         </servlet-class>      <!-- FreemarkerServlet settings: -->
         <init-param>
             <param-name>TemplatePath</param-name>
             <param-value>/</param-value>
         </init-param>
         <init-param>
             <param-name>NoCache</param-name>
             <param-value>true</param-value>
         </init-param>
         <init-param>
             <param-name>ContentType</param-name>
             <param-value>text/html; charset=UTF-8</param-value>
             <!-- Forces UTF-8 output encoding! -->
         </init-param>      <!-- FreeMarker settings: -->
         <init-param>
             <param-name>template_update_delay</param-name>
             <param-value>0</param-value><!-- 0 is for development only! Use higher value otherwise. -->
         </init-param>
         <init-param>
             <param-name>default_encoding</param-name>
             <param-value>UTF-8</param-value><!-- The encoding of the template files. -->
         </init-param>
         <init-param>
             <param-name>number_format</param-name>
             <param-value>0.##########</param-value>
         </init-param>      <load-on-startup>1</load-on-startup>
    </servlet>  <servlet-mapping>
         <servlet-name>freemarker</servlet-name>
         <url-pattern>*.html</url-pattern>
    </servlet-mapping>  使用哪一种组合方式,看您喜好了。 借助于Freemarker自身的Servlet工具,只是用于拦截Servlet中forward转向使用到的HTML资源文件。 很简陋,但凑合着能看。

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/10/055929500.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-23 00:19 , Processed in 0.401321 second(s), 52 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表