JSP Template I see

xiaoxiao2021-03-06  35

In the process of making procedures, we often encounter such a situation. In one project, it will find that there are many page frames that are basically the same, but the specific display is different, and we may Use the incline statement to raise the framework, then reference these frames in each page, but this will generate a problem. If my frame is changed, what should I do? For example, an index.jsp may contain Head.jsp and Foot.jsp. And other pages also include these two JSPs. But if I have a navigation code in Index.jsp, I am now quoting in each page, and I don't want to add to Head.jsp, do we have to add this navigation code in each page? ? I think I will do this. Because there is a better way to solve this problem. That is the template page.

The principle of the Template page is actually writing a servlet to implement the page's jump, then configure this servlet in web.xml, so that you want to use this type of JSP file to handle this servlet, so that they all go to Template page. Then in the template page, INCLUDE content pages are available.

Specific practice, just write a template.jsp as a template page, a TemplatedSpatch.java is a class as a jump. Then when you call, directly to this servlet, add a parameter, which means 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.getaTribute ("body")%>

<% @ include file = "foot.jsp"%>

TemplatedSpatch

=====================================

Public class templatedispatchextends httpservlet {

Public void doget (httpservletRequest request, httpservletResponse response) {

DOPOST (Request, Response);

}

Public void dopost (httpservletRequest request, httpservletResponse response) {

HttpSession session = request.getations (TRUE);

Session.SetaTRibute ("Body", "INDEX.JSP");

RequestDispatcher rd = session.getServletContext (). GetRequestDispatcher ("template.jsp"); rd.forward (request, response);

}

}

Web.xml

=====================================

dispatch

templatedispatchextends

dispatch

/ *

The code is called, and I have not been tested, just express me.

===================================================

If you feel simple, you can expand it. By configuration file configuration page, and taglib to define two tag INSERTs to add content pages, Value is used to display content directly in the template.

Now I think about the problem mentioned earlier, is it easier to solve? If there is any change in the page, you can change the template page. This will not make a mistake of leaking off which page.

转载请注明原文地址:https://www.9cbs.com/read-61911.html

New Post(0)