`

Spring Roo 自动化集成开发工具

阅读更多

Roo是一种 Spring 开发的辅助工具,使用命令行操作来生成自动化项目,操作非常类似于rails

我这里使用spring tool suit来开发一个demo项目

首先新建个spring template project,然后选择template类型为Roo Wep App (based on Roo 1.0.0.RC1)
这样,便会通过roo的命令自动生成一个标准的maven web项目,里面含有最基本的Spring 配置

建好项目后,我们可以在project上运行roo shell
通过hint命令,我们可以很快的了解各种操作提示

首先,设置数据库

Roo shell代码 复制代码
  1. install jpa -provider HIBERNATE -database MYSQL  
install jpa -provider HIBERNATE -database MYSQL


roo会帮你在配置文件applicationContext.xml中配置好了相应的数据源配置,并且在 pom.xml中已经添加jpa和数据库驱动等相关的依赖。

然后新建一个domain

Roo shell代码 复制代码
  1. new persistent class jpa -name ~.domain.Employee -testAutomatically  
new persistent class jpa -name ~.domain.Employee -testAutomatically



这时roo会帮我们生成Employee类,这时Employee类还没有任何字段,我们可以通过roo shell往里面加入一些字段

Roo shell代码 复制代码
  1. add field string -fieldName name -notNull -sizeMax 200  
  2. add field date jpa -fieldName birth -type java.util.Date   
  3. ......  
add field string -fieldName name -notNull -sizeMax 200
add field date jpa -fieldName birth -type java.util.Date
......



最终生成的Employee如下:

Java代码 复制代码
  1. @Entity  
  2. @RooEntity  
  3. @RooJavaBean  
  4. @RooToString  
  5. public class Employee {   
  6.   
  7.     @NotNull  
  8.     @Size(max = 200)   
  9.     private String name;   
  10.   
  11.     @Temporal(TemporalType.TIMESTAMP)   
  12.     private Date birth;   
  13. }  
@Entity
@RooEntity
@RooJavaBean
@RooToString
public class Employee {

    @NotNull
    @Size(max = 200)
    private String name;

    @Temporal(TemporalType.TIMESTAMP)
    private Date birth;
}



你会发现,怎么没有get set呢?我们反编译看下编译后的class代码

Java代码 复制代码
  1. // Decompiled by DJ v3.5.5.77 Copyright 2003 Atanas Neshkov  Date: 2009-8-8 21:37:43   
  2. // Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!   
  3. // Decompiler options: packimports(3)    
  4. // Source File Name:   Employee.java   
  5.   
  6. package com.javaeye.domain;   
  7.   
  8. import java.util.Date;   
  9. import java.util.List;   
  10. import javax.persistence.EntityManager;   
  11. import org.aspectj.runtime.reflect.Factory;   
  12. import org.springframework.beans.factory.annotation.Configurable;   
  13. import org.springframework.beans.factory.aspectj.*;   
  14.   
  15. // Referenced classes of package com.javaeye.domain:   
  16. //            Employee_Roo_ToString, Employee_Roo_Entity, Employee_Roo_JavaBean   
  17.   
  18. public class Employee   
  19.     implements ConfigurableObject   
  20. {   
  21.   
  22.     public Employee()   
  23.     {   
  24.         org.aspectj.lang.JoinPoint joinpoint1 = Factory.makeJP(ajc$tjp_1, thisthis);   
  25.         org.aspectj.lang.JoinPoint joinpoint = Factory.makeJP(ajc$tjp_0, thisthis);   
  26.         if(this != null && getClass().isAnnotationPresent(org/springframework/beans/factory/annotation/Configurable) && AnnotationBeanConfigurerAspect.ajc$if_1((Configurable)getClass().getAnnotation(org/springframework/beans/factory/annotation/Configurable)))   
  27.             AnnotationBeanConfigurerAspect.aspectOf().ajc$before$org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect$1$e854fa65(this);   
  28.         if(this != null && getClass().isAnnotationPresent(org/springframework/beans/factory/annotation/Configurable) && (this == null || !getClass().isAnnotationPresent(org/springframework/beans/factory/annotation/Configurable) || !AnnotationBeanConfigurerAspect.ajc$if_1((Configurable)getClass().getAnnotation(org/springframework/beans/factory/annotation/Configurable))) && AbstractDependencyInjectionAspect.ajc$if_0(joinpoint))   
  29.             AnnotationBeanConfigurerAspect.aspectOf().ajc$afterReturning$org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect$2$1ea6722c(this);   
  30.         Employee_Roo_Entity.ajc$interFieldInit$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$entityManager(this);   
  31.         Employee_Roo_Entity.ajc$interFieldInit$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$id(this);   
  32.         Employee_Roo_Entity.ajc$interFieldInit$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$version(this);   
  33.         if(!AnnotationBeanConfigurerAspect.ajc$if_1((Configurable)getClass().getAnnotation(org/springframework/beans/factory/annotation/Configurable)) && AbstractDependencyInjectionAspect.ajc$if_0(joinpoint1))   
  34.             AnnotationBeanConfigurerAspect.aspectOf().ajc$afterReturning$org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect$2$1ea6722c(this);   
  35.     }   
  36.   
  37.     public static String ajc$privFieldGet$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$name(Employee employee)   
  38.     {   
  39.         return employee.name;   
  40.     }   
  41.   
  42.     public static void ajc$privFieldSet$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$name(Employee employee, String s)   
  43.     {   
  44.         employee.name = s;   
  45.     }   
  46.   
  47.     public static Date ajc$privFieldGet$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$birth(Employee employee)   
  48.     {   
  49.         return employee.birth;   
  50.     }   
  51.   
  52.     public static void ajc$privFieldSet$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$birth(Employee employee, Date date)   
  53.     {   
  54.         employee.birth = date;   
  55.     }   
  56.   
  57.     public static long countEmployees()   
  58.     {   
  59.         return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$countEmployees();   
  60.     }   
  61.   
  62.     public static EntityManager entityManager()   
  63.     {   
  64.         return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$entityManager();   
  65.     }   
  66.   
  67.     public static List findAllEmployees()   
  68.     {   
  69.         return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$findAllEmployees();   
  70.     }   
  71.   
  72.     public static Employee findEmployee(Long long1)   
  73.     {   
  74.         return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$findEmployee(long1);   
  75.     }   
  76.   
  77.     public static List findEmployeeEntries(int i, int j)   
  78.     {   
  79.         return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$findEmployeeEntries(i, j);   
  80.     }   
  81.   
  82.     public void flush()   
  83.     {   
  84. Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$flush(this);   
  85.     }   
  86.   
  87.     public Date getBirth()   
  88.     {   
  89.         return Employee_Roo_JavaBean.ajc$interMethod$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$getBirth(this);   
  90.     }   
  91.   
  92.     public Long getId()   
  93.     {   
  94.         return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$getId(this);   
  95.     }   
  96.   
  97.     public String getName()   
  98.     {   
  99.         return Employee_Roo_JavaBean.ajc$interMethod$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$getName(this);   
  100.     }   
  101.   
  102.     public Integer getVersion()   
  103.     {   
  104.         return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$getVersion(this);   
  105.     }   
  106.   
  107.     public void merge()   
  108.     {   Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$merge(this);   
  109.     }   
  110.   
  111.     public void persist()   
  112.     {     Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$persist(this);   
  113.     }   
  114.   
  115.     public void remove()   
  116.     {   
  117.         Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$remove(this);   
  118.     }   
  119.   
  120.     public void setBirth(Date date)   
  121.     {   
  122.         Employee_Roo_JavaBean.ajc$interMethod$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$setBirth(this, date);   
  123.     }   
  124.   
  125.     public void setId(Long long1)   
  126.     {   
  127.         Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$setId(this, long1);   
  128.     }   
  129.   
  130.     public void setName(String s)   
  131.     {   
  132.         Employee_Roo_JavaBean.ajc$interMethod$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$setName(this, s);   
  133.     }   
  134.   
  135.     public void setVersion(Integer integer)   
  136.     {   
  137.         Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$setVersion(this, integer);   
  138.     }   
  139.   
  140.     public String toString()   
  141.     {   
  142.         return Employee_Roo_ToString.ajc$interMethod$com_javaeye_domain_Employee_Roo_ToString$com_javaeye_domain_Employee$toString(this);   
  143.     }   
  144.   
  145.     private String name;   
  146.     private Date birth;   
  147.     public transient EntityManager ajc$interField$com_javaeye_domain$entityManager;   
  148.     public Long ajc$interField$com_javaeye_domain_Employee_Roo_Entity$id;   
  149.     public Integer ajc$interField$com_javaeye_domain_Employee_Roo_Entity$version;   
  150.     private static final org.aspectj.lang.JoinPoint.StaticPart ajc$tjp_0; /* synthetic field */  
  151.     private static final org.aspectj.lang.JoinPoint.StaticPart ajc$tjp_1; /* synthetic field */  
  152.   
  153.     static    
  154.     {   
  155.         Factory factory = new Factory("Employee.java", Class.forName("com.javaeye.domain.Employee"));   
  156.         ajc$tjp_0 = factory.makeSJP("initialization", factory.makeConstructorSig("1""org.springframework.beans.factory.aspectj.ConfigurableObject"""""""), 17);   
  157.         ajc$tjp_1 = factory.makeSJP("initialization", factory.makeConstructorSig("1""com.javaeye.domain.Employee"""""""), 17);   
  158.     }   
  159. }  
// Decompiled by DJ v3.5.5.77 Copyright 2003 Atanas Neshkov  Date: 2009-8-8 21:37:43
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   Employee.java

package com.javaeye.domain;

import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import org.aspectj.runtime.reflect.Factory;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.aspectj.*;

// Referenced classes of package com.javaeye.domain:
//            Employee_Roo_ToString, Employee_Roo_Entity, Employee_Roo_JavaBean

public class Employee
    implements ConfigurableObject
{

    public Employee()
    {
        org.aspectj.lang.JoinPoint joinpoint1 = Factory.makeJP(ajc$tjp_1, this, this);
        org.aspectj.lang.JoinPoint joinpoint = Factory.makeJP(ajc$tjp_0, this, this);
        if(this != null && getClass().isAnnotationPresent(org/springframework/beans/factory/annotation/Configurable) && AnnotationBeanConfigurerAspect.ajc$if_1((Configurable)getClass().getAnnotation(org/springframework/beans/factory/annotation/Configurable)))
            AnnotationBeanConfigurerAspect.aspectOf().ajc$before$org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect$1$e854fa65(this);
        if(this != null && getClass().isAnnotationPresent(org/springframework/beans/factory/annotation/Configurable) && (this == null || !getClass().isAnnotationPresent(org/springframework/beans/factory/annotation/Configurable) || !AnnotationBeanConfigurerAspect.ajc$if_1((Configurable)getClass().getAnnotation(org/springframework/beans/factory/annotation/Configurable))) && AbstractDependencyInjectionAspect.ajc$if_0(joinpoint))
            AnnotationBeanConfigurerAspect.aspectOf().ajc$afterReturning$org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect$2$1ea6722c(this);
        Employee_Roo_Entity.ajc$interFieldInit$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$entityManager(this);
        Employee_Roo_Entity.ajc$interFieldInit$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$id(this);
        Employee_Roo_Entity.ajc$interFieldInit$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$version(this);
        if(!AnnotationBeanConfigurerAspect.ajc$if_1((Configurable)getClass().getAnnotation(org/springframework/beans/factory/annotation/Configurable)) && AbstractDependencyInjectionAspect.ajc$if_0(joinpoint1))
            AnnotationBeanConfigurerAspect.aspectOf().ajc$afterReturning$org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect$2$1ea6722c(this);
    }

    public static String ajc$privFieldGet$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$name(Employee employee)
    {
        return employee.name;
    }

    public static void ajc$privFieldSet$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$name(Employee employee, String s)
    {
        employee.name = s;
    }

    public static Date ajc$privFieldGet$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$birth(Employee employee)
    {
        return employee.birth;
    }

    public static void ajc$privFieldSet$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$birth(Employee employee, Date date)
    {
        employee.birth = date;
    }

    public static long countEmployees()
    {
        return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$countEmployees();
    }

    public static EntityManager entityManager()
    {
        return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$entityManager();
    }

    public static List findAllEmployees()
    {
        return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$findAllEmployees();
    }

    public static Employee findEmployee(Long long1)
    {
        return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$findEmployee(long1);
    }

    public static List findEmployeeEntries(int i, int j)
    {
        return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$findEmployeeEntries(i, j);
    }

    public void flush()
    {
Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$flush(this);
    }

    public Date getBirth()
    {
        return Employee_Roo_JavaBean.ajc$interMethod$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$getBirth(this);
    }

    public Long getId()
    {
        return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$getId(this);
    }

    public String getName()
    {
        return Employee_Roo_JavaBean.ajc$interMethod$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$getName(this);
    }

    public Integer getVersion()
    {
        return Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$getVersion(this);
    }

    public void merge()
    {   Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$merge(this);
    }

    public void persist()
    {     Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$persist(this);
    }

    public void remove()
    {
        Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$remove(this);
    }

    public void setBirth(Date date)
    {
        Employee_Roo_JavaBean.ajc$interMethod$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$setBirth(this, date);
    }

    public void setId(Long long1)
    {
        Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$setId(this, long1);
    }

    public void setName(String s)
    {
        Employee_Roo_JavaBean.ajc$interMethod$com_javaeye_domain_Employee_Roo_JavaBean$com_javaeye_domain_Employee$setName(this, s);
    }

    public void setVersion(Integer integer)
    {
        Employee_Roo_Entity.ajc$interMethod$com_javaeye_domain_Employee_Roo_Entity$com_javaeye_domain_Employee$setVersion(this, integer);
    }

    public String toString()
    {
        return Employee_Roo_ToString.ajc$interMethod$com_javaeye_domain_Employee_Roo_ToString$com_javaeye_domain_Employee$toString(this);
    }

    private String name;
    private Date birth;
    public transient EntityManager ajc$interField$com_javaeye_domain$entityManager;
    public Long ajc$interField$com_javaeye_domain_Employee_Roo_Entity$id;
    public Integer ajc$interField$com_javaeye_domain_Employee_Roo_Entity$version;
    private static final org.aspectj.lang.JoinPoint.StaticPart ajc$tjp_0; /* synthetic field */
    private static final org.aspectj.lang.JoinPoint.StaticPart ajc$tjp_1; /* synthetic field */

    static 
    {
        Factory factory = new Factory("Employee.java", Class.forName("com.javaeye.domain.Employee"));
        ajc$tjp_0 = factory.makeSJP("initialization", factory.makeConstructorSig("1", "org.springframework.beans.factory.aspectj.ConfigurableObject", "", "", ""), 17);
        ajc$tjp_1 = factory.makeSJP("initialization", factory.makeConstructorSig("1", "com.javaeye.domain.Employee", "", "", ""), 17);
    }
}



经过反编译生成的class发现,通过domain类上面的roo注释,自动在类编译时加入get set,甚至我们发现了findAllEmployees,persist,remove,哈,这不是我们梦寐以求的充血模型吗?

原来,roo在为我们创建domain的时候自动为同一个domain创建了4个aspect(.aj files),并在编译期动态为domain切入代码.
4个aspect分别是:
domainName_Roo_Configurable.aj(配置)
domainName_Roo_Entity.aj(加入orm持久)
domainName_Roo_JavaBean.aj(为字段生成get set)
domainName_Roo_ToString.aj(重写toString)


可见,使用roo我们彻底摆脱了在daomain里对各个属性写get set,并且使domain摇身一变为充血。

下一步,就是建Controller了,同样是一条语句

Roo shell代码 复制代码
  1. new controller automatic -formBackingObject ~.domain.Employee -name ~.web.CommentController  
new controller automatic -formBackingObject ~.domain.Employee -name ~.web.CommentController



你会发现,EmployeeController,spring mvc的配置以及curd的jsp页面全部生成了

EmployeeController代码如下:

Java代码 复制代码
  1. @RooWebScaffold(automaticallyMaintainView = true, formBackingObject = Employee.class)   
  2. @RequestMapping("/employee/**")   
  3. @Controller  
  4. public class EmployeeController {   
  5. }  
@RooWebScaffold(automaticallyMaintainView = true, formBackingObject = Employee.class)
@RequestMapping("/employee/**")
@Controller
public class EmployeeController {
}



在这里出现了一行@RooWebScaffold注解,做过rails的人会想,这不是rails里面的scaffold吗?没错,通过@RooWebScaffold注解,EmployeeController自动获得了curd的所有功能。

同样是生成controllerName_Roo_Controller.aj在编译期织入代码

我们来看看生成的EmployeeController_Roo_Controller.aj:

Java代码 复制代码
  1. package com.javaeye.web;   
  2.   
  3. privileged aspect EmployeeController_Roo_Controller {   
  4.        
  5.     @org.springframework.web.bind.annotation.RequestMapping(value = "/employee", method = org.springframework.web.bind.annotation.RequestMethod.POST)       
  6.     public java.lang.String EmployeeController.create(@org.springframework.web.bind.annotation.ModelAttribute("employee") com.javaeye.domain.Employee employee, org.springframework.validation.BindingResult result) {       
  7.         if (employee == nullthrow new IllegalArgumentException("A employee is required");           
  8.         for(javax.validation.ConstraintViolation<com.javaeye.domain.Employee> constraint : javax.validation.Validation.buildDefaultValidatorFactory().getValidator().validate(employee)) {           
  9.             result.rejectValue(constraint.getPropertyPath(), null, constraint.getMessage());               
  10.         }           
  11.         if (result.hasErrors()) {           
  12.             return "employee/create";               
  13.         }           
  14.         employee.persist();           
  15.         return "redirect:/employee/" + employee.getId();           
  16.     }       
  17.        
  18.     @org.springframework.web.bind.annotation.RequestMapping(value = "/employee/form", method = org.springframework.web.bind.annotation.RequestMethod.GET)       
  19.     public java.lang.String EmployeeController.createForm(org.springframework.ui.ModelMap modelMap) {       
  20.         modelMap.addAttribute("employee"new com.javaeye.domain.Employee());           
  21.         return "employee/create";           
  22.     }       
  23.        
  24.     @org.springframework.web.bind.annotation.RequestMapping(value = "/employee/{id}", method = org.springframework.web.bind.annotation.RequestMethod.GET)       
  25.     public java.lang.String EmployeeController.show(@org.springframework.web.bind.annotation.PathVariable("id") java.lang.Long id, org.springframework.ui.ModelMap modelMap) {       
  26.         if (id == nullthrow new IllegalArgumentException("An Identifier is required");           
  27.         modelMap.addAttribute("employee", com.javaeye.domain.Employee.findEmployee(id));           
  28.         return "employee/show";           
  29.     }       
  30.        
  31.     @org.springframework.web.bind.annotation.RequestMapping(value = "/employee", method = org.springframework.web.bind.annotation.RequestMethod.GET)       
  32.     public java.lang.String EmployeeController.list(org.springframework.ui.ModelMap modelMap) {       
  33.         modelMap.addAttribute("employees", com.javaeye.domain.Employee.findAllEmployees());           
  34.         return "employee/list";           
  35.     }       
  36.        
  37.     @org.springframework.web.bind.annotation.RequestMapping(method = org.springframework.web.bind.annotation.RequestMethod.PUT)       
  38.     public java.lang.String EmployeeController.update(@org.springframework.web.bind.annotation.ModelAttribute("employee") com.javaeye.domain.Employee employee, org.springframework.validation.BindingResult result) {       
  39.         if (employee == nullthrow new IllegalArgumentException("A employee is required");           
  40.         for(javax.validation.ConstraintViolation<com.javaeye.domain.Employee> constraint : javax.validation.Validation.buildDefaultValidatorFactory().getValidator().validate(employee)) {           
  41.             result.rejectValue(constraint.getPropertyPath(), null, constraint.getMessage());               
  42.         }           
  43.         if (result.hasErrors()) {           
  44.             return "employee/update";               
  45.         }           
  46.         employee.merge();           
  47.         return "redirect:/employee/" + employee.getId();           
  48.     }       
  49.        
  50.     @org.springframework.web.bind.annotation.RequestMapping(value = "/employee/{id}/form", method = org.springframework.web.bind.annotation.RequestMethod.GET)       
  51.     public java.lang.String EmployeeController.updateForm(@org.springframework.web.bind.annotation.PathVariable("id") java.lang.Long id, org.springframework.ui.ModelMap modelMap) {       
  52.         if (id == nullthrow new IllegalArgumentException("An Identifier is required");           
  53.         modelMap.addAttribute("employee", com.javaeye.domain.Employee.findEmployee(id));           
  54.         return "employee/update";           
  55.     }       
  56.        
  57.     @org.springframework.web.bind.annotation.RequestMapping(value = "/employee/{id}", method = org.springframework.web.bind.annotation.RequestMethod.DELETE)       
  58.     public java.lang.String EmployeeController.delete(@org.springframework.web.bind.annotation.PathVariable("id") java.lang.Long id) {       
  59.         if (id == nullthrow new IllegalArgumentException("An Identifier is required");           
  60.         com.javaeye.domain.Employee.findEmployee(id).remove();           
  61.         return "redirect:/employee";           
  62.     }       
  63.        
  64.     @org.springframework.web.bind.annotation.InitBinder       
  65.     public void EmployeeController.initBinder(org.springframework.web.bind.WebDataBinder binder) {       
  66.         binder.registerCustomEditor(java.util.Date.classnew org.springframework.beans.propertyeditors.CustomDateEditor(new java.text.SimpleDateFormat("yyyy-M-d"), false));           
  67.     }       
  68.        
  69. }  
package com.javaeye.web;

privileged aspect EmployeeController_Roo_Controller {
    
    @org.springframework.web.bind.annotation.RequestMapping(value = "/employee", method = org.springframework.web.bind.annotation.RequestMethod.POST)    
    public java.lang.String EmployeeController.create(@org.springframework.web.bind.annotation.ModelAttribute("employee") com.javaeye.domain.Employee employee, org.springframework.validation.BindingResult result) {    
        if (employee == null) throw new IllegalArgumentException("A employee is required");        
        for(javax.validation.ConstraintViolation<com.javaeye.domain.Employee> constraint : javax.validation.Validation.buildDefaultValidatorFactory().getValidator().validate(employee)) {        
            result.rejectValue(constraint.getPropertyPath(), null, constraint.getMessage());            
        }        
        if (result.hasErrors()) {        
            return "employee/create";            
        }        
        employee.persist();        
        return "redirect:/employee/" + employee.getId();        
    }    
    
    @org.springframework.web.bind.annotation.RequestMapping(value = "/employee/form", method = org.springframework.web.bind.annotation.RequestMethod.GET)    
    public java.lang.String EmployeeController.createForm(org.springframework.ui.ModelMap modelMap) {    
        modelMap.addAttribute("employee", new com.javaeye.domain.Employee());        
        return "employee/create";        
    }    
    
    @org.springframework.web.bind.annotation.RequestMapping(value = "/employee/{id}", method = org.springframework.web.bind.annotation.RequestMethod.GET)    
    public java.lang.String EmployeeController.show(@org.springframework.web.bind.annotation.PathVariable("id") java.lang.Long id, org.springframework.ui.ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("employee", com.javaeye.domain.Employee.findEmployee(id));        
        return "employee/show";        
    }    
    
    @org.springframework.web.bind.annotation.RequestMapping(value = "/employee", method = org.springframework.web.bind.annotation.RequestMethod.GET)    
    public java.lang.String EmployeeController.list(org.springframework.ui.ModelMap modelMap) {    
        modelMap.addAttribute("employees", com.javaeye.domain.Employee.findAllEmployees());        
        return "employee/list";        
    }    
    
    @org.springframework.web.bind.annotation.RequestMapping(method = org.springframework.web.bind.annotation.RequestMethod.PUT)    
    public java.lang.String EmployeeController.update(@org.springframework.web.bind.annotation.ModelAttribute("employee") com.javaeye.domain.Employee employee, org.springframework.validation.BindingResult result) {    
        if (employee == null) throw new IllegalArgumentException("A employee is required");        
        for(javax.validation.ConstraintViolation<com.javaeye.domain.Employee> constraint : javax.validation.Validation.buildDefaultValidatorFactory().getValidator().validate(employee)) {        
            result.rejectValue(constraint.getPropertyPath(), null, constraint.getMessage());            
        }        
        if (result.hasErrors()) {        
            return "employee/update";            
        }        
        employee.merge();        
        return "redirect:/employee/" + employee.getId();        
    }    
    
    @org.springframework.web.bind.annotation.RequestMapping(value = "/employee/{id}/form", method = org.springframework.web.bind.annotation.RequestMethod.GET)    
    public java.lang.String EmployeeController.updateForm(@org.springframework.web.bind.annotation.PathVariable("id") java.lang.Long id, org.springframework.ui.ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("employee", com.javaeye.domain.Employee.findEmployee(id));        
        return "employee/update";        
    }    
    
    @org.springframework.web.bind.annotation.RequestMapping(value = "/employee/{id}", method = org.springframework.web.bind.annotation.RequestMethod.DELETE)    
    public java.lang.String EmployeeController.delete(@org.springframework.web.bind.annotation.PathVariable("id") java.lang.Long id) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        com.javaeye.domain.Employee.findEmployee(id).remove();        
        return "redirect:/employee";        
    }    
    
    @org.springframework.web.bind.annotation.InitBinder    
    public void EmployeeController.initBinder(org.springframework.web.bind.WebDataBinder binder) {    
        binder.registerCustomEditor(java.util.Date.class, new org.springframework.beans.propertyeditors.CustomDateEditor(new java.text.SimpleDateFormat("yyyy-M-d"), false));        
    }    
    
}


这样,Controller里不用写一行代码,就自动拥有了curd,而且,最新的roo使用了spring3.0,还支持了rest

分享到:
评论
1 楼 snowolf 2010-08-25  
注解时代~

相关推荐

    Spring Roo 简介,第 4 部分: 用 Spring Roo 和 Cloud Foundry 在云中快速开发应用程序

    Spring Roo 是一个基于 Java 的开源工具,它简化了 Spring 应用程序的构建过程,通过自动化任务和代码生成,让开发者能够快速地搭建起可扩展的、基于最佳实践的项目结构。 **Spring Roo 的核心特点** 1. **代码生成...

    spring roo使用文档

    - **提高生产力**:通过自动化生成常见的代码结构,如实体类、控制器等,可以显著提高开发效率。 - **遵循标准 Java 实践**:生成的代码遵循良好的编程习惯和标准,易于维护。 - **易于学习和使用**:提供直观的...

    spring roo 1.1.3 学习资料

    5. **持续集成**:Roo还支持与持续集成工具如Maven和Gradle的集成,方便开发者进行构建、测试和部署流程的自动化。 6. **IDE集成**:Spring Roo可以与Eclipse、IntelliJ IDEA等流行IDE无缝集成,提供便捷的代码补全...

    Spring ROO

    总的来说,Spring ROO是Spring生态体系中的一个重要组成部分,它通过自动化工具和最佳实践,极大地提升了开发效率,降低了企业级应用的开发复杂性。掌握Spring ROO的使用,对于提高开发者的生产力和项目进度具有显著...

    spring-roo-1.3.2.zip

    总结来说,Spring Roo 1.3.2是Spring框架下的一款强大的开发工具,旨在通过自动化和代码生成加速Java应用的开发流程,同时保持与Spring生态系统的紧密集成,让开发者能更专注于业务逻辑的实现,而非基础设施的搭建。

    Spring Roo命令文档

    Roo通过自动化过程,简化了常见的开发任务,如设置项目结构、创建实体类、生成数据库表映射以及创建CRUD操作。本文将深入探讨Spring Roo的核心命令及其在开发过程中的作用。 1. **初始化项目** Spring Roo可以通过...

    springroo快速学习

    SpringRoo是Spring框架的扩展,它使用命令行界面(CLI)或集成开发环境(IDE)插件来提供代码生成和自动化功能。它的目标是简化Java应用程序的构建过程,特别适合于Spring Boot应用的早期开发阶段。 二、安装与设置...

    spring-roo-1.1.5.RELEAS

    Spring Roo是Spring框架家族中的一个开发工具,它旨在加速Java应用程序的开发过程,特别是通过提供命令行接口和集成开发环境(IDE)插件来简化常见的编程任务。标题"spring-roo-1.1.5.RELEASE"指的是Spring Roo的一...

    SpringRoo 官方文档-版本 2.0.0.RC1

    SpringRoo 通过提供一系列自动化代码生成功能,允许开发者在几分钟内创建出完整的企业级应用程序。 **SpringRoo 版本 2.0.0.RC1** 是一个重要的里程碑,标志着 SpringRoo 向更加成熟和强大的方向发展。这一版本在多...

    spring roo 生成数据库表

    在这个场景中,"spring roo 生成数据库表"指的是使用Spring Roo工具来自动化创建与数据库表对应的Java实体类和数据访问层。 首先,Spring Roo支持多种数据库,包括MySQL、Oracle、PostgreSQL等。在开始之前,你需要...

    spring-roo-2.0.0.RC1.zip

    Spring Roo的核心特性包括自动化代码生成、强大的元数据驱动开发、以及对最佳实践的集成。它通过命令行界面或集成开发环境(IDE)插件提供服务,帮助开发者快速创建项目的结构、实体、控制器和服务层代码。Spring ...

    os-springroo2-sample_code

    - 自动化构建工具如Maven或Gradle的集成。 - 如何根据需求自定义Roo生成的代码。 总的来说,【os-springroo2-sample_code】项目是一个宝贵的教育资源,适合想要深入了解和使用Spring Roo 2的开发者,他们可以通过此...

    spring-roo-1.1.0.M1.zip_54587.m1_M1 ssh_Spring Roo download_spri

    Spring Roo是Spring Framework的一个附加工具,它为Java开发者提供了一...这个压缩包提供的资源正是为了帮助开发者快速启动基于Spring的项目,利用Spring Roo的自动化功能和SSH的安全特性来构建高效且安全的Java应用。

    Spring Roo - Reference Documentation

    Spring Roo提供的不仅仅是自动化工具,还包括了一整套详尽的教程,指导开发者从项目创建、实体设计、集成测试到Web层构建的全过程。通过跟随官方教程,即使是新手也能逐步掌握Roo的使用技巧,最终独立完成复杂的应用...

    spring-roo-docs

    - 开发效率降低:需要手动编写原本由SpringRoo自动生成的代码。 - 维护成本增加:缺少SpringRoo提供的自动化功能。 **6.3 移除步骤** 移除SpringRoo的步骤包括: - **第一步:推入重构**:将SpringRoo生成的代码...

    spring roo action

    - **支持持续集成**:与 Maven 和 Ant 等构建工具集成,方便进行自动化构建和部署。 - **丰富的插件生态系统**:Spring Roo 提供了一系列的插件,这些插件可以进一步扩展其功能,满足不同场景的需求。 #### Spring ...

    Spring Roo 简介

    - **快速开发**:Spring Roo 通过自动化大部分常规任务来加速应用程序开发过程,例如创建实体、服务层、控制器以及视图。 - **标准遵循**:生成的应用程序遵循 Spring 最佳实践,使用 Bean Validation (JSR-303) 和 ...

    vaadin-springRoo可运行的例子

    这个"vaadin-springRoo可运行的例子"是一个整合了这两个框架的实际项目,提供了完整的war包和源代码,使得开发者可以深入学习和理解如何在实际开发中结合Vaadin和Spring Roo。 Vaadin是一个开源的Java框架,它允许...

    Spring_Roo_in_Action_英文版

    Spring Roo是一个开源工具,它采用基于命令行的交互方式,旨在加速Java应用的开发过程,通过自动化常见任务,使得开发者可以更专注于业务逻辑。这本书详细介绍了Spring Roo的各个方面,包括它的核心概念、工作原理...

Global site tag (gtag.js) - Google Analytics