JSP include,之前和之后导入代码 [英] JSP include, import code after and before

查看:182
本文介绍了JSP include,之前和之后导入代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网络应用程序的许多页面中拥有以下结构。我想在页码之前和之后创建一个带有固定代码的模板页面,然后将我的代码放到每个不同的页面中。问题是我应该在不同的文件中删除 div s。

  <! -  fixed jsp code 
< header>
<进口> ...
< div id =some-fixes-divs-inside>
- >

页面代码

<! - 固定jsp代码
< / div>
< other>
< footer>
- >

我知道的唯一方法就是这样,但我认为切割 div s:

 < jsp:include page =fixed-jsp -code-head.jsp/> 
我的页面代码
< jsp:include page =fixed-jsp-code-footer.jsp/>




Q1:jsp或其他框架中是否存在指令或其他内容要做到这一点?



Q2:如果第一个问题是否定的,我的解决方案是一个不好的做法吗?


编辑:我想知道是否有像< import> mycode< / import> 这样的指令在模板中包含所有的代码页脚和头文件,并且不要在头文件中打开相同的 div ,并在页脚中关闭它,并将我的代码放入模板中。 / p>

解决方案

您也可以使用Apache Tiles。它是一个模板组合框架。像

首先定义一个模板:

 < definition name =myapp.homepagetemplate =/ layouts / classic.jsp> 
< put-attribute name =headervalue =/ tiles / banner.jsp/>
< put-attribute name =menuvalue =/ tiles / common_menu.jsp/>
< put-attribute name =bodyvalue =/ tiles / home_body.jsp/>
< put-attribute name =footervalue =/ tiles / credits.jsp/>
< / definition>

然后在代码中使用它:

 <%@ taglib uri =http://tiles.apache.org/tags-tilesprefix =tiles%> 
< table>
< tr>
< td colspan =2>
< tiles:insertAttribute name =header/>
< / td>
< / tr>
< tr>
< td>
< tiles:insertAttribute name =menu/>
< / td>
< td>
< tiles:insertAttribute name =body/>
< / td>
< / tr>
< tr>
< td colspan =2>
< tiles:insertAttribute name =footer/>
< / td>
< / tr>
< / table>


I have the following structure in many pages in my web app. I would like to create a template page with the fixed code before and after the page code and then put my code for each different page. The problem is that I should cut divs in different files.

<!-- fixed jsp code 
  <header>
  <imports> ...
  <div id="some-fixes-divs-inside">
-->

   page code

<!-- fixed jsp code
  </div>
  <other>
  <footer>
 -->

The only way I know would be something like this but I think is not a good practice to cut divs:

<jsp:include page="fixed-jsp-code-head.jsp" />
    my page code
<jsp:include page="fixed-jsp-code-footer.jsp" />

Q1: Are there any directive or something in jsp or any other framework to do that?

Q2: If first Q is no, is my solution a bad practice?

Edit: I would like to know if are there any directive like <import>mycode</import> to have in the template all the code footer and header and don't open the same div in the header and close it in the footer, and put my code inside the template.

解决方案

You can also use Apache Tiles. It is a template composition framework. Like this.

First of all, define a template:

<definition name="myapp.homepage" template="/layouts/classic.jsp">
  <put-attribute name="header" value="/tiles/banner.jsp" />
  <put-attribute name="menu" value="/tiles/common_menu.jsp" />
  <put-attribute name="body" value="/tiles/home_body.jsp" />
  <put-attribute name="footer" value="/tiles/credits.jsp" />
</definition>

And after that use it in your code:

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<table>
  <tr>
    <td colspan="2">
      <tiles:insertAttribute name="header" />
    </td>
  </tr>
  <tr>
    <td>
      <tiles:insertAttribute name="menu" />
    </td>
    <td>
      <tiles:insertAttribute name="body" />
    </td>
  </tr>
  <tr>
    <td colspan="2">
      <tiles:insertAttribute name="footer" />
    </td>
  </tr>
</table>

这篇关于JSP include,之前和之后导入代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆