The self-assembly method is roughly as follows: divide the content of the entire page into different view components. The responsibility of displaying the page itself is to include these view components, and construct the complete HTML response in the correct way. The display page is responsible for page assembly. When the master page assembly method is used, the displayed page becomes another content segment contained in the entire response of the master page. As shown in figure 3-6, the master page is responsible for assembling and defines the structure and layout of the entire page.
|
Figure 3-6 Comparison between self-assembly and master page assembly |
In terms of concept, this is very simple, but how do I use the same master page for different display pages on the site? How can I know which display page should I introduce to the master page?
The trick to use this technology is to add a <JSP: Include> action instruction that can be executed during page running on the master page to introduce the correct content page. <JSP: Include> action commands have two basic forms. The first form uses the Static Page name:
- <JSP: Include Page="/Home. jsp"/>
Second, use the runtime expression to define the Page name:
- <JSP: Include Page="<% = Variablename %>"/>
This form of using the runtime expression as the page name provides a simple and feasible way to share the same master page among multiple display pages. In the simplest usage, the Master. jsp page first looks for the specific request parameter page, and then the action command uses the page as the parameter to place the correct page in the display area of the complete template of the master page:
-
- ...
-
- <% StringPagename=Request. Getparameter ("page"); %>
-
- ...
- <Body>
-
- <Table>
-
- ...
-
- <% Try {%>
-
- <JSP: Include Page="<% = Pagename %>"/>
-
- <%}
-
- Catch (ioexception e ){
- %>
-
- <JSP: Include Page="Blank. jsp"/>
-
- <%} %>
-
- ...
-
- </Table>
-
- </Body>
Next, use a URL consisting of the master page name and a query string with the display Page name to access each display page. For example, http: // SERVERNAME: Port/master. jsp? Page = home. jsp will call the master. jsp master page and point out that the name of the page to be introduced is home. jsp. You can use the following syntax to specify URLs for hyperlinks on a page:
- <A Href="Master. jsp? Page = viewproperty. jsp">...</A>
Now we can change the appearance of the entire site by modifying an independent master. jsp page without touching any display page. These changes can include the overall layout of the page structure, the addition or deletion of the included view components, and other expected changes.