struts2+sitemesh+freemarker+Dojo

前几天开始学习struts2,打算用struts2+sitemesh+freemarker+ Spring+ ibatis做一个简单系统。
开发过程中突然想加入ajax技术,看到struts2已经包含了Dojo。于是便开始尝试加入ajax。根据《struts2权威指南》和网上的参考,终于弄好了siteMesh+freemarker。但是当在某个页面加入dojo的时候,发现局部刷新的内容包含了整个template的内容,即ajax 结果页面仍旧被siteMesh+freemarker所截取。后来仔细与struts2 showcase比较,最终发现自己少了一个配置文件sitemesh.xml,现在终于可以了。
特记录如下:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
</filter>

<filter>
<filter-name>struts-execute</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
</filter>

<filter>
<filter-name>sitemesh</filter-name>
<filter-class>org.apache.struts2.sitemesh.FreeMarkerPageFilter</filter-class>
</filter>

<!--  -->
   
<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>struts-execute</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--
<listener>
    <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>
</listener>

使FreeMarker模块能够使用strut2标签,使用方式:<#assign
s=JspTaglibs["/WEB-INF/struts-tags.tld"] />

-->
<servlet>
<servlet-name>sitemesh-freemarker</servlet-name>
<servlet-class>com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet</servlet-class>
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/</param-value>
</init-param>
         <init-param> 
             <param-name>default_encoding</param-name> 
             <param-value>GBK</param-value> 
         </init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>sitemesh-freemarker</servlet-name>
<url-pattern>*.action</url-pattern>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>


<servlet>
<servlet-name>JspSupportservlet</servlet-name>
<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>



<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

-----------freemarker模板
<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"]/>
<#assign sj=JspTaglibs["/WEB-INF/struts-jquery-tags.tld"]/>
<html> 
     <head> 
         <title>${title}</title>
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<script type="text/javascript" src="${base}/inc/js/swfobject.js"></script>
<script type="text/javascript" src="${base}/inc/js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="${base}/inc/js/ie7/IE7.js"></script>
        <link href="${base}/inc/css/template.css" rel="stylesheet" type="text/css">
        <link href="${base}/inc/css/public.css" rel="stylesheet" type="text/css">
        <link href="${base}/inc/css/nav-horizontal.css" rel="stylesheet" type="text/css">
        <link rel="stylesheet" type="text/css" href="${base}/inc/css/overlay-minimal.css"/>
        <@sj.head locale="cn" jqueryui="true"/>
         ${head}
     </head> 
     <body>
<div id="container" class="container">
<div id="header" class="header">
    <#include "/WEB-INF/decorators/header.ftl">
</div>

<div id="menuList" class="menuList">
    <#include "/WEB-INF/decorators/menu.ftl">
</div>

<div id="mainContent" class="mainContent">
<div id="locationTip">
<table class="locationTip"><tr><td>&nbsp&nbsp你所在位置:<@s.property value="menuItem"/></td></tr></table>
</div>
${body}
</div>

<div id="footer">
<#include "/WEB-INF/decorators/footer.ftl"> 
</div>
</div>
<script> jQuery("#loader_container").hide();</script>    
     </body> 
</html>

其中header.ftl、menu.ftl、footer.ftl都很简单

同时需要在decorator.xml中排除不需要装饰的页面,即ajax关联的页面或action

<?xml version="1.0" encoding="UTF-8"?>
<!-- defaultdir指定装饰器文件所在的路径 -->
<decorators defaultdir="/WEB-INF/decorators">
     <excludes>
     <pattern>/login.jsp</pattern>
     <pattern>/welcome.jsp</pattern>
     <pattern>/pages/test/*</pattern>
     <pattern>/upload/*</pattern>
     <pattern>/pages/ajax/AjaxResult*</pattern>
     <pattern>/pages/test/*</pattern>
     <pattern>/AjaxTest.action</pattern>
     </excludes>

<!-- 指定main装饰器,该装饰器使用main.jsp页面 -->
    <decorator name="main" page="template.ftl">
<!-- 使用main装饰器装饰所有的JSP页面 -->
        <pattern>/*</pattern>
    </decorator>
</decorators>

你可能感兴趣的:(jquery,freemarker,Ajax,struts,dojo)