[Project Summary: Boston Alumni Association] Use freemaker to achieve static front-end pages

Source: Internet
Author: User

First, the index of the result returned by the original acceptance action. JSP is divided into three parts, top. JSP, body. JSP, bottom. JSP to generate and include each part separately through the freemaker template.

This article takes generating body. jsp as an example to explain the entire process.


Step 1: Convert the dynamic body. jsp of the original homepage to body. FTL:

The conversion result is as follows:

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <# macro indextopiclist indextopic titlesize showdate = true hash = false hasdt = true showdot = true df =" mm/DD "divid = "" hrefclz = ""> <Div $ {(divid = "")? String ("", "id = '$ {divid}'")}> <# If hash> <# nested/> </# If> <DL> <# If hasdt> <DT> <# nested/> </DT> </# If> <# list indextopic. topics as topic> <DD> <a Title = "$ {topic. title} "href =" Topic/$ {topic. id} "$ {(hrefclz = "")? String ("", "class = '$ {hrefclz}'") }>< # If showdate> [$ {(topic. publishdate )? String ("$ {DF}")}] </# If> <# If topic. Title? Length GT titlesize >$ {topic. title [0 .. titlesize]} <# If showdot>... </# If> <# else >$ {topic. title }</# If> </a> </DD> </# list> </dl> </div> </# macro> <Div id = "content"> <Div id = "content_con"> <Div id = "xiaoxun"> </div> <Div id = "notice_rollpic"> <@ indextopiclist indextopic = ts ["1"] titlesize = 12 divid = "notice" hrefclz = "index_link"> <span> <a href = "channel/$ {ts [" 1 "]. CID} "class =" index_title_href ">$ {ts [" 1 "]. cname} </a> </span> </@ indextopiclist> <Div id = "rollpic"> <Div id = "rollcaption"> <span> </ div> <Div id = "rollpager"> </div> <# list pics as PIC> <a href = "$ {pic. linkurl} "Title =" $ {pic. title} "> /resources/indexpic/$ {pic. newname} "border =" 0 "/> </a> </# list> </div> <Div id =" split_line "> </div> <div id = "xwgk_xxgk"> <@ indextopiclist indextopic = ts ["2"] hash = true hasdt = false titlesize = 37 divid = "xwgk" hrefclz = "index_link"> 

Place the body. FTL and other template files in the resources folder of the project's classpath for subsequent calls.


Step 2: Write the template to the static JSP interface and implement it as follows:

package org.konghao.cms.service;public interface IIndexService {public void generateTop();public void generateBottom();public void generateBody();}

Implementation: generate static JSP to the specified path.

Package Org. konghao. CMS. service; import Java. util. hashmap; import Java. util. list; import Java. util. map; import Java. util. properties; import javax. inject. inject; import Org. directwebremoting. UI. servlet. baseutilhandler; import Org. konghao. basic. model. systemcontext; import Org. konghao. basic. util. freemarkerutil; import Org. konghao. basic. util. propertiesutil; import Org. konghao. CMS. model. baseinfo; import Org. konghao. CMS. model. channel; import Org. konghao. CMS. model. channeltype; import Org. konghao. CMS. model. indextopic; import Org. konghao. CMS. model. topic; import Org. konghao. CMS. web. baseinfoutil; import Org. springframework. beans. factory. annotation. autowired; import Org. springframework. stereotype. service; @ Service ("Indexservice") public class Indexservice implements iindexservice {private string outpath; private freemarkerutil util; @ autowired (required = true) Public Indexservice (string ftlpath, string outpath) {super (); If (util = NULL) {This. outpath = outpath; util = freemarkerutil. getinstance (ftlpath) ;}} private ichannelservice channelservice; private itopicservice topicservice; private iindexpicservice indexpicservice; private ikeywordservice keyworkservice; Public ikeywordservice getkeyworkservice () {return keyworkservice ;} @ injectpublic void setkeyworkservice (ikeywordservice keyworkservice) {This. keyworkservice = keyworkservice;} public iindexpicservice getindexpicservice () {return indexpicservice;} @ injectpublic void setindexpicservice (iindexpicservice indexpicservice) {This. indexpicservice = indexpicservice;} public itopicservice gettopicservice () {return topicservice;} @ injectpublic void settopicservice (itopicservice topicservice) {This. topicservice = topicservice;} public ichannelservice getchannelservice () {return channelservice;} @ injectpublic void setchannelservice (ichannelservice channelservice) {This. channelservice = channelservice;} @ overridepublic void generatetop () {system. out. println ("============== the top information is regenerated ============================== = "); list <channel> cs = channelservice. listtopnavchannel (); Map <string, Object> root = new hashmap <string, Object> (); root. put ("navs", CS); root. put ("baseinfo", baseinfoutil. getinstacne (). read (); string OUTFILE = systemcontext. getrealpath () + outpath + "/top. JSP "; util. fprint (root, "/top. FTL ", OUTFILE) ;}@ overridepublic void generatebottom () {system. out. println ("================ the bottom information is regenerated = "); map <string, Object> root = new hashmap <string, Object> (); root. put ("baseinfo", baseinfoutil. getinstacne (). read (); string OUTFILE = systemcontext. getrealpath () + outpath + "/bottom. JSP "; util. fprint (root, "/bottom. FTL ", OUTFILE) ;}@ overridepublic void generatebody () {system. out. println ("========== regenerate the content of the home page ================ "); // 1. Obtain the list of all homepage columns <channel> cs = channelservice. listallindexchannel (channeltype. topic_list); // 2. Create an indextopic object based on the homepage. // load the indexchannel. propertiesproperties prop = propertiesutil. getinstance (). load ("indexchannel"); Map <string, indextopic> topics = new hashmap <string, indextopic> (); For (channel C: CS) {int cid = C. GETID (); string [] xs = prop. getproperty (CID + ""). split ("_"); string order = Xs [0]; int num = integer. parseint (XS [1]); indextopic it = new indextopic (); it. setcid (CID); it. setcname (C. getname (); List <topic> tops = topicservice. listtopicbychannelandnumber (CID, num); // system. out. println (CID + "--" + tops); it. settopics (TOPs); topics. put (Order, it);} string OUTFILE = systemcontext. getrealpath () + outpath + "/body. JSP "; // 3. Update the homepage image baseinfo Bi = baseinfoutil. getinstacne (). read (); int picnum = Bi. getindexpicnumber (); Map <string, Object> root = new hashmap <string, Object> (); root. put ("ts", topics); root. put ("pics", indexpicservice. listindexpicbynum (picnum); root. put ("keywords", keyworkservice. getmaxtimeskeyword (12); root. put ("xxgk", topicservice. loadlastedtopicbycolumn (7); util. fprint (root, "/body. FTL ", OUTFILE );}}

The generated static body. jsp fragment is as follows:

<Div id = "content"> <Div id = "content_con"> <Div id = "xiaoxun"> </div> <Div id = "notice_rollpic"> <Div id = "'Notice '> <DL> <DT> <span> <a href = "channel/10" class = "index_title_href"> campus news </a> </span> </ DT> <DD> <a Title =" 4" href = "Topic/28" class = 'index _ link'> [10/21] 4 4 </a> </DD> <a Title = "campus news Test 3" href = "Topic/37" class = 'index _ link'> [10/21] campus news Test 3 </ a> </DD> <a Title = "campus news Test 2" href = "Topic/21" class = 'index _ link'> [10/21] campus news Test 2 </a> </DD> <a Title = "1 1" href = "Topic/9" class = 'index _ link'> [10/21] test campus news 1 </a> </DD> <a Title = "campus news Test 5" href = "Topic/33" class = 'index _ link'> [10/21] Test campus news 5 </a> </DD> <a Title = "Article Test 2" href = "Topic/18" class = 'index _ Link '> [10/21] Article Test 2 </a> </DD> <a Title = "test article 1" href = "Topic/14" class = 'index _ link '> [10/21] test article 1 </a> </DD> <a Title = "test image" href = "Topic/3" class = 'index _ link '> [10/21] test image </a> </DD> </dl> </div>

Generate top. jsp and bottom. jsp respectively.


Step 3. Add the newly generated static body. jsp, top. jsp, and bottom. jsp to the index. jsp homepage JSP according to the split sequence of the original index. jsp.

The snippets are as follows:

Index. jsp

<body><jsp:include page="/jsp/template/top.jsp"/><jsp:include page="/jsp/template/body.jsp"/><jsp:include page="/jsp/template/bottom.jsp"/></body>

Summary:

In the background, after each update of news content, you can call the static Generation Service button through the static generation function. When you access the homepage, you do not have to retrieve the database content through control every time you refresh the page.

Instead, you can directly access the generated static page without the action returned in the static page. The speed is greatly improved!




[Project Summary: Boston Alumni Association] Use freemaker to achieve static front-end pages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.