Spring MVC3构建Web应用第五步
构建业务实体模型。在src目录中,新建包命名为paul.sydney.model,在这个包下,再新建一个实体类,命名为Person,修改代码如下:
1
package paul.sydney.model;
2
3
/**
4
* Copyright : adobocode.com , 2010
5
* @author Paul Sydney Orozco | xtrycatchx@gmail.com
6
*/
7
public class Person {
8
9
private int id;
10
private String name;
11
private int age;
12
private String address;
13
14
public int getId() {
15
return id;
16
}
17
public void setId(int id) {
18
this.id = id;
19
}
20
21
public String getName() {
22
return name;
23
}
24
public void setName(String name) {
25
this.name = name;
26
}
27
public int getAge() {
28
return age;
29
}
30
public void setAge(int age) {
31
this.age = age;
32
}
33
public String getAddress() {
34
return address;
35
}
36
public void setAddress(String address) {
37
this.address = address;
38
}
39
40
@Override
41
public String toString(){
42
StringBuilder sb = new StringBuilder();
43
sb.append("\nname : " + this.name);
44
sb.append("\nage : " + this.age);
45
sb.append("\naddress : " + this.address);
46
return sb.toString();
47
48
}
49
50
}
package paul.sydney.model; 2
3
/** 4
* Copyright : adobocode.com , 2010 5
* @author Paul Sydney Orozco | xtrycatchx@gmail.com 6
*/7
public class Person { 8
9
private int id; 10
private String name; 11
private int age; 12
private String address; 13
14
public int getId() { 15
return id; 16
} 17
public void setId(int id) { 18
this.id = id; 19
} 20
21
public String getName() { 22
return name; 23
} 24
public void setName(String name) { 25
this.name = name; 26
} 27
public int getAge() { 28
return age; 29
} 30
public void setAge(int age) { 31
this.age = age; 32
} 33
public String getAddress() { 34
return address; 35
} 36
public void setAddress(String address) { 37
this.address = address; 38
} 39
40
@Override41
public String toString(){ 42
StringBuilder sb = new StringBuilder(); 43
sb.append("\nname : " + this.name); 44
sb.append("\nage : " + this.age); 45
sb.append("\naddress : " + this.address); 46
return sb.toString(); 47
48
} 49
50
}
构建业务逻辑层。同样在src目录下新建一个包,命名为paul.sydney.service,并且新建一个类DummyService,代码如下:
1
paul.sydney.service;
2
3
import java.util.ArrayList;
4
5
import paul.sydney.model.Person;
6
7
import java.util.List;
8
9
import org.springframework.stereotype.Service;
10
11
/**
12
* Copyright : adobocode.com , 2010
13
* @author Paul Sydney Orozco | xtrycatchx@gmail.com
14
*/
15
@Service
16
public class DummyService {
17
18
/**
19
* This method supposed to be returning a Collection of Person objects from a DAO layer
20
* For this tutorial, let us just hard-code this List of Person objects
21
*/
22
public List<Person> getDummyList() {
23
List<Person> list = new ArrayList<Person>();
24
Person p1 = new Person();
25
p1.setId(12345);
26
p1.setName("Paul");
27
p1.setAge(27);
28
p1.setAddress("Dalaguete, Cebu");
29
30
Person p2 = new Person();
31
p2.setId(54321);
32
p2.setName("Sydney");
33
p2.setAge(25);
34
p2.setAddress("Cebu City");
35
36
list.add(p1);
37
list.add(p2);
38
return list;
39
}
40
41
/**
42
* This method supposed to be returning Person object from a DAO layer
43
* For this tutorial, let us just hard-code the Person instance
44
*/
45
public Person retrievePerson(int id) {
46
Person person = new Person();
47
person.setId(56789);
48
person.setName("Nikki");
49
person.setAge(63);
50
person.setAddress("Dalaguete, Cebu");
51
return person;
52
}
53
54
/**
55
* This method supposed to be persisting the passed Person object
56
* For this tutorial, let us include the persisting DAO layer
57
* and assume the method successful saved or updated the Person object
58
*/
59
public void savePerson(Person person) {
60
System.out.println("\n\nSaving" + person);
61
}
62
}
paul.sydney.service; 2
3
import java.util.ArrayList; 4
5
import paul.sydney.model.Person; 6
7
import java.util.List; 8
9
import org.springframework.stereotype.Service; 10
11
/** 12
* Copyright : adobocode.com , 2010 13
* @author Paul Sydney Orozco | xtrycatchx@gmail.com 14
*/15
@Service16
public class DummyService { 17
18
/** 19
* This method supposed to be returning a Collection of Person objects from a DAO layer 20
* For this tutorial, let us just hard-code this List of Person objects 21
*/22
public List<Person> getDummyList() { 23
List<Person> list = new ArrayList<Person>(); 24
Person p1 = new Person(); 25
p1.setId(12345); 26
p1.setName("Paul"); 27
p1.setAge(27); 28
p1.setAddress("Dalaguete, Cebu"); 29
30
Person p2 = new Person(); 31
p2.setId(54321); 32
p2.setName("Sydney"); 33
p2.setAge(25); 34
p2.setAddress("Cebu City"); 35
36
list.add(p1); 37
list.add(p2); 38
return list; 39
} 40
41
/** 42
* This method supposed to be returning Person object from a DAO layer 43
* For this tutorial, let us just hard-code the Person instance 44
*/45
public Person retrievePerson(int id) { 46
Person person = new Person(); 47
person.setId(56789); 48
person.setName("Nikki"); 49
person.setAge(63); 50
person.setAddress("Dalaguete, Cebu"); 51
return person; 52
} 53
54
/** 55
* This method supposed to be persisting the passed Person object 56
* For this tutorial, let us include the persisting DAO layer 57
* and assume the method successful saved or updated the Person object 58
*/59
public void savePerson(Person person) { 60
System.out.println("\n\nSaving" + person); 61
} 62
}
在这里,只是简单的在一个List中存放了多个person对象而已。
最后,项目的结构如下图:

接着,我们右击ant.xml,选择Run As->Ant Build,如下图:
