背景
struts是apache基金組織中jakarta項目組的一個開源(open source)項目,主要就是實現(xiàn)了mvc設計模式,在struts中有自己的控制器(actionservlet),同時也提供了各種常用的頁面標簽庫以減少jsp頁面中的scriptlet代碼,struts實際上就屬于在傳統(tǒng)技術上發(fā)展起來的一種新的應用模式,其操作的本質(zhì)依然還是jsp、servlet、javabean等技術的應用,struts的體系結構圖如下。
最近將組內(nèi)項目的部署環(huán)境進行了一次升級。將jdk1.7s升級為1.8,resin替換為tomcat。在升級替換的過程中遇到了一些問題。特記錄再次,希望能幫助有同樣需求的朋友。
struts2 和 jdk8
項目中使用的struts2版本是2.3.35。
1
2
3
4
5
|
<dependency> <groupid>org.apache.struts</groupid> <artifactid>struts2-core</artifactid> <version> 2.3 . 35 </version> </dependency> |
struts2里面依賴xwork-core
1
2
3
4
5
|
<dependency> <groupid>org.apache.struts.xwork</groupid> <artifactid>xwork-core</artifactid> <version> 2.3 . 35 </version> </dependency> |
xwork-core依賴asm-*
問題來了!
低版本的asm不能在jdk1.8環(huán)境中使用。如果強行使用,會導致一些奇怪的問題。
例如:
只有一部分action類可以正常被struts2加載并處理http請求。某些在jdk1.7環(huán)境下可以正常工作的action不能在jdk1.8下使用。原來可以訪問的接口,現(xiàn)在是404。
具體問題出在:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
private void readclassdef(string classname) { if (!classname.endswith( ".class" )) { classname = classname.replace( '.' , '/' ) + ".class" ; } try { url resource = classloaderinterface.getresource(classname); if (resource != null ) { inputstream in = resource.openstream(); try { classreader classreader = new classreader(in); classreader.accept( new infobuildingvisitor( this ), classreader.skip_debug); } finally { in.close(); } } else { throw new xworkexception( "could not load " + classname); } } catch (ioexception e) { throw new xworkexception( "could not load " + classname, e); } } |
這部分代碼就因為使用了低版本的asm導致類解析失敗(indexoutofboundsexception)。
解決辦法一
最簡單方便的解決版本就是升級struts2的版本到2.5.x。新版本將xwork依賴直接合并到struts2-core中了。而且使用了asm 5.x版本,支持jdk8。
解決辦法二
使用struts2官方提供的一個插件。具體用法如下:
在項目中加入依賴:
1
2
3
4
5
|
<dependency> <groupid>org.apache.struts</groupid> <artifactid>struts2-java8-support-plugin</artifactid> <version> 2.3 . 35 </version> </dependency> |
排除asm依賴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<dependency> <groupid>org.apache.struts.xwork</groupid> <artifactid>xwork-core</artifactid> <exclusions> <exclusion> <groupid>asm</groupid> <artifactid>asm</artifactid> </exclusion> <exclusion> <groupid>asm</groupid> <artifactid>asm-commons</artifactid> </exclusion> </exclusions> </dependency> |
struts2 版本升級問題
標簽庫不兼容
眾所周知,struts2框架的安全問題很多,建議升級到最新版本2.5.x。
但是2.5.x版本的struts2提供的標簽庫和低版本的不兼容。這就會導致原有的jsp頁面不能正常渲染。
當然了,如果你的項目里面沒有使用struts2替換的標簽,這個問題可以忽略了。
核心類攔截器變化
1
2
3
4
5
6
7
8
9
10
11
|
<filter> <filter-name>struts2</filter-name> <filter- class >org.apache.struts2.dispatcher.filter.strutsprepareandexecutefilter</filter- class > <!-- org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter 之前的核心過濾器全類名會有個ng ,struts2. 5 核心過濾器沒有這個 --> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
aspectjweaver
我們項目使用的版本是:
1
2
3
4
5
|
<dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjweaver</artifactid> <version> 1.6 . 9 </version> </dependency> |
升級jdk1.8以后,需要同時升級該jar的版本到1.8.13。
參考
- Struts2.5配置
- Java 8 Support Plugin
- Struts+2.3+to+2.5+migration
- what-is-the-difference-between-struts-2-3-x-and-struts-2-5-x
- ASM-VERSIONS
- Struts2最新RCE漏洞S2-057
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://leokongwq.github.io/2018/11/07/struts2-with-jdk8.html