Spring3 MVC 教程 (Mybatis+ExtJS基本权限管理)

说明: 
喷EXT的就绕行吧    
  • 验证码:采用开源的https://code.google.com/p/kaptcha/,代码自己修改了部分。
  • 上传文件:swfupload,参考论坛里面的另外一个帖子。
  • ext的tab非iframe模式,只加载一次ExtJS,速度还可以~自己命名jsp里面的变量的时候注意变量ID不要重复 参加下面部分js代码
  • 框架采用了mybatis+spring3MVC,应该说是学习springmvc的好例子。代码都有详细注释。
  • js的加载采用了yepnope,是一个基于条件的异步资源(JS和CSS)加载工具
  • mybatis的配置文件的生成采用了自己修改的mybatis的工具。mybatis-generator 修改版


其他: 
附件为maven构建的工程,需要lib的可以去网盘下载,里面也有base_power.pdm文件,可以自己转为mysql的脚本。 
建库语句在war包里面的readme文件夹下面,或者Authority\src\main\webapp\readme  
7z的分卷压缩老是搞不好,只好贴网盘地址了  
http://dl.dbank.com/c0bakwliui  
Js代码   收藏代码
  1. // tab主面板  
  2. index.tabPanel = new Ext.TabPanel({  
  3.     id: 'mainTabPanel',  
  4.     region: 'center',  
  5.     activeTab: 0,  
  6.     deferredRender: false,  
  7.     enableTabScroll: true,  
  8.     // bodyStyle:'height:100%',  
  9.     defaults: {  
  10.         layout: 'fit',  
  11.         autoScroll: true  
  12.     },  
  13.     plugins: new Ext.ux.TabCloseMenu({  
  14.         closeTabText: '关闭标签页',  
  15.         closeOtherTabsText: '关闭其他标签页',  
  16.         closeAllTabsText: '关闭所有标签页'  
  17.     }),  
  18.     items: [{  
  19.         id: 'home',  
  20.         title: '我的主页',  
  21.         iconCls: 'home',  
  22.         closable: false,  
  23.         autoScroll: true,  
  24.         autoLoad: {  
  25.             url: index.welcome,  
  26.             scripts: true,  
  27.             nocache: true  
  28.         }  
  29.     }],  
  30.     listeners: {  
  31.         'bodyresize'function (panel, neww, newh) {  
  32.             // 自动调整tab下面的panel的大小  
  33.             var tab = panel.getActiveTab();  
  34.             var centerpanel = Ext.getCmp(tab.id + "_div_panel");  
  35.             if (centerpanel) {  
  36.                 centerpanel.setHeight(newh - 2);  
  37.                 centerpanel.setWidth(neww - 2);  
  38.             }  
  39.         }  
  40.     }  
  41. });  


Html代码   收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"%>  
  2. <%@ include file="/WEB-INF/views/commons/taglibs.jsp"%>  
  3. <div id="changePwdDiv" style="width: 100%; height: 100%;">  
  4.     <div id="changePwdToolBarDiv"></div>  
  5.     <div id="changePwdFormDiv"></div>  
  6. </div>  
  7.   
  8. <script type="text/javascript">  
  9. $(document).ready(function() {  
  10.     //yepnope("${ctx}/resources/js/user/changepwd.js");  
  11.     Ext.ns("Ext.Authority.changepassword"); // 自定义一个命名空间  
  12.     changepwd = Ext.Authority.changepassword; // 定义命名空间的别名  
  13.     changepwd = {  
  14.         changeurl : ctx + "/user/changepwd"  
  15.     };  
  16.     // 编辑用户Form  
  17.     changepwd.changePwdFormPanel = new Ext.form.FormPanel({  
  18.         renderTo : 'changePwdFormDiv',  
  19.         border : false,  
  20.         defaults : {  
  21.             xtype : "textfield",  
  22.             labelWidth : 50  
  23.         },  
  24.         bodyStyle : 'padding:5px;background-color:transparent;',  
  25.         items : [ {// 原密码  
  26.             id : 'old_password',  
  27.             name : 'old_password',  
  28.             inputType : "password",  
  29.             fieldLabel : '原密码',  
  30.             anchor : '99%',  
  31.             allowBlank : false,  
  32.             maxLength : 32  
  33.         }, { // 新密码  
  34.             id : 'new_password',  
  35.             name : 'new_password',  
  36.             inputType : "password",  
  37.             fieldLabel : '新密码',  
  38.             anchor : '99%',  
  39.             allowBlank : false,  
  40.             maxLength : 32  
  41.         }, {// 确认密码  
  42.             id : 'compare_password',  
  43.             name : 'compare_password',  
  44.             inputType : "password",  
  45.             fieldLabel : '确认密码',  
  46.             vtype : "password",// 自定义的验证类型  
  47.             vtypeText : "两次输入的密码不一致!",  
  48.             confirmTo : "new_password",// 要比较的另外一个的组件的id  
  49.             anchor : '99%',  
  50.             allowBlank : false,  
  51.             maxLength : 32  
  52.         }, {// 账户ID  
  53.             xtype : 'hidden',  
  54.             name : 'userId',  
  55.             value : '${user.userId}'  
  56.         } ]  
  57.     });  
  58.     // 工具栏  
  59.     changepwd.changePwdToolbar = new Ext.Toolbar({  
  60.         renderTo : 'changePwdToolBarDiv',  
  61.         items : [ new Ext.Button({  
  62.             text : '保存',  
  63.             iconCls : 'save',  
  64.             handler : function() {  
  65.                 var form = changepwd.changePwdFormPanel.getForm();  
  66.                 if (form.isValid()) {  
  67.                     var values = form.getValues();  
  68.                     // 发送请求  
  69.                     Share.AjaxRequest({  
  70.                         url : changepwd.changeurl,  
  71.                         params : {  
  72.                             oldPassword : Ext.MD5(values.old_password),  
  73.                             newPassword : Ext.MD5(values.new_password),  
  74.                             comparePassword : Ext.MD5(values.compare_password),  
  75.                             userId : values.userId  
  76.                         },  
  77.                         callback : function(json) {  
  78.                             // 重新登录  
  79.                             Share.getWin().location = ctx;  
  80.                         },  
  81.                         falseFun : function(json) {//失败后想做的个性化操作函数  
  82.                             if (json.msg.indexOf('原密码不正确!请重新输入') > -1) {  
  83.                                 $("#old_password").focus().val('');  
  84.                                 return;  
  85.                             }  
  86.                             if (json.msg.indexOf('两次输入的新密码不一致') > -1) {  
  87.                                 $("#new_password").val('');  
  88.                                 $("#compare_password").val('').focus();  
  89.                                 return;  
  90.                             }  
  91.                             if (json.msg.indexOf('请输入正确的帐号和其注册邮箱') > -1) {  
  92.                                 return;  
  93.                             }  
  94.                         }  
  95.                     });  
  96.                 }  
  97.             }  
  98.         }), new Ext.Button({  
  99.             text : '取消',  
  100.             iconCls : 'cancel',  
  101.             handler : function() {  
  102.                 header.changePwdWindow.close();  
  103.             }  
  104.         }) ]  
  105.     });  
  106.     var events = "beforecopy beforepaste beforedrag contextmenu selectstart drag paste copy cut dragenter";  
  107.     $("#old_password").bind(events, function(e) {  
  108.         if (e && e.preventDefault)  
  109.             e.preventDefault();  
  110.         else  
  111.             window.event.returnValue = false;  
  112.         return false;  
  113.     });  
  114.     $("#new_password").bind(events, function(e) {  
  115.         return false;  
  116.     });  
  117.     $("#compare_password").bind(events, function(e) {  
  118.         return false;  
  119.     });  
  120. });  
  121. </script>  


Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  3.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xsi:schemaLocation="  
  5.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  6.     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
  7.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  8.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  9.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"  
  10.     default-lazy-init="true">  
  11.   
  12.     <!-- 只搜索@Controller 标注的类 不搜索其他标注的类 -->  
  13.     <context:component-scan base-package="com.chenxin.authority" use-default-filters="false">  
  14.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />  
  15.     </context:component-scan>  
  16.   
  17.     <!-- controller层的属性和配置文件读入 ,多个用逗号隔开 <context:property-placeholder location="classpath:/config/others/config.properties" /> -->  
  18.   
  19.     <!-- 应用属性文件读入 -->  
  20.     <bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  21.         <property name="ignoreResourceNotFound" value="true" />  
  22.         <property name="locations">  
  23.             <list>  
  24.                 <!-- 标准配置 -->  
  25.                 <value>classpath:/config/others/config.properties</value>  
  26.                 <value>classpath:/config/ibatis/jdbc.properties</value>  
  27.             </list>  
  28.         </property>  
  29.     </bean>  
  30.   
  31.     <!-- 用于持有applicationProperties,将properties转变为静态方法使用,PropertiesHolder.getProperty("somekey") -->  
  32.     <bean class="com.chenxin.authority.common.utils.PropertiesHolder">  
  33.         <property name="properties" ref="applicationProperties" />  
  34.     </bean>  
  35.   
  36.     <!-- PropertyPlaceholderConfigurer,用于spring ${placeholder}的解析 -->  
  37.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  38.         <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />  
  39.         <property name="properties" ref="applicationProperties" />  
  40.     </bean>  
  41.   
  42.     <!-- 激活 @Required @Autowired,JSR 250's @PostConstruct, @PreDestroy and @Resource 等标注 -->  
  43.     <context:annotation-config />  
  44.   
  45.   
  46.     <!-- 对某些静态资源,如css,图片等进行过滤 ,有引用 "/resources/**" 的路径引用转到工程的/resources/目录取资源 -->  
  47.     <!-- <mvc:resources mapping="/favicon.ico" location="/favicon.ico"/> -->  
  48.     <mvc:resources mapping="/resources/**" location="/resources/" />  
  49.   
  50.     <!-- 简单的地址映射 -->  
  51.     <!-- Forwards requests to the "/" resource to the "welcome" view   
  52.      <mvc:view-controller path="/admin" view-name="/admin/login" />   
  53.     -->  
  54.       
  55.     <mvc:annotation-driven />  
  56.     <!-- Configures support for @Controllers <mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping  
  57.     和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。  
  58.     如果不用这个,则要声明下面2个bean  
  59.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  60.         <property name="messageConverters">  
  61.             <list>  
  62.                 <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
  63.                     <property name="supportedMediaTypes" value="application/json"/>  
  64.                 </bean>  
  65.             </list>  
  66.         </property>  
  67.     </bean>  
  68.      -->  
  69.         <!--  
  70.     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">  
  71.          拦截器注册 -->  
  72.         <!-- <property name="interceptors">  
  73.             <bean class="com.company.project.common.springmvc.interceptor.SharedRenderVariableInterceptor"/>  
  74.         </property>   
  75.     </bean>  
  76.         -->  
  77.       
  78.       
  79.     <!-- jsp视图 -->  
  80.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  81.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
  82.         <property name="prefix" value="/WEB-INF/views/" />  
  83.         <property name="suffix" value=".jsp" />  
  84.     </bean>  
  85.   
  86.     <!-- 针对类、方法级别的权限拦截器 -->  
  87.     <mvc:interceptors>  
  88.         <mvc:interceptor>  
  89.             <!-- <mvc:mapping path="/fileupload" /> -->  
  90.             <mvc:mapping path="/main*" />  
  91.             <mvc:mapping path="/header*" />  
  92.             <mvc:mapping path="/welcome*" />  
  93.             <mvc:mapping path="/treeMenu*" />  
  94.             <mvc:mapping path="/role**/**" />  
  95.             <mvc:mapping path="/user**/**" />  
  96.             <mvc:mapping path="/module**/**" />  
  97.             <mvc:mapping path="/field**/**" />  
  98.             <bean class="com.chenxin.authority.web.interseptor.LoginInterceptor"></bean>  
  99.         </mvc:interceptor>  
  100.     </mvc:interceptors>  
  101.   
  102.     <!--开发期间注释掉,上线后启用 错误解析器 发生错误默认会跳转到/web-inf/views/timeout.jsp -->  
  103.     <!-- <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView"   
  104.         value="timeout" /> <property name="exceptionMappings"> <props> <prop key="java.sql.SQLException">timeout</prop> <prop key="java.lang.RuntimeException">timeout</prop>   
  105.         <prop key="org.springframework.transaction.TransactionException">timeout</prop> <prop key="org.springframework.dao.DataAccessException">timeout</prop>   
  106.         </props> </property> </bean> -->  
  107.   
  108.     <!-- 国际化,并且可以批定文件编码,可以使用classpath: 或者WEB-INF/ 前缀 -->  
  109.     <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
  110.         <property name="basenames">  
  111.             <list>  
  112.                 <value>classpath:/config/others/messages</value>  
  113.                 <value>classpath:ValidationMessages</value>  
  114.             </list>  
  115.         </property>  
  116.         <property name="defaultEncoding" value="UTF-8" />  
  117.         <property name="cacheSeconds" value="60" />  
  118.     </bean>  
  119.   
  120.     <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />  
  121.   
  122.     <!-- 文件上传 -->  
  123.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  124.         <!-- 解析request的编码 ,Default is ISO-8859-1 -->  
  125.         <property name="defaultEncoding" value="UTF-8" />  
  126.         <!-- 设置最大允许的大小(字节)。-1表示没有限制(默认) 1024*1024*10=10MB -->  
  127.         <property name="maxUploadSize" value="1048576000" />  
  128.         <!--被允许的最大的内存的大小,Default is 10240 bytes -->  
  129.         <property name="maxInMemorySize" value="20480" />  
  130.         <!-- 一个类似懒加载的属性.可以定义该属性.让解析文件的时候再抛异常,然后Controller中定义异常处理的方法 -->  
  131.         <property name="resolveLazily" value="true" />  
  132.     </bean>  
  133. </beans>  

代码不多贴了,上图片吧。 

 

 

 


 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值