到這里已經寫到第12篇了,前11篇基本上把Spring MVC主要的內容都講了,現在就直接上一個項目吧,希望能對有需要的朋友有一些幫助。
一、首先看一下項目結構:
InfrastructureProjects:是抽取出的基礎項目,主要封裝了一些通用的操作。
SpringMVC3Demo:就是管理系統所在的項目。
WeiXinAPI:是之前做微信管理平台測試時封裝一些操作,如果不需要把該項目移除即可。
注:項目的前端UI框架用的是國外的一個基於Bootstrap框架的開發的Demo,如不需要替換為自己熟悉的UI框架即可。
二、實現的功能:
登錄:
注冊:
首頁:
用戶管理:
賬戶綁定:
角色管理:
角色授權:
權限管理:
組織機構管理:
這是權限添加界面:
PS:
1.關於權限配置:權限配置是按正則表達式匹配的,可參見下表是我現在數據庫里面的權限配置:
2.在Hibernate配置文件比如Role.hbm.xml中,authorities一對多我這里配的是lazy="false",是因為在用戶登錄時檢查權限肯定要獲取對應Role下的所有authorities,所以沒必要開lazy加載,但是如果你要添加其它功能是,請根據需求來決定是否開lazy,比如賬戶—>訂單這樣的就需要開lazy。關於Hibernate網上有很多資料可供參考,這里推薦一下劉冬大哥寫的NHibernate系列文章:http://www.cnblogs.com/GoodHelper/category/214139.html可以作為學習Hibernate的參考。
3.由於OpenSessionInView會有性能上的損失,所以項目沒有開OpenSessionInView,而且Hibernate的事務是配置到Service的,所以請在Service層返回數據時准備好所有需要的數據,否則在Controller或View中想調用lazy加載數據會拋出事務已關閉的異常。
4.運行項目時最好用新的chrome或firefox瀏覽器,Bootstrap3不支持IE8以下瀏覽器,而且用IE的話要想完全顯示出Bootstrap3的效果則至少IE10.
5.由於最近事情比較多,項目的國際化還沒來得及添加,可以根據系列(8)所講的內容自行添加也算做練手(^.^)~~~
6.項目引用的jar包是用的User Library添加進的,在把項目解壓到本地時有可能里面的引用的路徑和我機器上的不一樣,如果包路徑不正確請按包在自己機器上的路徑進行更改。
代碼下載:http://pan.baidu.com/s/1sjwdPLz
總共70多MB,但是別激動,項目沒那么大主要是找需要的包時,下載的zip文件解壓后會有一堆包文件,很多是沒用到的,我沒刪都放在Dependence文件夾下面了,只是在項目中對需要的包添加了引用。
數據庫文件下載:http://pan.baidu.com/s/1i33gczN
PPS:因為這個系統有2個項目,一個是通用的基礎項目,一個是網站,當時為了管理包的方便所以用了User Library的方式添加項目中需要的包,前些時間感覺電腦越來越慢了,就把電腦中的虛擬機卸載掉了,只保留的項目的源碼,然后有朋友告訴我項目導入后User Library里面內容是空的,之后我把項目導入到我win系統中的eclipse中也發現User Library沒有內容(又遇到了一個坑,還是我用的方式不對?)。所以,不知道User Library引用什么包的朋友可以參考第11篇的項目源碼,里面引用的包是直接添加到項目里面的,當然第11篇引用的包是少於這個項目的,不過少引用的包根據提示到Dependence文件夾下找到后添加即可,另:Infrastructure Projects中引用的User Library和SpringMVC3 Demo引用的是不同的,需要注意。
PPPS:貼一下一個朋友調試成功項目所引用的包(我機器的開發環境沒有了,懶得折騰了,原諒我的懶惰~),給需要的做一個參考:
InfrastructureProjects項目:
/InfrastructureProjects/lib/antlr-2.7.7.jar
/InfrastructureProjects/lib/dom4j-1.6.1.jar
/InfrastructureProjects/lib/hibernate-commons-annotations-4.0.2.Final.jar
/InfrastructureProjects/lib/hibernate-core-4.2.8.Final.jar
/InfrastructureProjects/lib/hibernate-jpa-2.0-api-1.0.1.Final.jar
/InfrastructureProjects/lib/javassist-3.18.1-GA.jar
/InfrastructureProjects/lib/jboss-logging-3.1.0.GA.jar
/InfrastructureProjects/lib/jboss-transaction-api_1.1_spec-1.0.1.Final.jar
/InfrastructureProjects/lib/jstl.jar
/InfrastructureProjects/lib/spring-beans-3.2.5.RELEASE
/InfrastructureProjects/lib/standard.jar
SpringMvc3Demo項目:
antlr-2.7.7.jar
aopalliance-1.0.jar
aspectjrt.jar
aspectjweaver.jar
commons-beanutils-1.8.0.jar
commons-collections-3.2.1.jar
commons-fileupload-1.3.1.jar
commons-io-2.4.jar
commons-lang-2.5.jar
commons-logging-1.1.3.jar
dom4j-1.6.1.jar
ezmorph-1.0.6.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.8.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-validator-4.3.1.Final.jar
hibernate-validator-annotation-processor-4.3.1.Final.jar
jackson-core-asl-1.9.13.jar
jackson-mapper-asl-1.9.13.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.0.CR2.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
joda-time-2.3.jar
json-lib-2.4-jdk15.jar
jstl.jar
mysql-connector-java-5.1.10.jar
proxool-0.9.1.jar
proxool-cglib.jar
spring-aop-3.2.5.RELEASE.jar
spring-aop-3.2.5.RELEASE-javadoc.jar
spring-aop-3.2.5.RELEASE-sources.jar
spring-aspects-3.2.5.RELEASE.jar
spring-aspects-3.2.5.RELEASE-javadoc.jar
spring-aspects-3.2.5.RELEASE-sources.jar
spring-beans-3.2.5.RELEASE.jar
spring-beans-3.2.5.RELEASE-javadoc.jar
spring-beans-3.2.5.RELEASE-sources.jar
spring-build-src-3.2.5.RELEASE.jar
spring-context-3.2.5.RELEASE.jar
spring-context-3.2.5.RELEASE-javadoc.jar
spring-context-3.2.5.RELEASE-sources.jar
spring-context-support-3.2.5.RELEASE.jar
spring-context-support-3.2.5.RELEASE-javadoc.jar
spring-context-support-3.2.5.RELEASE-sources.jar
spring-core-3.2.5.RELEASE.jar
spring-core-3.2.5.RELEASE-javadoc.jar
spring-core-3.2.5.RELEASE-sources.jar
spring-expression-3.2.5.RELEASE.jar
spring-expression-3.2.5.RELEASE-javadoc.jar
spring-expression-3.2.5.RELEASE-sources.jar
spring-instrument-3.2.5.RELEASE.jar
spring-instrument-3.2.5.RELEASE-javadoc.jar
spring-instrument-3.2.5.RELEASE-sources.jar
spring-instrument-tomcat-3.2.5.RELEASE.jar
spring-instrument-tomcat-3.2.5.RELEASE-javadoc.jar
spring-instrument-tomcat-3.2.5.RELEASE-sources.jar
spring-jdbc-3.2.5.RELEASE.jar
spring-jdbc-3.2.5.RELEASE-javadoc.jar
spring-jdbc-3.2.5.RELEASE-sources.jar
spring-jms-3.2.5.RELEASE.jar
spring-jms-3.2.5.RELEASE-javadoc.jar
spring-jms-3.2.5.RELEASE-sources.jar
spring-orm-3.2.5.RELEASE.jar
spring-orm-3.2.5.RELEASE-javadoc.jar
spring-orm-3.2.5.RELEASE-sources.jar
spring-oxm-3.2.5.RELEASE.jar
spring-oxm-3.2.5.RELEASE-javadoc.jar
spring-oxm-3.2.5.RELEASE-sources.jar
spring-struts-3.2.5.RELEASE.jar
spring-struts-3.2.5.RELEASE-javadoc.jar
spring-struts-3.2.5.RELEASE-sources.jar
spring-test-3.2.5.RELEASE.jar
spring-test-3.2.5.RELEASE-javadoc.jar
spring-test-3.2.5.RELEASE-sources.jar
spring-tx-3.2.5.RELEASE.jar
spring-tx-3.2.5.RELEASE-javadoc.jar
spring-tx-3.2.5.RELEASE-sources.jar
spring-web-3.2.5.RELEASE.jar
spring-web-3.2.5.RELEASE-javadoc.jar
spring-web-3.2.5.RELEASE-sources.jar
spring-webmvc-3.2.5.RELEASE.jar
spring-webmvc-3.2.5.RELEASE-javadoc.jar
spring-webmvc-3.2.5.RELEASE-sources.jar
spring-webmvc-portlet-3.2.5.RELEASE.jar
spring-webmvc-portlet-3.2.5.RELEASE-javadoc.jar
spring-webmvc-portlet-3.2.5.RELEASE-sources.jar
standard.jar
validation-api-1.0.0.GA.jar
PPPPS:請把InfrastructureProjects項目中ChainEntityDao類中的setLevelCode方法改為以下代碼:
protected void setLevelCode(EntityType entity) throws ValidatException { PKType id=(PKType)super.getSession().save(entity); if(entity.getParent()==null){ entity.setLevelCode(id.toString()); super.getSession().update(entity); } else{ EntityType parentEntity=super.get(entity.getParent().getId()); if(parentEntity==null) throw new ValidatException("The parent does not exist!"); else{ entity.setLevelCode(parentEntity.getLevelCode()+","+id.toString()); super.getSession().update(entity); } } /*DecimalFormat df = new DecimalFormat( "000" ); EntityType maxLevelCodeEntity; if(entity.getParent()==null) maxLevelCodeEntity=getMaxLevelCodeExceCurrentEntity(null, entity.getId()); else maxLevelCodeEntity=getMaxLevelCodeExceCurrentEntity(entity.getParent().getId(), entity.getId()); if(maxLevelCodeEntity==null){ if(entity.getParent()==null) entity.setLevelCode(df.format(1)); else entity.setLevelCode(entity.getParent().getLevelCode()+df.format(1)); } else{ String maxLevelCodeStr=maxLevelCodeEntity.getLevelCode(); int maxLevelCode=Integer.parseInt(maxLevelCodeStr.substring(maxLevelCodeStr.length()-3, maxLevelCodeStr.length())); if(maxLevelCode+1>999) throw new ValidatException("The levelcode exceeds the maximum of 999!"); else{ if(entity.getParent()==null) entity.setLevelCode(df.format(maxLevelCode+1)); else entity.setLevelCode(entity.getParent().getLevelCode()+df.format(maxLevelCode+1)); } }*/ }
原來代碼里面注釋的部分才是最終的代碼,下面的才是應該被注釋的,不知道為什么變成以前的版本了。