Include another JSP file

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

I am currently trying to learn JSP. My question is, at present I used to include the header and footer of the page using:

 

and

 

But now, I have separated the page content also. So, if user clicks on a page, say products, it has to load the JSP file which is situated in: includes/pages/products.jsp So, the link to the user is like: Products.

So, I have to get the p value and display the page based on it.

Following is what I have done so far.

         

So, how do I place the value of variable "p" in position of "page_name" ?

Or, is there any other method that I could use ?

In PHP, we could use the include() or include_once(). I am bit stuck in this JSP. :(

回答1:

What you're doing is a static include. A static include is resolved at compile time, and may thus not use a parameter value, which is only known at execution time.

What you need is a dynamic include:

Note that you should use the JSP EL rather than scriptlets. It also seems that you're implementing a central controller with index.jsp. You should use a servlet to do that instead, and dispatch to the appropriate JSP from this servlet. Or better, use an existing MVC framework like Stripes or Spring MVC.



回答2:

You can use Include Directives

      "%>   

or JSP Include Action

      "/>   

the different is include directive includes a file during the translation phase. while JSP Include Action includes a file at the time the page is requested

I recommend Spring MVC Framework as your controller to manipulate things. use url pattern instead of parameter.

example:

www.yourwebsite.com/products 

instead of

www.yourwebsite.com/?p=products 

Watch this video Spring MVC Framework



回答3:

You can use parameters like that

and

in about.jsp you can take the paramter

 


回答4:

At page translation time, the content of the file given in the include directive is ‘pasted’ as it is, in the place where the JSP include directive is used. Then the source JSP page is converted into a java servlet class. The included file can be a static resource or a JSP page. Generally, JSP include directive is used to include header banners and footers.

Syntax for include a jsp file:

 

Example

 


回答5:

1.Products when user clicks on Products link,you can directly call products.jsp.

I mean u can maintain name of the JSP file same as parameter Value.

      " %>   

or

2.you can maintain external resource file with key,value pairs. like below

products : products.jsp

customer : customers.jsp

you can programatically retrieve the name of JSP file from properies file.

this way you can easily change the name of JSP file



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!