JSP master page assembly

Source: Internet
Author: User

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:

    1. <JSP: Include Page="/Home. jsp"/> 

 

Second, use the runtime expression to define the Page name:

    1. <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:

  1. ...
  2. <% StringPagename=Request. Getparameter ("page"); %> 
  3. ...
  4. <Body> 
  5. <Table> 
  6. ...
  7. <% Try {%> 
  8. <JSP: Include Page="<% = Pagename %>"/> 
  9. <%}
  10. Catch (ioexception e ){
  11. %> 
  12. <JSP: Include Page="Blank. jsp"/> 
  13. <%} %> 
  14. ...
  15. </Table> 
  16. </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:

    1. <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.

Related Article

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.