通过java生成文件毫无疑问会使用到流,对于流在该文章中不做重点解释。首先我们做一个准备工作,搭建你的项目,采用Freemarker模版技术,首先给大家展示一个Freemarker文件。
预定价格: ${trip.price!'面议'}元 目的地: ${trip.toCity!''} 行程天数:${trip.tripDay!'待定'} 提前预定天数: 建议提前一周报名 出团日期:${trip.toTirpTime} 友谊
模版建好了,我们接下来写一个工具类,通过该工具类生成html文件。FreeMarkerUtil.java 此处我们只列出主要用到的方法。
public static void writeTo(ServletContext context, Mapmap, String templatePath, String templateName, String htmlPath) throws IOException, TemplateException { Configuration freemarkerCfg = new Configuration(); // 设置要解析的模板所在的目录,并加载模板文件 freemarkerCfg.setServletContextForTemplateLoading(context, templatePath); freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8"); // 获取模板,并设置编码方式 Template template = freemarkerCfg.getTemplate(templateName); template.setEncoding("UTF-8"); File htmlFilel = new File(context.getRealPath("") +File.separator +"html"+File.separator+"channel"); if(! htmlFilel.exists()) { htmlFilel.mkdir(); } File htmlFile = new File(context.getRealPath("") + File.separator + htmlPath); Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(htmlFile), "UTF-8")); // 合并数据模型与模板 template.process(map, out); out.flush(); out.close(); }
需要导入的类,这些都在freemarker-2.3.16.jar 包中了:
import freemarker.template.Configuration;import freemarker.template.DefaultObjectWrapper;import freemarker.template.Template;import freemarker.template.TemplateException;
接下来就是业务处理了,其实很简单的没有任何的技术含量,这个功能也是在使用Freemarker做模版视图较多使用的,此处为了让学习的人看的明白,业务和控制层都写在一个方法了,容易理解。
public ModelAndView doAdd(HttpServletRequest request, MultipartHttpServletRequest filerequest, Trip trip) throws ParseException { Listfile1 = filerequest.getFiles("file1"); Listfile2 = filerequest.getFiles("file2"); if (file1.size() > 0) { String titleImg = ""; titleImg = uploadImage(request, file1.get(0)); if (null != titleImg && !titleImg.equals("")) { trip.setImgUrl(titleImg); } } if (file2.size() > 0) { String titleImg = ""; titleImg = uploadImage(request, file2.get(0)); if (null != titleImg && !titleImg.equals("")) { trip.setImgUrl2(titleImg); } } trip.setInputTime(new Date()); File directory = new File(request.getSession().getServletContext() .getRealPath("/") + "trip"); if (!directory.exists()) { directory.mkdirs(); } DateFormat format1 = new SimpleDateFormat("yyyyMMddhhmm"); String dateStr = format1.format(new Date()); String html = "trip" + File.separator + trip.getChannelId() + "-" + trip.getCityId() + "-" + dateStr + ".html"; trip.setUrlStr(html); PageBean pageBean3 = tripService.getTriplist(0, 10); ListtripList = pageBean3.getBeanList(); PageBean pageBean2 = newsService.getTripNewslist(1, 10); ListtripNewsList = pageBean2.getBeanList(); PageBean pageBean22 = newsService.getTripNews2list(1, 10); ListtripNews2List = pageBean22.getBeanList(); Map map = new HashMap(); map.put("tripNews2List", tripNews2List); map.put("tripList", tripList); map.put("tripNewsList", tripNewsList); map.put("trip", trip); map.put("base", request.getContextPath()); try { FreeMarkerUtil.writeTo(request.getSession().getServletContext(), map, "/templates", "trip.ftl", html); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TemplateException e) { // TODO Auto-generated catch block e.printStackTrace(); } tripService.save(trip); return new ModelAndView("redirect:/trip/page.shtml?page_now=1"); }
好了,这样你就可以写你的静态网站了。
也许有人现在也不知道生成静态的作用到底是什么呢?
1、访问的时候不需要在操作数据库了。
2、对于搜索引擎来说html静态页面更加的友好。
3、访问速度变的很快。
等等,大家自己感受一下。有需要可留言要源码demo。
本文地址:http://www.osblog.net/code/117.html

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.