Methods for referencing other pages in jsp pages, and methods for referencing jsp pages

Source: Internet
Author: User

Methods for referencing other pages in jsp pages, and methods for referencing jsp pages

First look at this title .... everyone's feelings must be good 2 ..... the Blogger must talk about jsp dynamic reference (jsp: include) and static reference (@ include. This article introduces the differences between the two .. .. The blogger even introduced these old antiques .. Decisive alt + F4...

If you do think so... Please do not rush to close the page... Today, I want to share my understanding with you, which is a supplement to the things on bad streets... (The text below will be too long)



First, explain why we will discuss this topic.

When I write my own blog Program (blogv2.labofjet.com), the directory in the program needs to read the data in the database, which is to be referenced by multiple pages. What should we do at this time.

At that time, I used static reference. On the jsp page, I used <% @ include file = "menu. jsp "%> reference the jsp page segment of this directory (a jsp is made for the Directory separately ), the disadvantage is that when different controllers jump to different pages, as long as the page contains the HTML segment of the directory, the Controller needs to call the dao method of the Directory and then set the value for the request, in this way, the directory page can be displayed normally.

Later I thought about it. I think it is best to write a servlet and access the servlet url to get the HTML segment of the directory. Then, on different pages, loading the url is equivalent to loading the HTML segment of the directory. The advantage of this is that different controllers no longer need to call the dao of the Directory, as long as there is a dedicated directory controller to process the dao of the Directory, set the request, and then return the page of the directory. Then I thought of jsp: include ....

Then I made some bad things on Baidu Street. Most articles share the same meaning. jsp: include can reference jsp and dynamically compile it .. Blablabla... But none of them indicate that jsp: include can reference servlet? We will not place the logic in jsp now. Generally, the logic is put in servlet and then jump to jsp. After testing, I found that it is okay. However, there are some areas where they are relatively pitfall ..... Note that...

Jsp: include is the content introduced in most articles on the Internet when jsp is referenced completely OK.

Jsp: include When referencing servlet, this servlet needs to be configured in web. xml, otherwise eclipse will give a red line.

I don't know if there is a new problem after reading the summary above: Currently we use some frameworks when writing programs, such as SpringMVC, we are not on the web. xml is configured separately for each Servlet. Instead, SpringMVC uses its own configuration file to configure these controllers (servlets. Can jsp: include be used?

The answer is yes .... But it is very pitfall... In eclipse, there will still be a red line. eclipse cannot find the servlet corresponding to this url, but it is actually accessible.

There is also a small problem. It may be that I have not set it myself. Every time I use jsp: include, java will be reported in the console. io. IOException: Stream closed exception. I think it may be that when other servlets are referenced, other servlets automatically call the out close method, as a result, the content of the page containing other pages cannot be output normally .... I am not continuing to study this issue .... I think there must be a solution, but it is not the focus of my article.

Now there are two minor problems: an exception, an eclipse red line error, and so on... Although it can also be used, it is quite unpleasant ..

So find an alternative .. That is, the c: import of jstl.

This label is actually super simple... For example, if I set the url corresponding to the HTML segment of the directory to/menu

<c:import var="data" url="/menu" />${data}

OK. The directory is included in other pages .. Is it super simple ?!


Instantly feels the world is beautiful .....

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.