Struts性能优化

来源:互联网 发布:spark sql python 编辑:程序博客网 时间:2024/05/19 11:50

       在学习编程的过程中,我觉得不止要获得课本的知识,更多的是通过学习技术知识提高解决问题的能力,这样我们才能走在最前方,更多Java学习,请搜索疯狂Java;

  The following are some tips and tricks tosqueeze the most performance out of Struts 2.

  Important OGNL update

  For Struts 2 versions before 2.3: the OGNLversion 3.0.3 library is a drop-in replacement for older OGNL jars, andprovides much better performance. See the following Jira issue for moreinformation://issues.apache.org/jira/browse/WW-3580

  Turn off logging and devMode. 关掉日志与开发模式

  devMode allows reloading of configurationand validation related files, but because they happen on each request, thissetting will totally kill your performance.

  When using logging, make sure to turn offlogging (esp. Freemarker generates a LOT of logging), and check if a level isenabled before printing it, or you will get the cost of the Stringparsing/concatination anyways.

  Use the Java Templates 使用Java模板

  If you use the simple theme, and do notoverwrite any of the FreeMarker templates, consider using the java templates,which provide a drop in replacement for most tags, and are a lot faster thanthe regular tags.

  Do not use interceptors you do not need. 不要使用拦截器,除非你真的需要

  If you do not require a full stack ofinterceptors for an Action, then try using a different one (basicStack), orremove interceptors you do not need. Remove the I18nInterceptor interceptor ifyou don't need it, as it can cause a session to be created.

  Use the correct HTTP headers (Cache-Control& Expires). 返回使用正确的HTML头部

  When returning HTML views, make sure to addthe correct headers so browsers know how to cache them.

  Copy the static content from the Struts 2jar when using the Ajax theme (Dojo) or the Calendar tag.拷贝struts jar包静态文件出来

  Struts 2 uses some external javascriptlibraries and cascading stylesheets for certain themes and tags. These bydefault are located inside the Struts 2 jar, and a special filter returns themwhen requesting a special path (/struts). Although Struts 2 can handle theserequests, an application/servlet container is not optimized for these kind ofrequests. Consider moving these .js and .css files to a seperated server(Lighttpd, Apache HTTPD, ..).

  Create a freemarker.properties file in yourWEB-INF/classes directory. 设置freemarker.properties模板更新延时时间

  Create the freemarker.properties file andadd the following setting (or whatever value you deem fitting):

  template_update_delay=60000

  This value determines how often Freemarkerchecks if it needs to reloads the templates from disk. The default value is 500ms. Since there is no reason to check if a template needs reloading, it is bestto set this to a very large value. Note that this value is in seconds andfreemarker will convert this value to milliseconds.

  See also: Freemarker configurationproperties

  Enable Freemarker template caching 关闭Freemarker模板缓存

  As of Struts 2.0.10, setting the propertystruts.freemarker.templatesCache to true will enable the Struts internalcaching of Freemarker templates. This property is set to false by default.

  In Struts versions prior to 2.0.10, you hadto copy the /template directory from the Struts 2 jar in your WEB_APP root toutilize Freemarker's built in chaching mechanism in order to achieve similarresults.

  The built in Freemarker caching mechanismfails 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 itneeds to reload the templates. Resources retrieved from the classpath have nolast modified time, so Freemarker will reload them on every request.

  When overriding a theme, copy all necessarytemplates to the theme directory. 覆盖主题时,拷贝所以模板(含父模板)

  There's a performance cost when a templatecannot be found in the current directory. The reason for this is that Struts 2must check for a template in the current theme first before falling back to theparent theme. In the future, this penalty could be eliminated by implementing amissing template cache in Struts 2.

  Do not create sessions unless you needthem.不要创建Session,除非你需要,国际化拦截器会创建Session

  Struts 2 does not create sessions unlessasked to (for example, by having the createSession interceptor in yourinterceptor stack). Note that when you use SiteMesh however, a session willalways be created (Seehttp://forums.opensymphony.com/thread.jspa?messageID=5688for details). The I18nInterceptor interceptor can create sessions, so make sureyou remove it, if you don't need it.

  When using Freemarker, try to use theFreemarker equivalent rather than using the JSP tags.使用模板时采用${}方式

  Freemarker has support for iterating lists,displaying properties, including other templates, macro's, and so on. There isa small performance cost when using the S2 tags instead of the Freemarkerequivalent (eg. should be replaced by ${foo}).

  疯狂Java培训的课程采用针对性培养,全面提升学员就业能力,重点加强训练职业素质。老师辛勤的讲解,让学员充分感受Java的魅力,充分激发每个学员对于编程的热爱,让学员在半年的时间内掌握8-10万的代码量,成为真正的技术高手,疯狂Java采用企业全真模拟开发训练,迅速积累项目经验。让学员迅速获得其他人需要花费两年才能获得的工作技能,无忧就业!

 

原创粉丝点击