我们有一个Tiles布局页面,包含页眉,菜单,正文和页脚.在此布局中,只要用户在菜单列表中执行某些操作,整个布局(包括页眉,菜单和页脚)就会刷新.我希望标题,菜单,页脚是静态的,只有正文部分应该更新.
有没有办法阻止刷新页眉,菜单和页脚,只更新菜单上的Body内容点击可以使用Tiles实现?
为此,您需要使用ajax调用.一般程序是:
1.-创建并定义一个作为基础的图块,由标题,正文和页脚组成,如下所示:
<div id='headerTile'>whatever goes here...</div> <!--this is baseHeader.jsp-->
<div id='bodyTile'>whatever goes here....</div> <!--this is baseBody.jsp-->
<div id='footerTile'>whatever goes here...</div> <!--this is baseFooter.jsp-->
Run Code Online (Sandbox Code Playgroud)
让我们将每个div视为您定义中的一个tile:
<definition name="layoutBase" path="/whateverPathItIs/layoutBase.jsp">
<put name="header" value="/whateverPathItIs/baseHeader.jsp"/>
<put name="body" value="/whateverPathItIs/baseBody.jsp" />
<put name="footer" value="/whateverPathItIs/baseFooter.jsp" />
</definition>
Run Code Online (Sandbox Code Playgroud)
2.-创建并定义将替换你的身体内容的所有不同的jsp,请记住这个jsp必须只包含body元素.假设您有2个其他正文内容已准备好显示在您的页面中,每个内容都将是一个磁贴:
<div id='bodyContent1'>whatever goes here again...</div> <!--this is content1.jsp-->
<div id='bodyContent2'>whatever goes here again 2</div> <!--this is content2.jsp-->
Run Code Online (Sandbox Code Playgroud)
3.-此时你有基本的jsp tile和另外两个包含body内容的jsp.现在我们需要一个struts动作,它将作为一个服务,根据ajax请求返回相应的主体.这是一个普通的动作,最后在mapping.findForward方法中,返回包含你的身体的jsp.您可以为每个正文内容创建一个操作,也可以为每个正文包含一个方法的单个DispatchAction.第二个选项更清晰,操作将如下定义:
<action path="/bodySwitcher"
type="BodySwitcherAction"
parameter="method"
scope="request"
>
<forward name="content1" path="/pathToJsp/content1.jsp"/>
<forward name="content2" path="/pathToJsp/content2.jsp"/>
</action>
Run Code Online (Sandbox Code Playgroud)
4.-要切换内容,使用javascript或jquery进行ajax调用并将返回的jsp加载到您的身体中.这是使用.load()作为ajax调用在jQuery中切换内容的方法示例:
function switchContent(whichContent){
$('#bodyTile').children().remove();
$('#bodyTile').load("/pathToYourApp/bodySwitcher.do?method="+whichContent);
}
Run Code Online (Sandbox Code Playgroud)
不要气馁答案是多长时间,我只是想干净利落地解释一下.这实际上很容易做到,问题是jquery/javascript与其他任何东西相关,唯一的细节是如何将struts动作用作服务.祝好运.
| 归档时间: |
|
| 查看次数: |
10846 次 |
| 最近记录: |