Friday, 5 December 2014

Struts2 Restful webservices


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

  <constant name="struts.devMode" value="true" />
  <constant name="struts.convention.action.suffix" value="Controller"/>
  <constant name="struts.convention.action.mapAllMatches" value="true"/>
  <constant name="struts.convention.default.parent.package" value="rest-default"/>
  <constant name="struts.convention.package.locators" value="example"/>

  <constant name="struts.convention.default.parent.package" value="rest-default"/>  

</struts>

OrdersController.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package org.lkr.struts2.rest.example;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import org.lkr.struts2.rest.service.OrdersService;
import org.lkr.struts2.rest.to.Order;

import com.opensymphony.xwork2.ModelDriven;

@Results({ @Result(name = "success", type = "redirectAction", params = {
  "actionName", "orders" }) })
public class OrdersController implements ModelDriven<Object> {

 /**
  * 
  */
 private static final long serialVersionUID = 3241256322152936000L;
 private Order model = new Order();
 private String id;
 private Collection<Order> list;
 private OrdersService ordersService = new OrdersService();

 // GET /orders/1
 public HttpHeaders show() {
  return new DefaultHttpHeaders("show");
 }

 // GET /orders
 public HttpHeaders index() {
  list = ordersService.getAll();
  return new DefaultHttpHeaders("index").disableCaching();
 }

 // GET /orders/1/edit
 public String edit() {
  return "edit";
 }

 // GET /orders/new
 public String editNew() {
  model = new Order();
  return "editNew";
 }

 // GET /orders/1/deleteConfirm
 public String deleteConfirm() {
  return "deleteConfirm";
 }

 // DELETE /orders/1
 public String destroy() {
  ordersService.remove(id);
  //addActionMessage("Order removed successfully");
  return "success";
 }

 // POST /orders
 public HttpHeaders create() {
  ordersService.save(model);
  //addActionMessage("New order created successfully");
  return new DefaultHttpHeaders("success").setLocationId(model.getId());
 }

 // PUT /orders/1
 public String update() {
  ordersService.save(model);
  //addActionMessage("Order updated successfully");
  return "success";
 }

 public void validate() {
  if (model.getClientName() == null
    || model.getClientName().length() == 0) {
   //addFieldError("clientName", "The client name is empty");
  }
 }

 public void setId(String id) {
  if (id != null) {
   this.model = ordersService.get(id);
  }
  this.id = id;
 }

 public Object getModel() {
  return (list != null ? list : model);
 }

}


orders-index.jsp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
 <title>Orders</title>
</head>
<body>
    <s:actionmessage />
    <table>
        <tr>
            <th>ID</th>
            <th>Client</th>
            <th>Amount</th>
            <th>Actions</th>
        </tr>
        <s:iterator value="model">
        <tr>
            <td>${id}</td>
            <td>${clientName}</td>
            <td>${amount}</td>
            <td><a href="orders/${id}">View</a> |
                <a href="orders/${id}/edit">Edit</a> |
                <a href="orders/${id}/deleteConfirm">Delete</a></td>
        </tr>
        </s:iterator>
    </table>     
    <a href="orders/new">Create a new order</a>
</body>
</html>


web.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <welcome-file-list>
  <!-- <welcome-file>index.jsp</welcome-file> -->
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

No comments:

Post a Comment