Performance Tuning
Performance tuning
The following are some tips and tricks to squeeze the most performance out of Struts 2.
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):
template_update_delay=60000
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 (See http://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}).
分享到:
相关推荐
本文将根据给定文件的信息,深入探讨Struts2项目性能优化的三个关键步骤,旨在帮助开发者提升系统响应速度和用户体验。 ### 一、关闭不必要的日志记录与开发模式 Struts2在默认情况下开启的日志记录和开发模式虽然...
17. **性能优化**:提供Struts 2性能调优的技巧,包括配置优化、缓存策略、减少HTTP请求等。 18. **测试与调试**:讲解单元测试、集成测试在Struts 2项目中的应用,以及调试工具和技巧。 19. **最佳实践**:总结...
ognl2.7相对于2.6在性能上有了“显著”的提升.所以该jar包能对struts2的调优有很大帮助,尤其是ValueStack中的操作比较多的情况下。
11. **最佳实践与性能优化**:提供开发高质量Struts2应用的建议,包括代码组织、性能调优和安全注意事项。 12. **实战案例**:通过实际项目示例,展示如何从零开始构建一个完整的Struts2应用,涵盖需求分析、设计、...
8. **安全和性能优化**:讲解Struts 2的安全问题,如XSS、CSRF防范,以及如何进行性能调优,提升系统响应速度。 通过学习《深入浅出Struts 2》,开发者不仅可以掌握Struts 2的核心概念和技术,还能了解到如何在实际...
因此,开发者需要关注官方发布的安全补丁,并进行性能调优,如合理配置拦截器、避免内存泄漏等。 综上所述,"Struts2入门教材"将涵盖这些基础概念和实践技巧,帮助初学者理解并掌握Struts2框架的使用,从而能高效地...
此外,书中还会涵盖测试和性能优化策略,包括单元测试Action、集成测试和性能调优技巧。良好的测试习惯和性能优化对于任何Web应用来说都至关重要。 总而言之,《Struts 2 in Action》是Java开发者学习和精通Struts ...
7. **最佳实践**:学习SSH集成的最佳实践,包括配置优化、性能调优、异常处理等。 课件中可能还会包含相关的练习题和实例代码,帮助你更好地理解和掌握这些知识。如果你对SSH框架感兴趣,或者正在进行Java Web开发...
这些工具可以帮助开发者提高效率,比如使用Maven或Gradle构建Struts2项目,使用Eclipse或IntelliJ IDEA的插件进行代码生成和调试,或者使用JProfiler等工具进行性能调优。 至于压缩包中的“struts.gif”,这可能是...
8. **最佳实践与性能优化**:分享了实际开发过程中的最佳实践,以及如何对Struts 2应用进行性能调优,提高系统效率。 通过阅读这本书,读者不仅可以掌握Struts 2的基本用法,还能了解到如何在实际项目中灵活运用这...
9. **最佳实践和性能优化**:分享开发Struts 2应用时的最佳实践,如何编写高效的代码,以及如何进行性能调优。 10. **实战案例**:书中可能会包含多个实际项目案例,帮助读者将理论知识应用于实际开发场景,提升...
- **性能调优**:合理使用拦截器、减少不必要的数据库查询,优化模板渲染等。 6. **实战项目**: - **构建一个简单的Struts2应用**:从零开始,创建第一个Action,实现基本的CRUD操作。 - **整合其他技术**:如...
例如,对于XSS和CSRF等Web安全问题的防护,以及性能调优的更新。 总结起来,Struts2.1.6-Lib是一个包含了Struts2核心框架和必要依赖的集合,为开发Java Web应用提供了全面的支持。通过理解和掌握这些库的用途,...
10. **最佳实践与性能优化**:本章可能涵盖Struts 2应用的性能调优技巧,如减少不必要的拦截器、缓存策略、避免内存泄漏等。 以上内容是基于标题和标签的推测,实际章节内容可能包含但不限于这些知识点。对于学习...
在实际开发中,还需要考虑安全性、性能调优、代码规范等多个方面,确保应用的稳定性和可扩展性。通过熟练掌握Struts2和Hibernate,开发者可以有效地提高开发效率,减少出错概率,从而为“当当”这样的电商系统提供...
10. 性能调优和安全:这部分内容包括了如何对Struts2应用程序进行性能调优以及安全性的相关措施,如Actions、Wildcard Mappings、Beans、Constants、Exceptions、Interceptors、Namespaces、Packages和Results等。...
7. **测试与优化**:进行单元测试和集成测试,确保各个组件协同工作,根据需求进行性能调优。 这个框架模板适用于大型企业级应用,可以帮助开发者快速搭建项目结构,降低开发难度,提高代码的可维护性和可扩展性。...
- **性能调优**: 结合具体的应用场景,可以通过调整配置参数等方式进一步优化性能。 - **学习资源**: Apache官网提供了丰富的文档和教程资源,初学者可以从这些资源入手快速上手。 综上所述,Struts 2.3.32版本作为...
- **性能调优**:根据实际应用需求,调整缓存策略、连接池配置等,优化系统性能。 - **安全考虑**:整合过程中需关注SQL注入、XSS攻击等问题,使用预编译SQL、过滤输入等手段加强安全性。 通过上述整合,开发者...