控制器由servlet实现,页由freeMark完成。
第一个例子:
test.ftl如下:
<html>
<head>
<title>FreeMarker Example Web Application 1</title>
</head>
<body>
${message}
</body>
</html>
servlet如下:
public class HelloServlet extends HttpServlet {
private Configuration cfg;
public void init() {
cfg = new Configuration();
cfg.setServletContextForTemplateLoading(getServletContext(),
"templates");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Map root = new HashMap();
root.put("message", "Hello World!");
Template t = cfg.getTemplate("test.ftl");
System.out.println(t.getEncoding());//GBK编码
response.setContentType("text/html; charset=" + t.getEncoding());
Writer out = response.getWriter();
try {
t.process(root, out);
} catch (TemplateException e) {
throw new ServletException(
"Error while processing FreeMarker template", e);
}
}
}
web.xml配置如下:
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.mengya.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
测试:
<body>
<a href="hello">FreeMark测试</a>
</body>
第二个例子:(get,set方法省掉,辅助类)
public class Page {
private String template;
private String forward;
private Map root = new HashMap();
}
共同的servlet如下:
public class ControllerServlet extends HttpServlet {
private Configuration cfg;
public void init() {
cfg = new Configuration();
//设置ftl文件加载的路径
cfg.setServletContextForTemplateLoading(getServletContext(),
"templates");
//设置freeMark更新缓存的时间//0表示不用缓存
cfg.setTemplateUpdateDelay(0);
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
//设置freeMark的包裹策略
cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
cfg.setDefaultEncoding("ISO-8859-1");
cfg.setOutputEncoding("UTF-8");
cfg.setLocale(Locale.US);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//URL:http://127.0.0.1:8080/FreeMarkStudy/templates/index.a
//request.getServletPath:/templates/index.a
String action = req.getServletPath();
if (action == null)
action = "index";
if (action.startsWith("/"))
//action = action.substring(1);
action = action.substring(action.lastIndexOf("/")+1);//获得index
if (action.lastIndexOf(".") != -1) {
action = action.substring(0, action.lastIndexOf("."));
}
Method actionMethod;
try {
actionMethod = getClass().getMethod(action + "Action",
new Class[] { HttpServletRequest.class, Page.class });
} catch (NoSuchMethodException e) {
throw new ServletException("Unknown action: " + action);
}
req.setCharacterEncoding(cfg.getOutputEncoding());
Page page = new Page();
try {
actionMethod.invoke(this, new Object[] { req, page });
} catch (IllegalAccessException e) {
throw new ServletException(e);
} catch (InvocationTargetException e) {
throw new ServletException(e.getTargetException());
}
if (page.getTemplate() != null) {
Template t = cfg.getTemplate(page.getTemplate());
resp.setContentType("text/html; charset=" + cfg.getOutputEncoding());
resp.setHeader("Cache-Control",
"no-store, no-cache, must-revalidate, "
+ "post-check=0, pre-check=0");
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Expires", "Thu, 01 Dec 1994 00:00:00 GMT");
Writer out = resp.getWriter();
try {
t.process(page.getRoot(), out);
} catch (TemplateException e) {
throw new ServletException(
"Error while processing FreeMarker template", e);
}
} else if (page.getForward() != null) {
RequestDispatcher rd = req.getRequestDispatcher(page.getForward());
rd.forward(req, resp);
} else {
throw new ServletException("The action didn't specified a command.");
}
}
}
实体Bean如下:(get,set方法省掉)
public class GuestbookEntry {
private String name;
private String email;
private String message;
}
servlet如下:
public class GuestbookServlet extends ControllerServlet {
private ArrayList guestbook = new ArrayList();
public void indexAction(HttpServletRequest req, Page p) {
List snapShot;
synchronized (guestbook) {
snapShot = (List) guestbook.clone();
}
p.put("guestbook", snapShot);
p.setTemplate("index.ftl");
}
public void formAction (HttpServletRequest req, Page p)
throws IOException, ServletException {
p.put("name", noNull(req.getParameter("name")));
p.put("email", noNull(req.getParameter("email")));
p.put("message", noNull(req.getParameter("message")));
List errors = (List) req.getAttribute("errors");
p.put("errors", errors == null ? new ArrayList() : errors);
p.setTemplate("form.ftl");
}
public void addAction (HttpServletRequest req, Page p)
throws IOException, ServletException {
List errors = new ArrayList();
String name = req.getParameter("name");
String email = req.getParameter("email");
String message = req.getParameter("message");
if (isBlank(name)) {
errors.add("You must give your name.");
}
if (isBlank(message)) {
errors.add("You must give a message.");
}
if (errors.isEmpty()) {
if (email == null) email = "";
GuestbookEntry e = new GuestbookEntry(name.trim(), email.trim(), message);
synchronized (guestbook) {
guestbook.add(0, e);
}
p.put("entry", e);
p.setTemplate("add.ftl");
} else {
req.setAttribute("errors", errors);
p.setForward("form.a");
}
}
public static String noNull(String s) {
return s == null ? "" : s;
}
public static boolean isBlank(String s) {
return s == null || s.trim().length() == 0;
}
}
web.xml配置如下:
<servlet>
<servlet-name>guestbook</servlet-name>
<servlet-class>com.mengya.servlet.GuestbookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>guestbook</servlet-name>
<url-pattern>*.a</url-pattern>
</servlet-mapping>
共同的ftl如下:
<#macro page title>
<html>
<head>
<title>FreeMarker Example Web Application 2 - ${title?html}</title>
<meta http-equiv="Content-type" content="text/html; charset=${.output_encoding}">
</head>
<body>
<h1>${title?html}</h1>
<hr>
<#nested>
<hr>
<table border="0" cellspacing=0 cellpadding=0 width="100%">
<tr valign="middle">
<td align="left">
<i>FreeMarker Example 2</i>
</td>
<td align="right">
<a href="http://freemarker.org"><img src="poweredby_ffffff.png" border=0></a>
</td>
</tr>
</table>
</body>
</html>
</#macro>
页面ftl文件一:index.ftl(做list用)
<#import "/common/common.ftl" as com>
<#escape x as x?html>
<@com.page title="Index">
<a href="form.a">Add new message</a> | <a href="help.html">How this works?</a>
<#if guestbook?size = 0>
<p>No messages.
<#else>
<p>The messages are:
<table border=0 cellspacing=2 cellpadding=2 width="100%">
<tr align=center valign=top>
<th bgcolor="#C0C0C0">Name</th>
<th bgcolor="#C0C0C0">Message</td>
</tr>
<#list guestbook as e>
<tr align=left valign=top>
<td bgcolor="#E0E0E0">${e.name} <#if e.email?length != 0> (<a href="mailto:${e.email}">${e.email}</a>)</#if></td>
<td bgcolor="#E0E0E0">${e.message}</td>
</tr>
</#list>
</table>
</#if>
</@com.page>
add.ftl如下:
<#import "/common/common.ftl" as com>
<#escape x as x?html>
<@com.page title="Entry added">
<p>You have added the following entry to the guestbook:
<p><b>Name:</b> ${entry.name}
<#if entry.email?length != 0>
<p><b>Email:</b> ${entry.email}
</#if>
<p><b>Message:</b> ${entry.message}
<p><a href="index.a">Back to the index page...</a>
</@com.page>
</#escape>
from.ftl如下:
<#import "/common/common.ftl" as com>
<#escape x as x?html>
<@com.page title="Add Entry">
<#if errors?size != 0>
<p><font color=red>Please correct the following problems:</font>
<ul>
<#list errors as e>
<li><font color=red>${e}</font>
</#list>
</ul>
</#if>
<form method="POST" action="add.a">
<p>Your name:<br>
<input type="text" name="name" value="${name}" size=60>
<p>Your e-mail (optional):<br>
<input type="text" name="email" value="${email}" size=60>
<p>Message:<br>
<textarea name="message" wrap="soft" rows=3 cols=60>${message}</textarea>
<p><input type="submit" value="Submit">
</form>
<p><a href="index.a">Back to the index page</a>
</@com.page>
分享到:
相关推荐
在IT行业中,静态页面生成是一种常见的优化网页性能的技术,它将动态内容转化为静态...在实际的Web开发中,这样的技术组合有着广泛的应用场景,尤其适用于内容发布系统、博客平台等对页面加载速度有较高要求的项目。
逻辑与:&& 逻辑或:|| 逻辑非:! 逻辑运算符只能作用于布尔值,否则将产生错误 1.9 内建函数 FreeMarker还提供了一些内建函数来转换输出,可以在任何变量后紧跟?,?后紧跟内建函数,就可以通过内建函数来轮换输出...
POV系列-24灯十字旋转LED,资料有原理图、PCB丝印图、 改字软件 以及单片机固件,如果有单片机基础完全可以制作参考制作
大学生创业项目源码
已实现http协议下的请求转发。支持GET,POST请求以及文件上传,支持IP白名单、apiKey配置。
【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、MATLAB、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。
weixin056基于微信小程序的购物系统+php(文档+源码)_kaic
使用mingw编译的openssl-3.4.1,有需要的自取吧
Oracle19c netca.rsp
本资源聚焦前端三剑客基础。课程从 HTML 构建网页结构开始,深入 CSS 样式美化,再到 JavaScript 实现交互逻辑。无论你是零基础小白,还是想巩固基础的学习者,都能通过学习,具备搭建静态网页与简单交互页面的能力,轻松迈进前端开发领域。
Invoke-WmiCommand
python五子棋 转载的!!!
关键词:学科竞赛管理,Java语言,MYSQL数据库,Vue框架 摘 要 I ABSTRACT II 1绪 论 1 1.1研究背景 1 1.2设计原则 1 1.3论文的组织结构 2 2 相关技术简介 3 2.1Java技术 3 2.2B/S结构 3 2.3MYSQL数据库 4 2.4Spring Boot框架 4 2.5Vue框架 5 3 系统分析 6 3.1可行性分析 6 3.1.1技术可行性 6 3.1.2操作可行性 6 3.1.3经济可行性 6 3.1.4法律可行性 6 3.2系统性能分析 7 3.3系统功能分析 7 3.4系统流程分析 8 3.4.1注册流程 8 3.4.2登录流程 9 3.4.3添加信息流程 10 4 系统设计 11 4.1系统概要设计 11 4.2系统结构设计 11 4.3 系统顺序图 12 4.4数据库设计 14 4.4.1 数据库实体(E-R图) 14 4.4.2 数据库表设计 16 5 系统的实现 19 5.1学生功能模块的实现 19 5.1.1 学生注册界面 19 5.1.2 学生登录界面 20 5.1.3 赛项详情界面 21 5.1.4 个人中心界
大学生创业项目源码
开源项目整合包 更多内容可以查阅 项目源码搭建介绍: 《我的AI工具箱Tauri+Django开源git项目介绍和使用》https://datayang.blog.csdn.net/article/details/146156817 图形桌面工具使用教程: 《我的AI工具箱Tauri+Django环境开发,支持局域网使用》https://datayang.blog.csdn.net/article/details/141897682
智慧园区,作为未来城市发展的重要组成部分,正逐步从传统园区向智能化、高效化转型。这一转型不仅提升了园区的运营管理水平,更为入驻企业和民众带来了前所未有的便捷与高效。智慧园区的总体设计围绕现状分析、愿景规划、设计理念及六位一体配套展开。传统园区往往面临服务体系不完善、智慧应用面不广、信息资源共享能力不足等问题,而智慧园区则致力于打破这些壁垒,通过物联网技术、大数据分析等手段,构建起一个完整的运营服务体系。这一体系不仅覆盖了企业成长的全周期,还通过成熟的智慧运营经验,为产业集群的发展提供了有力支撑。智慧园区的愿景在于吸引优秀物联网企业和人才入驻,促进产业转型,提高社会经济效应,并为民众打造更安全、高效的智慧生活方式。 在智慧园区的服务体系及配套方面,园区围绕“1+1+1”(学院+创客+基地)、“两中心”(园区指挥中心+金融中心)、“三平台”(成果展示+招商+政府)等核心配套,辅以日常生活各方面的配套,真正实现了从人才培养、研发、转化、孵化、加速到发展的六位一体示范园区。园区服务体系包括园区运营管理体系、企业服务体系和产业社区服务体系。园区运营管理体系通过协同办公、招商推广、产业分析等手段,打破了信息数据壁垒,构建了统一园区运营服务。企业服务体系则提供了共享智能展厅、会议室预定、园区信息服务、办事大厅等一系列便捷服务,助力企业快速成长。产业社区服务体系则更加注重周边生活的便捷性,如物联网成果展示平台、智慧物流、共享创客空间等,为入驻企业和民众提供了全方位的生活配套。这些服务体系不仅提升了园区的整体竞争力,还为入驻企业创造了良好的发展环境。 智慧园区的场景应用更是丰富多彩,涵盖了智慧停车、智慧访客、公共服务、智慧楼宇、智慧物业等多个方面。智慧停车系统通过车牌识别、车位引导、缴费等子系统,实现了停车场的智能化管理,极大提升了停车效率。智慧访客系统则通过预约、登记、识别等手段,确保了园区的安全有序。公共服务方面,智慧照明、智慧监控、智慧充电桩等设施的应用,不仅提升了园区的整体品质,还为民众带来了更加便捷、安全的生活环境。智慧楼宇和智慧物业系统更是通过智能化手段,实现了楼宇和园区的统一化管理,提升了运营效率和居住舒适度。此外,智慧园区还通过O2O平台、医疗系统、综合服务系统等手段,将线上线下资源有机整合,为入驻企业和民众提供了全方位、便捷的服务体验。这些场景应用不仅展示了智慧园区的智能化水平,更为读者提供了丰富的想象空间和实施方案参考。 综上所述,智慧园区作为未来城市发展的重要方向,正以其独特的魅力和优势吸引着越来越多的关注。通过智能化手段的应用和服务体系的完善,智慧园区不仅提升了园区的整体竞争力和运营效率,还为入驻企业和民众带来了前所未有的便捷与高效。对于写方案的读者来说,智慧园区的解决方案不仅提供了丰富的案例参考和实践经验,更为方案的制定和实施提供了有力的支撑和启示。
成熟STM32直流电压电流采集与检测方案:包含PCB设计、KEIL源码及原理图与详细设计说明,完备STM32直流电压电流采集与检测解决方案:PCB、KEIL源码、原理图、设计说明,lunwen复现新型扩展移相eps调制,双有源桥dab变器,MATLAB simulink仿真 ,核心关键词:lunwen复现; 新型扩展移相eps调制; 双有源桥dab变换器; MATLAB simulink仿真;,复现新型扩展移相EPS调制:DAB双有源桥变换器在MATLAB Simulink中的仿真研究
大学生创业项目源码
清华大学deepseek三部曲PDF