My view of JSP template

Source: Internet
Author: User
Tags include
JS we are in the process of doing the procedure, it is often the case that in a project you will find that there are many frames of the page that are basically the same, but the contents are different, and, as a general rule, we might use the include statement to present the framework content and then reference the frames in each page. But this creates a problem, if my frame changes, what should I do? For instance, a index.jsp may contain head.jsp and foot.jsp. Other pages also contain these two jsps. But if I have a navigation bar code in index.jsp, and I want to refer to each page instead of adding it to the head.jsp, do we add the navigation bar code to each page? I don't think I would have done that. Because there is a better way to solve this problem. That is to adopt the template page.

Template The principle of the page, in fact, is to write a servlet to achieve the page jump, and then configure the web.xml inside the servlet, so that you want to use the template of this class of JSP files, through the servlet to deal with, so that they all go to the template page can be. Then include the content pages of these pages in the template page.

Concrete practice, as long as write a template.jsp as a template page, a Templatedispatch.java as a jump class can be. Then, at the time of the call, point directly to the servlet, and add a parameter that explains which content page to use. Write a simple example as follows:
template.jsp
=====================================
<% page contenttype= "text/html; charset=gb2312 "%>
<%@ include file= "head.jsp"%>
<%= (String) request.getattribute ("Body")%>
<%@ include file= "foot.jsp"%>

Templatedispatch
=====================================
public class Templatedispatchextends httpservlet{
public void doget (HttpServletRequest request,httpservletresponse response) {
DoPost (Request,response);
}
public void DoPost (HttpServletRequest request,httpservletresponse response) {
HttpSession session = Request.getsession (true);
Session.setattribute ("Body", "index.jsp");
RequestDispatcher rd = Session.getservletcontext (). Getrequestdispatcher ("template.jsp");
Rd.forward (Request,response);
}
}

Xml
=====================================
<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>templatedispatchextends </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

The code is improvised, has not been tested, just to express the meaning.
===========================================

If you think it's easy, you can expand it. Configure a content page with a profile and taglib to define two tag inserts to add content pages and value to display content directly in the template.

Is it easier to solve the problem that is mentioned earlier? If there is any change in the page, just modify the template page. This will not make a mistake about which page is missing.


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.