spring3和struts2整合實現零配置的教程
阿新 • • 發佈:2019-01-05
我們目前做的專案使用的就是這種方式,今天比較有空,就寫了個demo演示下這個功能。所謂的零配置其實並不是沒有配置檔案,只是約定大於配置而已,約定做好了,整個專案都會清晰明瞭很多。詳細的約定在struts2-spring-plugin-xxx.jar裡面的struts-plugin.xml中有說明。
今天在測試的時候,我struts2-spring-plugin這個jar用的是最新版的,也就是2.3.16.3,按我們專案中的配置完後,請求一直有問題,我沒有具體看官方文件,對比了下版本2.1.8.1,struts-plugin.xml中的內容變化挺大,估計配置有變化,這個問題有時間看官過方文件後再更新過來吧。
2.1.8.1版本的struts-plugin.xml如下:
2.3.16.3版本的struts-plugin.xml如下:
目測新版本的會更加靈活。
這個整合很簡單,我只是讓spring去管理了action,重點是struts.xml的配置,如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.action.extension" value="action" /><!-- struts處理的請求字尾 --> <constant name="struts.enable.SlashesInActionNames" value="true"></constant> <package name="zuidaima" extends="struts-default"> <action name="*_*" class="{1}Action" method="{2}" > <result name="default">${target}</result> <result name="redirect" type="redirect">${target}</result> <result name="redirectAction" type="redirectAction">${target}</result> </action> </package> </struts>
說明下:在這個示例中,action規定的請求格式是action的名稱+“_”+請求的方法名稱,比如請求的是UserAction的getUser方法,則請求地址為:user_getUser.action
大家如果想了解更多的配置,詳細看下struts-plugin.xml中的常量說明。
專案截圖:
開發環境:eclipse kepler + jdk7 + win7 64 + maven