日期:2014-05-16  浏览次数:20478 次

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="driverClass