论坛首页 Java企业应用论坛

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

浏览 64622 次
精华帖 (7) :: 良好帖 (4) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-01-11   最后修改:2012-12-13
系统admin,密码:1

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
// tab主面板
index.tabPanel = new Ext.TabPanel({
    id: 'mainTabPanel',
    region: 'center',
    activeTab: 0,
    deferredRender: false,
    enableTabScroll: true,
    // bodyStyle:'height:100%',
    defaults: {
        layout: 'fit',
        autoScroll: true
    },
    plugins: new Ext.ux.TabCloseMenu({
        closeTabText: '关闭标签页',
        closeOtherTabsText: '关闭其他标签页',
        closeAllTabsText: '关闭所有标签页'
    }),
    items: [{
        id: 'home',
        title: '我的主页',
        iconCls: 'home',
        closable: false,
        autoScroll: true,
        autoLoad: {
            url: index.welcome,
            scripts: true,
            nocache: true
        }
    }],
    listeners: {
        'bodyresize': function (panel, neww, newh) {
            // 自动调整tab下面的panel的大小
            var tab = panel.getActiveTab();
            var centerpanel = Ext.getCmp(tab.id + "_div_panel");
            if (centerpanel) {
                centerpanel.setHeight(newh - 2);
                centerpanel.setWidth(neww - 2);
            }
        }
    }
});


<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ include file="/WEB-INF/views/commons/taglibs.jsp"%>
<div id="changePwdDiv" style="width: 100%; height: 100%;">
	<div id="changePwdToolBarDiv"></div>
	<div id="changePwdFormDiv"></div>
</div>

<script type="text/javascript">
$(document).ready(function() {
	//yepnope("${ctx}/resources/js/user/changepwd.js");
	Ext.ns("Ext.Authority.changepassword"); // 自定义一个命名空间
	changepwd = Ext.Authority.changepassword; // 定义命名空间的别名
	changepwd = {
		changeurl : ctx + "/user/changepwd"
	};
	// 编辑用户Form
	changepwd.changePwdFormPanel = new Ext.form.FormPanel({
		renderTo : 'changePwdFormDiv',
		border : false,
		defaults : {
			xtype : "textfield",
			labelWidth : 50
		},
		bodyStyle : 'padding:5px;background-color:transparent;',
		items : [ {// 原密码
			id : 'old_password',
			name : 'old_password',
			inputType : "password",
			fieldLabel : '原密码',
			anchor : '99%',
			allowBlank : false,
			maxLength : 32
		}, { // 新密码
			id : 'new_password',
			name : 'new_password',
			inputType : "password",
			fieldLabel : '新密码',
			anchor : '99%',
			allowBlank : false,
			maxLength : 32
		}, {// 确认密码
			id : 'compare_password',
			name : 'compare_password',
			inputType : "password",
			fieldLabel : '确认密码',
			vtype : "password",// 自定义的验证类型
			vtypeText : "两次输入的密码不一致!",
			confirmTo : "new_password",// 要比较的另外一个的组件的id
			anchor : '99%',
			allowBlank : false,
			maxLength : 32
		}, {// 账户ID
			xtype : 'hidden',
			name : 'userId',
			value : '${user.userId}'
		} ]
	});
	// 工具栏
	changepwd.changePwdToolbar = new Ext.Toolbar({
		renderTo : 'changePwdToolBarDiv',
		items : [ new Ext.Button({
			text : '保存',
			iconCls : 'save',
			handler : function() {
				var form = changepwd.changePwdFormPanel.getForm();
				if (form.isValid()) {
					var values = form.getValues();
					// 发送请求
					Share.AjaxRequest({
						url : changepwd.changeurl,
						params : {
							oldPassword : Ext.MD5(values.old_password),
							newPassword : Ext.MD5(values.new_password),
							comparePassword : Ext.MD5(values.compare_password),
							userId : values.userId
						},
						callback : function(json) {
							// 重新登录
							Share.getWin().location = ctx;
						},
						falseFun : function(json) {//失败后想做的个性化操作函数
							if (json.msg.indexOf('原密码不正确!请重新输入') > -1) {
								$("#old_password").focus().val('');
								return;
							}
							if (json.msg.indexOf('两次输入的新密码不一致') > -1) {
								$("#new_password").val('');
								$("#compare_password").val('').focus();
								return;
							}
							if (json.msg.indexOf('请输入正确的帐号和其注册邮箱') > -1) {
								return;
							}
						}
					});
				}
			}
		}), new Ext.Button({
			text : '取消',
			iconCls : 'cancel',
			handler : function() {
				header.changePwdWindow.close();
			}
		}) ]
	});
	var events = "beforecopy beforepaste beforedrag contextmenu selectstart drag paste copy cut dragenter";
	$("#old_password").bind(events, function(e) {
		if (e && e.preventDefault)
			e.preventDefault();
		else
			window.event.returnValue = false;
		return false;
	});
	$("#new_password").bind(events, function(e) {
		return false;
	});
	$("#compare_password").bind(events, function(e) {
		return false;
	});
});
</script>



<?xml version="1.0" encoding="UTF-8"?>
<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"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd	
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
	default-lazy-init="true">

	<!-- 只搜索@Controller 标注的类 不搜索其他标注的类 -->
	<context:component-scan base-package="com.chenxin.authority" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<!-- controller层的属性和配置文件读入 ,多个用逗号隔开 <context:property-placeholder location="classpath:/config/others/config.properties" /> -->

	<!-- 应用属性文件读入 -->
	<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<!-- 标准配置 -->
				<value>classpath:/config/others/config.properties</value>
				<value>classpath:/config/ibatis/jdbc.properties</value>
			</list>
		</property>
	</bean>

	<!-- 用于持有applicationProperties,将properties转变为静态方法使用,PropertiesHolder.getProperty("somekey") -->
	<bean class="com.chenxin.authority.common.utils.PropertiesHolder">
		<property name="properties" ref="applicationProperties" />
	</bean>

	<!-- PropertyPlaceholderConfigurer,用于spring ${placeholder}的解析 -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="properties" ref="applicationProperties" />
	</bean>

	<!-- 激活 @Required @Autowired,JSR 250's @PostConstruct, @PreDestroy and @Resource 等标注 -->
	<context:annotation-config />


	<!-- 对某些静态资源,如css,图片等进行过滤 ,有引用 "/resources/**" 的路径引用转到工程的/resources/目录取资源 -->
	<!-- <mvc:resources mapping="/favicon.ico" location="/favicon.ico"/> -->
	<mvc:resources mapping="/resources/**" location="/resources/" />

	<!-- 简单的地址映射 -->
	<!-- Forwards requests to the "/" resource to the "welcome" view 
	 <mvc:view-controller path="/admin" view-name="/admin/login" /> 
	-->
	
	<mvc:annotation-driven />
	<!-- Configures support for @Controllers <mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping
	和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。
	如果不用这个,则要声明下面2个bean
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
	            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
	             	<property name="supportedMediaTypes" value="application/json"/>
	            </bean>
            </list>
        </property>
    </bean>
	 -->
		<!--
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
		 拦截器注册 -->
		<!-- <property name="interceptors">
			<bean class="com.company.project.common.springmvc.interceptor.SharedRenderVariableInterceptor"/>
		</property> 
	</bean>
		-->
	
	
	<!-- jsp视图 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<!-- 针对类、方法级别的权限拦截器 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<!-- <mvc:mapping path="/fileupload" /> -->
			<mvc:mapping path="/main*" />
			<mvc:mapping path="/header*" />
			<mvc:mapping path="/welcome*" />
			<mvc:mapping path="/treeMenu*" />
			<mvc:mapping path="/role**/**" />
			<mvc:mapping path="/user**/**" />
			<mvc:mapping path="/module**/**" />
			<mvc:mapping path="/field**/**" />
			<bean class="com.chenxin.authority.web.interseptor.LoginInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>

	<!--开发期间注释掉,上线后启用 错误解析器 发生错误默认会跳转到/web-inf/views/timeout.jsp -->
	<!-- <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" 
		value="timeout" /> <property name="exceptionMappings"> <props> <prop key="java.sql.SQLException">timeout</prop> <prop key="java.lang.RuntimeException">timeout</prop> 
		<prop key="org.springframework.transaction.TransactionException">timeout</prop> <prop key="org.springframework.dao.DataAccessException">timeout</prop> 
		</props> </property> </bean> -->

	<!-- 国际化,并且可以批定文件编码,可以使用classpath: 或者WEB-INF/ 前缀 -->
	<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="basenames">
			<list>
				<value>classpath:/config/others/messages</value>
				<value>classpath:ValidationMessages</value>
			</list>
		</property>
		<property name="defaultEncoding" value="UTF-8" />
		<property name="cacheSeconds" value="60" />
	</bean>

	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

	<!-- 文件上传 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 解析request的编码 ,Default is ISO-8859-1 -->
		<property name="defaultEncoding" value="UTF-8" />
		<!-- 设置最大允许的大小(字节)。-1表示没有限制(默认) 1024*1024*10=10MB -->
		<property name="maxUploadSize" value="1048576000" />
		<!--被允许的最大的内存的大小,Default is 10240 bytes -->
		<property name="maxInMemorySize" value="20480" />
		<!-- 一个类似懒加载的属性.可以定义该属性.让解析文件的时候再抛异常,然后Controller中定义异常处理的方法 -->
		<property name="resolveLazily" value="true" />
	</bean>
</beans>


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
















  • Authority.7z (1.2 MB)
  • 描述: maven构建
  • 下载次数: 6554
   发表时间:2012-01-12  
看了您的代码,挺不错的,但是为什么有那么多的数据库连接池配置啊,都要用还是注掉其他的,只留一个就成了
0 请登录后投票
   发表时间:2012-01-12   最后修改:2012-01-12
mazhiyuan 写道
看了您的代码,挺不错的,但是为什么有那么多的数据库连接池配置啊,都要用还是注掉其他的,只留一个就成了


以前做的一个连接池测试,只用一个就可以了。放着也没什么大碍的。
0 请登录后投票
   发表时间:2012-01-12  
嗯,不错学习一下
0 请登录后投票
   发表时间:2012-01-12  
想知道LZ是怎么解决 在 FF下上传文件:swfupload?
0 请登录后投票
   发表时间:2012-01-12  
感觉不错,支持下
0 请登录后投票
   发表时间:2012-01-12  
springmvc 啊,刚接触,学习了。。。
0 请登录后投票
   发表时间:2012-01-12   最后修改:2012-01-12
通常像楼主这样的帖子我都比较膜拜,这个才是好帖子.
其实我倒觉得用ext可维护性太差了,个人不是太喜欢用这个。我带的项目都是用html和美工来做的。。。

还有特别想说的是:建议用这个配置<mvc:annotation-driven />  ,
我之前就是自定义的AnnotationMethodHandlerAdapter和DefaultAnnotationHandlerMapping,结果有支持的特性没有注入,查资料也比较不好找,比如说annotation bindingResult验证时 save(UserParam userParam, BindingResult bindingResult)的结果bindingResult.hasErrors()始终false,就是这个配置有什么默认的没有注入的导致的
0 请登录后投票
   发表时间:2012-01-12  
看到EXT,就不想看了
0 请登录后投票
   发表时间:2012-01-12  
经典啊,感谢分享!
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics