`
jameszhao83
  • 浏览: 66031 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

Struts2 Spring Hibernate Extjs 整合简单用例,extjs代码已经经过优化

阅读更多
Action部分,这个部分关于id的处理很不好,但愿有同志能看到给提个醒,怎样做能好一点
package com.james.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.james.domain.User;
import com.james.service.UserService;

@Component("usermgr")
@Scope("prototype")
public class UserAction {
	public List<User> users;
    public String userName;
    public String passWord;
    public String telphone;
    public String context;
    public String address;
    public String  id;
    public UserService userService;
    public boolean success=true;
    public User user=new User();
    
    public String save(){
    	System.out.println("action method save get id is:"+id);
    	try {
			int vid=Integer.parseInt(id);
    		this.updata();
		} catch (NumberFormatException e) {
	    	user.setAddress(address);
	    	user.setContext(context);
	    	user.setPassWord(passWord);
	    	user.setTelphone(telphone);
	    	user.setUserName(userName);
	    	userService.save(user);
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
    	return "success";
    }
    public String del(){
    	int vid=Integer.parseInt(id);

    	userService.del(vid);
    	return "success";
    }
    public String updata(){
    	System.out.println("action method update get id is:"+id);
    	int vid=Integer.parseInt(id);
    	user.setAddress(address);
    	user.setContext(context);
    	user.setPassWord(passWord);
    	user.setTelphone(telphone);
    	user.setUserName(userName);
    	user.setId(vid);
    	userService.update(user);
    	return "success";
    }
    public String findById(){
    	return "success";
    }
    public String findAll(){
    	users=userService.findAll();
    	return "success";
    }
    public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	public String getTelphone() {
		return telphone;
	}
	public void setTelphone(String telphone) {
		this.telphone = telphone;
	}
	public String getContext() {
		return context;
	}
	public void setContext(String context) {
		this.context = context;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String findByName(){
    	return "success";
    }

	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	

	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}

}

 

spring.xml

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
       <context:annotation-config  />
       <context:component-scan base-package="com.james"/>
       <!-- 数据库配置部分 -->
       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
          <property name="driverClassName">
               <value>com.mysql.jdbc.Driver</value>
          </property>
          <property name="url">
               <value>jdbc:mysql://localhost:3306/mydb</value>
          </property>
          <property name="username">
                <value>root</value>
          </property>
          <property name="password">
                <value>a</value>
          </property>
       </bean>
       <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="annotatedClasses">
               <list>
                  <value>com.james.domain.User</value>
               </list>
            </property>
            <property name="hibernateProperties">
               <props>
                  <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                  <prop key="hibernate.show_sql">true</prop>
                  <prop key="hibernate.hbm2ddl.auto">update</prop>
               </props>
            </property>
       </bean>
       <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
             <property name="sessionFactory" ref="sessionFactory"></property>
       </bean>
</beans>

 

extjs部分

 

Ext.ns("com.james");
com.james.UserPanel = Ext.extend(Ext.Panel, {
			fields : ['id', 'userName', 'passWord', 'telphone', 'context',
					'address'],
			createEdit:function(){
							this.win = new Ext.Window({
							width : 300,
							height : 300,
							title : "用户维护",
							items : {
								xtype : "form",
								layout : "form",
								items : [
								        {xtype:"hidden",name:"id"},
									   	{
											xtype : "textfield",
											fieldLabel : "姓名",
											name : "userName",
											anchor : "70%"
										}, {
											xtype : "textfield",
											fieldLabel : "密码",
											name : "passWord",
											anchor : "70%"
										}, {
											xtype : "textfield",
											fieldLabel : "电话",
											name : "telphone",
											anchor : "70%"
										}, {
											xtype : "textfield",
											fieldLabel : "地址",
											name : "address",
											anchor : "70%"
										}, {
											xtype : "textarea",
											fieldLabel : "附录",
											name : "context",
											anchor : "70%"
										}]
							},
							buttons : [{
										text : "提交",
										handler : function() {
											this.win.getComponent(0).getForm()
													.submit({
														url : "usermgr!save",
														success : function() {
															Ext.Msg
																	.alert("添加成功");
															this.win.close();
															this.vstore.reload();
														},scope:this
													});
										},scope:this
									}, {
										text : "重置"
									}, {
										text : "取消",
										handler : function() {
											this.win.close();
										}
									}]
						});
			},
			// *********************************************添加用户用户**************************************************************
			//这种方式只要是有回调函数的地方都必须写 scope:this
			adduser : function() {
                this.createEdit();
				this.win.show();
				// ***********************************************************************************************************
			},
			deluser : function() {
				var record =this.grid.getSelectionModel().getSelected();
				var vid = record.get("id");
				Ext.Ajax.request({
							url : "usermgr!del",
							params : {
								id : vid
							},
							callback : function() {
								Ext.Msg.alert("信息提示", "删除成功");
								this.vstore.reload();
							},scope:this
						});
			},
			// ************************************修改用户***********************************************************************
			updateuser : function() {
               this.createEdit();
				this.win.show();
				// 获取当前鼠标选择记录
				var record = this.grid.getSelectionModel().getSelected();
				this.win.getComponent(0).getForm().loadRecord(record);
			},
		  // ***************************初始化窗口********************************************************************************
			initComponent : function() {
				com.james.UserPanel.superclass.initComponent.call(this);
				this.vstore = new Ext.data.JsonStore({
							url : "usermgr!findAll",
							root : "users",
							idProperty : "id",
							fields :this.fields
						});
				var p1 = new Ext.Panel({
							title : "panel1",
							height : 100,
							collapsible : true
						});
				var p2 = new Ext.Panel({
							title : "panel2",
							height : 100,
							collapsible : true
						});
				var p3 = new Ext.Panel({
							title : "panel3",
							height : 100,
							collapsible : true,
							animCollapse : true
						});
				this. grid = new Ext.grid.GridPanel({
							tbar : [{
										text : "添加",
										handler : this.adduser,scope:this
									}, "-", {
										text : "删除",
										handler : this.deluser,scope:this
									}, "-", {
										text : "修改",
										handler : this.updateuser,scope:this
									}],
							store : this.vstore,
							autoHeight : true,
							columns : [{
										header : "编号",
										dataIndex : "id"
									}, {
										header : "姓名",
										dataIndex : "userName"
									}, {
										header : "电话",
										dataIndex : "telphone"
									}, {
										header : "密码",
										dataIndex : "passWord"
									}, {
										header : "备注",
										dataIndex : "context"
									}, {
										header : "地址",
										dataIndex : "address"
									}]
						});
				this.add(this.grid);
				this.vstore.load();
			}
		});

Ext.onReady(function() {
			var up = new com.james.UserPanel({
						title : "jlksfdjklasfdj"
					});
			var vp = new Ext.Viewport({
						layout : "border",
						items : [{
									region : "north",
									height : 200
								}, {
									region : "west",
									width : 200
								}, {
									region : "center",
									items : [up]
								}]
					});
		})

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics