Performance tuning
The following are some tips and tricks to squeeze the most performance out of Struts 2.
|
Important OGNL update For Struts 2 versions before 2.3: the OGNL version 3.0.3 library is a drop-in replacement
for older OGNL jars,
and provides much better performance. See the following Jira issue for more information:
https://issues.apache.org/jira/browse/WW-3580
|
Turn off logging and devMode.
devMode allows reloading of configuration and validation related files, but because they happen on each request, this setting will totally kill your performance.
When using logging, make sure to turn off logging (esp. Freemarker generates a LOT of logging), and check if a level is enabled before printing it, or you will get the cost of the String parsing/concatination anyways.
Use the Java Templates
If you use the simple theme, and do not overwrite any of the FreeMarker templates, consider using the java templates, which provide a drop in replacement for most tags, and are a lot faster than the regular tags.
Do not use interceptors you do not need.
If you do not require a full stack of interceptors for an Action, then try using a different one (basicStack), or remove interceptors you do not need. Remove the I18nInterceptor interceptor if you don't need it, as it can cause a session to be created.
Use the correct HTTP headers (Cache-Control & Expires).
When returning HTML views, make sure to add the correct headers so browsers know how to cache them.
Copy the static content from the Struts 2 jar when using the Ajax theme (Dojo) or the Calendar tag.
Struts 2 uses some external javascript libraries and cascading stylesheets for certain themes and tags. These by default are located inside the Struts 2 jar, and a special filter returns them when requesting a special path (/struts). Although Struts 2 can handle these requests, an application/servlet container is not optimized for these kind of requests. Consider moving these .js and .css files to a seperated server (Lighttpd, Apache HTTPD, ..).
Create a freemarker.properties file in your WEB-INF/classes directory.
Create the freemarker.properties file and add the following setting (or whatever value you deem fitting):
This value determines how often Freemarker checks if it needs to reloads the templates from disk. The default value is 500 ms. Since there is no reason to check if a template needs reloading, it is best to set this to a very large value. Note that this value is in seconds and freemarker will convert this value to milliseconds.
See also: Freemarker configuration properties
Enable Freemarker template caching
As of Struts 2.0.10, setting the property struts.freemarker.templatesCache to true will enable the Struts internal caching of Freemarker templates. This property is set to false by default.
In Struts versions prior to 2.0.10, you had to copy the /template directory from the Struts 2 jar in your WEB_APP root to utilize Freemarker's built in chaching mechanism in order to achieve similar results.
The built in Freemarker caching mechanism fails to properly cache templates when they are retrieved from the classpath. Copying them to the WEB_APP root allows Freemarker to cache them correctly. Freemarker looks at the last modified time of the template to determine if it needs to reload the templates. Resources retrieved from the classpath have no last modified time, so Freemarker will reload them on every request.
When overriding a theme, copy all necessary templates to the theme directory.
There's a performance cost when a template cannot be found in the current directory. The reason for this is that Struts 2 must check for a template in the current theme first before falling back to the parent theme. In the future, this penalty could be eliminated by implementing a missing template cache in Struts 2.
Do not create sessions unless you need them.
Struts 2 does not create sessions unless asked to (for example, by having the createSession interceptor in your interceptor stack). Note that when you use SiteMesh however, a session will always be created (Seehttp://forums.opensymphony.com/thread.jspa?messageID=5688 for details). The I18nInterceptor interceptor can create sessions, so make sure you remove it, if you don't need it.
When using Freemarker, try to use the Freemarker equivalent rather than using the JSP tags.
Freemarker has support for iterating lists, displaying properties, including other templates, macro's, and so on. There is a small performance cost when using the S2 tags instead of the Freemarker equivalent (eg. <s:property value="foo"/> should be replaced by ${foo}).
分享到:
相关推荐
Struts框架在这方面表现突出,它提供了多个扩展点,允许开发者根据项目需求灵活调整框架行为,比如通过自定义拦截器、过滤器或事件处理器等方式,增强应用的功能和性能。 #### 结语 综上所述,Struts框架的多模块...
### Struts 1.0 学习文档:初学者入门详解 #### 一、Struts 概述 Struts 是一款基于 Java Servlet 和 XML 的开源框架,专为简化 Web 应用开发而设计。随着 Web 应用复杂度的增加,代码重用性、移植性和模块化成为...
总的来说,"Struts-2.2.3帮助文档"对于Java Web开发者而言是一份不可或缺的资源,它可以帮助开发者深入理解Struts 2框架,提高开发效率,确保应用的安全性和稳定性。通过系统地学习和实践,开发者可以更好地利用...
### Struts 技术文档知识点总结 #### 一、Struts 框架简介与背景 **1.1 关于本书** - **作者**: Ted Husted(原著),Eric Liu(翻译) - **目的**: 旨在为Java技术框架的初学者提供深入浅出的学习资料。 - **...
- 获取Struts2的最新版本2.3.34,通常可以从Apache Struts的官方网站下载。 - 下载所需的依赖库,包括struts2-core、struts2-convention-plugin、struts2-dojo-plugin等相关插件,这些文件可能包含在提供的压缩包...
10. **文档阅读**:官方文档是升级过程中的重要参考,包含详细的升级指南和注意事项,例如Apache Struts官方文档的“Migrating from 2.3 to 2.5”章节。 通过以上知识点的学习和实践,开发者可以成功地将Struts...
### Struts1.3项目开发知识点详述 #### 一、环境搭建与配置 ##### 1.1 软件的下载与安装 ...项目开发完成后,进行功能测试、性能测试以及用户体验测试,确保项目稳定可靠,并根据测试结果进行必要的优化调整。
Struts2.2到Struts2.3的升级是一个重要的更新过程,涉及到多个方面的改动和优化,包括性能提升、安全增强以及新特性的引入。在升级过程中,我们需要关注以下几个关键知识点: 1. **依赖包的更新**:Struts2.3.32...
5. **文档更新**:官方文档通常会随着新版本的发布而更新,以便开发者更好地理解和使用新功能。 **开发实践** 在使用Struts 2.3.28.1时,开发者需要注意以下几点: 1. **迁移指南**:升级到新版本前,需阅读官方...
在使用Struts2.5.13时,开发者需要注意遵循最佳实践,例如,配置合适的拦截器来防止XSS、CSRF等常见攻击,以及定期检查并安装官方发布的安全补丁,以确保应用的安全性。 总之,Struts2.5.13是一个重要的更新,它在...
1. **性能优化**:官方可能已经对框架的内部结构进行了调整,以提高处理请求的速度和整体应用性能。这包括减少内存占用、优化执行路径以及提升响应时间。 2. **安全强化**:Struts 2.5.10.1可能包含了针对已知安全...
确保按照官方文档中的指导更新相关配置,以利用新版本的所有功能。 5. **插件兼容性**:检查并更新依赖的Struts2插件,确保它们与2.3.32版本兼容。如果某个插件尚未更新,可能需要寻找替代方案或者等待插件作者发布...
8. **文档更新**: 随着新特性的增加,官方文档也得到了更新和完善,帮助开发者更好地理解和使用Struts 1.1。 在实际项目中,开发人员会将struts1.1的jar包与其他依赖库(如log4j、commons-logging等)一起添加到...
6. **文档阅读**:仔细阅读官方升级指南和变更日志,了解新版本带来的改动和建议的升级步骤。 在升级过程中,可能会遇到的常见问题包括编译错误、运行时异常、页面渲染错误等。解决这些问题通常涉及调试、查阅文档...
1. **依赖冲突**:在使用Struts2的过程中可能会遇到与其他框架的依赖冲突问题,解决方法通常是调整依赖版本或者排除不必要的依赖。 2. **配置错误**:配置文件的错误是常见的问题之一,可以通过仔细检查配置文件以及...
这在更换日志实现或在不同环境中部署时非常方便,因为只需要调整配置,而无需修改代码。 `javadoc`是Java文档生成工具,它可以将源代码中的注释转换成格式化的HTML文档,便于开发者理解和使用API。在Eclipse这样的...
10. **性能优化**:通过源码分析,开发者可以了解Struts2的性能瓶颈,并学习如何通过调整配置或优化代码来提升性能。 通过深入阅读"struts2源代码分析(个人觉得非常经典).doc"、"struts2源代码分析.docx"和解压后的...
9. **文档查阅**:官方文档是升级过程中的重要参考,它包含了详细的升级步骤、注意事项和可能遇到的问题。务必仔细阅读并遵循指导。 10. **回滚计划**:在升级前,最好备份当前的系统和配置,以防升级后出现未预见...
9. **文档阅读**:官方发布的Struts 2.3.34版本文档是了解新特性和改动的重要资源,开发者应该详细阅读以获取全面信息。 10. **社区支持**:Struts 2拥有活跃的社区,开发者可以在论坛、邮件列表等平台上寻求帮助,...
在Struts2的升级过程中,还应该关注官方发布的文档和公告,了解新版本引入的改进和变更,以便更好地利用新功能并避免潜在问题。同时,保持框架的持续更新是防止安全威胁的有效手段,定期检查并应用最新补丁是良好...