Someone may ask, "Why tiles? Why does it use JSF instead of struts ?". If you are familiar with the application of tiles in struts, you will not ask the first question. Similarly, if you have used JSF, I believe that you will give priority to JSF rather than struts in future projects. (In fact, I agree with the complementarity between struts and JSF. Each of them has its own advantages. The powerful logic control of struts makes it more suitable for systems with simple logic and complex control logic, however, JSF is equally expressive at the view level.
Okay, no more nonsense. Let's see how tiles is integrated into JSF. Note: I will not talk much about the configuration of JSF. If you don't know how to run JSF, don't waste time.
1. Test struts. jar in struts1.1 to the lib directory.
2. Add the following configuration in Web. xml:
<Servlet>
<Servlet-Name> faces servlet </servlet-Name>
<Servlet-class> javax. Faces. webapp. facesservlet </servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet>
<Servlet-Name> tilesservet </servlet-Name>
<Servlet-class> org. Apache. Struts. Tiles. tilesservlet </servlet-class>
<Init-param>
<Param-name> definitions-config </param-name>
<Param-value>/WEB-INF/tiles-defs.xml </param-value>
</Init-param>
<Init-param>
<Param-name> definitions-parser-validate </param-name>
<Param-value> true </param-value>
</Init-param>
<Load-on-startup> 2 </load-on-startup>
</Servlet>
The blue part is what you want to add. Pay attention to the load sequence of the servlet above.
3. Add tiles-defs.xml to/WEB-INF/, this configuration file is no different from the previous in struts, a typical configuration is as follows:
<! DOCTYPE tiles-definitions PUBLIC
"-// Apache Software Foundation // DTD Tiles Configuration // EN"
Http://jakarta.apache.org/struts/dtds/tiles-config.dtd>
<Tiles-definitions>
<Definition name = "report. main" path = "/JSPROOT/templates/topLayout. jsp">
<Put name = "header" value = "/JSPROOT/templates/header. jsp"> </put>
<Put name = "menu" value = "/JSPROOT/templates/menu. jsp"> </put>
<Put name = "footer" value = "/JSPROOT/templates/footer. jsp"> </put>
<Put name = "body" value = "/JSPROOT/reports/report_main.jsp"> </put>
</Definition>
<Definition name = "report. summary" extends = "report. main">
<Put name = "body" value = "/jsproot/reports/summary_report.jsp" type = "page"> </put>
</Definition>
</Tiles-Definitions>
Two views, one report. Main and one report. Summary are defined above.
4. compile your own layout file, header, footer, and so on. A typical layout is as follows:
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-tiles" prefix = "tiles" %>
<HTML>
<Head>
<Title>
<Tiles: getasstring name = "Header"/>
</Title>
</Head>
<Body bgcolor = "# ffffff" topmargin = "0" leftmargin = "0" marginwidth = "0" marginheight = "0" onkeypress = "Return killent ();">
<Table width = "100%" border = "0" cellpadding = "0" cellspacing = "0">
<Tr>
<TD colspan = "2" align = "Justify">
<Tiles: insert attribute = "Header" Flush = "false" ignore = "false"> </tiles: Insert>
</TD>
</Tr>
<Tr>
<TD colspan = "1" bgcolor = "#000000" align = "right">
</TD>
</Tr>
& Lt; tr Height = "620" & gt;
<TD bgcolor = "#030d97" align = "Left" valign = "TOP" width = "17%">
<Tiles: insert attribute = "menu" Flush = "false" ignore = "false"> </tiles: Insert>
</TD>
<Td width = "83%" style = "vertical-align: top;">
& Lt; table width = "100%" & gt;
<Tr>
<Td style = "vertical-align: top; padding-top: 10px; padding-left: 10px;">
<Tiles: insert attribute = "body" flush = "false" ignore = "false"> </tiles: insert>
</Td>
</Tr>
</Table>
</Td>
</Tr>
<Tr>
<Td>
<Tiles: insert attribute = "footer" flush = "false" ignore = "false"> </tiles: insert>
</Td>
</Tr>
</Table>
</Body>
</Html>
Note: You must not set f: view in f: view. That is to say, you should not use the JSF tag in layout. jsp to generate some business logic. In fact, our main logic is also implemented in the body. In layout, we mainly define some text, images, and so on. Similarly, only html code is written in header. jsp, footer. jsp, and menu. jsp. Do not use the JSF tag.
5. Compile two jsp files like this:
Main_t.jsp:
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-tiles" prefix = "tiles" %>
<Tiles: insert definition = "report. main">
</Tiles: insert>
Summary_t.jsp:
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-tiles" prefix = "tiles" %>
<Tiles: insert definition = "report. summary">
</Tiles: insert>
This step is critical. If you understand this, you can easily combine tiles and jsf. Originally in Struts we configured the forward object can be directly a view in the tiles-defs.xml, but not in JSF. The navigation must be defined as a jsp file, so we only need to insert the view defined in tiles in this file. The downside is that every view we define in tiles-defs must have a jsp file like above, in addition, the navigation configured in JSF must be based on this file. Note: not only to-view-id, but also from-view-id must correspond to the above file, not the jsp file of the body (do not understand the configuration in step 1 ).
6. If you understand it, you can skip it .. The following are the remaining steps for the sake of completeness of the above example.
7. Compile an index. jsp
<Html>
<Head>
<Title>
Index
</Title>
</Head>
<Body bgcolor = "# ffffff">
<Jsp: forward page = "main_t.faces"> // corresponds to the file in step 1 above.
<Jsp: param name = "" value = ""/>
</Jsp: forward>
</Body>
</Html>
8. Compile the jsp file of the body defined in two tiles-defs.xml
Report_main.jsp:
<% @ Taglib uri = "http://java.sun.com/jsf/core" prefix = "f" %>
<% @ Taglib uri = "http://java.sun.com/jsf/html" prefix = "h" %>
<Html>
<Head>
<Title> main </title>
<Link rel = "stylesheet" type = "text/css" href = "sworx_IE.css"/>
</Head>
<Body bgcolor = "# ffffff">
<H1 align = "center" class = "header"> Report <F: view>
<H: form>
<P>
<H: commandButton action = "summary" value = "Summary Report" styleClass = "button" style = "width: 120"> <H: commandbutton type = "reset" value = "clear" styleclass = "button" style = "width: 60"> </P>
</H: Form>
</F: View>
</Body>
</Html>
Summary_report.jsp:
<% @ Taglib uri = "http://java.sun.com/jsf/core" prefix = "F" %>
<% @ Taglib uri = "http://java.sun.com/jsf/html" prefix = "H" %>
<HTML>
<Head>
<Title> main </title>
<LINK rel = "stylesheet" type = "text/CSS" href = "sworx_ie.css"/>
</Head>
<Body bgcolor = "# ffffff">
<H1 align = "center" class = "Header"> Summary Report </Body>
</Html>
9. Configure face-config.xml:
...........................
<Navigation-rule>
<From-view-id>/JSPROOT/reports/main_t.jsp (Note: The error report_main.jsp may be reported in some ides, such as JB2005) </from-view-id>
<Navigation-case>
<From-outcome> summary </from-outcome>
<To-view-id>/JSPROOT/reports/summary_t.jsp (not summary_report.jsp) </to-view-id>
</Navigation-case>
</Navigation-rule>
....................