C++ template is clumsy
After knowing that C++ template is actually marco, feel kind of disappointed.
I implemented a generic double linked list to feel what it is like in C++, I have done that in Java.
C++ code is redundant, wherever your class or methods have something to do with the template, need template <class name>. And also C++ template is good for container class only. Your template methods should not touch anything inside your generic part, because this may get you into runtime error.
For example, you have a generic method that use generic class T, and then try to call T->print(), if T has define print(), everything is just fine, otherwise you will get runtime error. Whereas Java have interface to restrict the signature of a generic class. C++ may use inheritance to over come this but it won’t be type safe.
Not to mention C++ multiple inheritance may lead to diamond problem. That is exactly why I learned C, C#, Java before C++, because in my opinion C++ is difficult to use and clumsy. But even so, you will eventually need to use C++ someday if you are not obsessed to a paticular language. But learning C++ is still very funny, it will be easier if you have good memory, but sadly my memory is not so good.
Icefaces Facelets to get a template effect
I am planning to make it dynamic, but haven’t yet done the hands on.
Preparation:
include icefaces-facelets.jar
and put the following into web.xml
<context-param> <param-name>javax.faces.DEFAULT_SUFFIX</param-name> <param-value>.jspx</param-value> </context-param>
This tells JSF to assume a prefix of jspx, which the Facelet’s rendered can interpret. Facelets can use many other parameters depending on your application.
put the following into faces-config.xml
<application> <view-handler>com.icesoft.faces.facelets.D2DFaceletViewHandler </view-handler> </application>
tell JSF about the Facelets view handler. This is a plug-in that handles the Render Response and Restore View phases of the JSF request-processing life cycle.
Template Idea
And then comes the confusing part, I can’t understand this after reading the document twice. I have to jump into the source and do some hands on to figure this out. I think I have a better way to present it. This may be my wrong intepretation.
You need three things.
1. a jsp that load the template
2. a jspx which is the template
3. some jspx which are components you put inside the template.
1. load template
- <ui:composition> – This is a templating tag that wraps content to be included in another facelet. Any content outside the composition tag will be ignored by the Facelets view handler.
- <ui:define> – This tag is a templating tag that defines named content to be inserted into a template.
example:
<ui:composition template="mainTemplate.jspx"> <ui:define name="faceletHeader"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta> <title> <ice:outputText value="Facelet Dynamic Include Tutorial"/> </title> <link href="./xmlhttp/css/xp/xp.css" rel="stylesheet" type="text/css"/> </ui:define> </ui:composition>
2. The template
- <ui:insert>- A templating tag that declares a named content element to be defined by another Facelet. Used effectively with the ui:define tag.
- <ui:include>- A server-side include tag for Facelets. It simply includes the document pointed to by the “src” attribute as part of the current JSF page.
example:
<ui:insert name="content"> <ui:include src="./content-facelet.jspx" /> </ui:insert>
3. Contents
just icefaces jspx that you normally write.
Conclusion:
The confusing part is when you load the template, you will have two way to actually put content inside <ui:insert>.
1. use <ui:define> inside “template load” to put content directly into it.
2. use <ui:include> inside “template” to reference a jspx.
And if I am not getting it wrong, any content inside <ui:insert> which is not <ui:include> will be replace by <ui:define> stuff.
I will update this post when I get the dynamic reference content work.
Have fun~