前几天开始学习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>  你所在位置:<@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>
分享到:
相关推荐
- `struts2 freemarker-plugin`: 支持使用FreeMarker模板引擎来渲染视图,FreeMarker是一种强大的、非侵入式的模板语言。 - `struts2-sitemesh-plugin`: 集成了Sitemesh,用于页面布局和装饰,可以统一网站的外观...
struts2-dojo-plugin-2.3.30.jar, struts2-dwr-plugin-2.3.30.jar, struts2-embeddedjsp-plugin-2.3.30.jar, struts2-gxp-plugin-2.3.30.jar, struts2-jasperreports-plugin-2.3.30.jar, struts2-javatemplates-...
6. **插件扩展**: Struts2拥有丰富的插件体系,如Struts2 Dojo Plugin提供富客户端支持,Struts2 JSON Plugin支持JSON响应,Struts2 Sitemesh Plugin整合页面布局等,极大地增强了框架的功能。 7. **最佳实践**: 在...
- 视图处理相关的库(struts2-dojo-plugin.jar, struts2-sitemesh-plugin.jar等) - 数据源和持久化支持的库(struts2-jdbc-plugin.jar, struts2-dbcp-plugin.jar等) - 其他辅助库(如ognl.jar, freemarker.jar等)...
Struts2可以与多种模板技术结合,如Freemarker或Sitemesh,以实现更灵活的视图渲染。源代码可以帮助你理解这些模板引擎如何与Struts2协作。 10. **注解驱动的开发**: 除了XML配置,Struts2还支持使用Java注解来...
Apache Struts Copyright 2000-2011 The Apache Software Foundation This product includes software developed by The Apache Software Foundation (http://www.apache.org/). Dojo (http://dojotoolkit.org/). ...
Apache Struts Copyright 2000-2011 The Apache Software Foundation This product includes software developed by The Apache Software Foundation (http://www.apache.org/). Dojo (http://dojotoolkit.org/). ...
- **第14章至第19章整合其他框架**:分别讲述了Struts2与其他流行框架(如Spring、JSF、SiteMesh、JasperReports、JFreeChart、Hibernate)的整合方法,扩展了Struts2的功能范围。 - **第20章至第21章案例分析**:...
Apache Struts Copyright 2000-2011 The Apache Software Foundation This product includes software developed by The Apache Software Foundation (http://www.apache.org/). Dojo (http://dojotoolkit.org/). ...
Apache Struts Copyright 2000-2007 The Apache Software Foundation This product includes software developed by The Apache Software Foundation (http://www.apache.org/). Dojo (http://dojotoolkit.org/). ...
struts2-dojo-plugin-2.3.15.3.jar struts2-dwr-plugin-2.3.15.3.jar struts2-embeddedjsp-plugin-2.3.15.3.jar struts2-gxp-plugin-2.3.15.3.jar struts2-jasperreports-plugin-2.3.15.3.jar struts2-javatemplates...