[1].[代码] web.xml
01 <servlet>
02 <servlet-name>RestServlet</servlet-name>
03 <servlet-class>net.sf.serfj.RestServlet</servlet-class>
04 <load-on-startup>5</load-on-startup>
05 </servlet>
06
07 <servlet-mapping>
08 <servlet-name>RestServlet</servlet-name>
09 <url-pattern>/banks/*</url-pattern>
10 </servlet-mapping>
11
12 <servlet-mapping>
13 <servlet-name>RestServlet</servlet-name>
14 <url-pattern>/accounts/*</url-pattern>
15 </servlet-mapping>
[2].[代码] serfj.properties
1 # Main package where looking for classes (controllers, serializers)
2 main.package=net.sf.serfj.test
[3].[代码] Bank.java
01 public class Bank extends RestController {
02 @GET
03 public void index() {
04 // By default, this action redirects to index.jsp (or index.html or index.htm)
05 }
06
07 @GET
08 public void show() {
09 // Gets ID from URL /banks/1
10 String id = this.getId("bank");
11
12 // Gets account's ID from URL /banks/1/accounts/2
13 String accountId = this.getId("account");
14
15 // Gets the account
16 Account account = // Code that gets the account 2 from bank 1
17
18 // Put account into the request so the page will be able to use it
19 this.addObject2Request("account", account);
20
21 // By default, this action redirects to show.jsp (or show.html or show.htm)
22 }
23
24 @GET
25 public void newResource() {
26 // By default, this action redirects to new.jsp (or new.html or new.htm)
27 }
28
29 @GET
30 public void edit() {
31 // By default, this action redirects to edit.jsp (or edit.html or edit.htm)
32 }
33
34 @POST
35 public void create() {
36 // By default, this action redirects to create.jsp (or create.html or create.htm)
37 }
38
39 @PUT
40 public void update() {
41 // Gets bank's ID
42 String id = this.getId("bank");
43
44 Bank bank = // Code that gets the bank object
45
46 // Gets new name for the bank
47 String name = this.getStringParam("name");
48
49 // Updating the bank
50 // ... Code that updates the bank's information
51
52 // By default, this action redirects to update.jsp (or update.html or update.htm)
53 }
54
55 @DELETE
56 public void delete() {
57 // By default, this action redirects to delete.jsp (or delete.html or delete.htm)
58 }
59
60 @GET
61 public void someAction() {
62 // By default, this action redirects to someAction.jsp (or someAction.html or someAction.htm)
63 }
64 }
[4].[代码] Account.java
01 public class Account extends RestController {
02 @GET
03 public void index() {
04 // By default, this action redirects to index.jsp (or index.html or index.htm)
05 }
06
07 @GET
08 public void show() {
09 // By default, this action redirects to show.jsp (or show.html or show.htm)
10 }
11
12 @GET
13 public void newResource() {
14 // By default, this action redirects to new.jsp (or new.html or new.htm)
15 }
16
17 @GET
18 public void edit() {
19 // By default, this action redirects to edit.jsp (or edit.html or edit.htm)
20 }
21
22 @POST
23 public void create() {
24 // By default, this action redirects to create.jsp (or create.html or create.htm)
25 // But I want to render another page!... easy
26 this.renderPage("mypage.jsp");
27 }
28
29 @PUT
30 public void update() {
31 // By default, this action redirects to update.jsp (or update.html or update.htm)
32 // But I want to render another page... from another controller!... easy
33 this.renderPage("bank", "another_page.jsp");
34 }
35
36 @DELETE
37 public void delete() {
38 // By default, this action redirects to delete.jsp (or delete.html or delete.htm)
39 // Well, if something happens, I want to redirect to mypage.jsp
40 if (somethingHappens) {
41 this.renderPage("mypage.jsp");
42 } else {
43 // Default page
44 this.renderPage();
45 }
46 }
47
48 /**
49 * If this method is called as /accounts/1/accountBalance.xml, then the balance object will
50 * be serialized as an XML, whereas if it's called as /accounts/1/accountBalance.json, the
51 * object will be serialized as a JSON object.
52 */
53 @POST
54 public Balance accountBalance() {
55 // Gets account's Id
56 String id = this.getId("account");
57
58 // Calculate balance
59 BalanceManager manager = new BalanceManager();
60 Balance balance = manager.getBalance(id);
61 this.serialize(balance);
62 }
63 }