You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by jo...@apache.org on 2015/05/01 13:00:19 UTC

[3/4] struts git commit: Refactor angularjs maven archetype to have a separate namespace for data action and DataService with promises

Refactor angularjs maven archetype to have a separate namespace for data action and DataService with promises


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/e3682f94
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/e3682f94
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/e3682f94

Branch: refs/heads/develop
Commit: e3682f94726015a6e97873b4e21cad2cf20b6eb6
Parents: e990fbb
Author: Johannes Geppert <jo...@gmail.com>
Authored: Fri May 1 12:48:09 2015 +0200
Committer: Johannes Geppert <jo...@gmail.com>
Committed: Fri May 1 12:48:09 2015 +0200

----------------------------------------------------------------------
 .../src/main/java/actions/ProjectsAction.java   | 67 ------------------
 .../main/java/actions/data/ProjectsAction.java  | 74 ++++++++++++++++++++
 .../src/main/resources/struts.xml               |  1 +
 .../src/main/webapp/WEB-INF/content/hello.jsp   |  1 +
 .../src/main/webapp/js/controllers.js           | 14 ++--
 .../src/main/webapp/js/services.js              | 57 +++++++++++++++
 6 files changed, 139 insertions(+), 75 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/e3682f94/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/java/actions/ProjectsAction.java
----------------------------------------------------------------------
diff --git a/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/java/actions/ProjectsAction.java b/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/java/actions/ProjectsAction.java
deleted file mode 100644
index 77c9497..0000000
--- a/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/java/actions/ProjectsAction.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * $Id$
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package ${package}.actions;
-
-import com.opensymphony.xwork2.ActionSupport;
-import org.apache.struts2.convention.annotation.Result;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * <code>List Apache projects.</code>
- */
-@Result(type = "json")
-public class ProjectsAction extends ActionSupport {
-
-    private static final long serialVersionUID = 9037336532369476225L;
-
-    private List<String> projectNames = new ArrayList<String>();
-
-    public String execute() throws Exception {
-
-        projectNames.add("Apache Struts");
-        projectNames.add("Apache Log4j");
-        projectNames.add("Apache Tomcat");
-        projectNames.add("Apache Maven");
-        projectNames.add("Apache Ant");
-        projectNames.add("Apache Log4Net");
-        projectNames.add("Apache Log4Cxx");
-        projectNames.add("Apache Chainsaw");
-        projectNames.add("Apache Incubator");
-        projectNames.add("Apache Hadoop");
-        projectNames.add("Apache OpenOffice");
-        projectNames.add("Apache Mahout");
-        projectNames.add("Apache Tapestry");
-        projectNames.add("Apache Jena");
-        projectNames.add("Apache Solr");
-        projectNames.add("Apache Cayenne");
-        projectNames.add("Apache OpenEJB");
-        projectNames.add("Apache Deltaspike");
-        projectNames.add("Apache Cordova");
-
-        return SUCCESS;
-    }
-
-    public List<String> getProjectNames() {
-        return projectNames;
-    }
-}

http://git-wip-us.apache.org/repos/asf/struts/blob/e3682f94/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/java/actions/data/ProjectsAction.java
----------------------------------------------------------------------
diff --git a/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/java/actions/data/ProjectsAction.java b/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/java/actions/data/ProjectsAction.java
new file mode 100644
index 0000000..ddbe7da
--- /dev/null
+++ b/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/java/actions/data/ProjectsAction.java
@@ -0,0 +1,74 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package ${package}.actions.data;
+
+import com.opensymphony.xwork2.ActionSupport;
+import org.apache.struts2.convention.annotation.Result;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * <code>List Apache projects.</code>
+ */
+@Result(type = "json")
+public class ProjectsAction extends ActionSupport {
+
+    private static final long serialVersionUID = 9037336532369476225L;
+    private static final Logger log = LogManager.getLogger(ProjectsAction.class);
+
+    private List<String> projectNames;
+
+    public String execute() throws Exception {
+
+        projectNames = new ArrayList<String>();
+        projectNames.add("Apache Struts");
+        projectNames.add("Apache Log4j");
+        projectNames.add("Apache Tomcat");
+        projectNames.add("Apache Maven");
+        projectNames.add("Apache Ant");
+        projectNames.add("Apache Log4Net");
+        projectNames.add("Apache Log4Cxx");
+        projectNames.add("Apache Chainsaw");
+        projectNames.add("Apache Incubator");
+        projectNames.add("Apache Hadoop");
+        projectNames.add("Apache OpenOffice");
+        projectNames.add("Apache Mahout");
+        projectNames.add("Apache Tapestry");
+        projectNames.add("Apache Jena");
+        projectNames.add("Apache Solr");
+        projectNames.add("Apache Cayenne");
+        projectNames.add("Apache OpenEJB");
+        projectNames.add("Apache Deltaspike");
+        projectNames.add("Apache Cordova");
+
+        log.debug("Return {} Apache projects", projectNames.size());
+
+        return SUCCESS;
+    }
+
+    public List<String> getProjectNames() {
+        return projectNames;
+    }
+}

http://git-wip-us.apache.org/repos/asf/struts/blob/e3682f94/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/resources/struts.xml
----------------------------------------------------------------------
diff --git a/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/resources/struts.xml b/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/resources/struts.xml
index 5da77c9..e725380 100644
--- a/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/resources/struts.xml
+++ b/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/resources/struts.xml
@@ -9,5 +9,6 @@
 
   <constant name="struts.convention.default.parent.package" value="angularstruts"/>
   <package name="angularstruts" extends="json-default"></package>
+  <package name="data" extends="angularstruts" namespace="/data"></package>
 
 </struts>

http://git-wip-us.apache.org/repos/asf/struts/blob/e3682f94/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/content/hello.jsp
----------------------------------------------------------------------
diff --git a/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/content/hello.jsp b/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/content/hello.jsp
index 70cdb14..703353e 100644
--- a/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/content/hello.jsp
+++ b/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/webapp/WEB-INF/content/hello.jsp
@@ -22,6 +22,7 @@
 <script src="<s:url value="js/lib/angular/angular-route.min.js" />"></script>
 <script src="<s:url value="js/bootstrap.js" />"></script>
 <script src="<s:url value="js/directives.js" />"></script>
+<script src="<s:url value="js/services.js" />"></script>
 <script src="<s:url value="js/controllers.js" />"></script>
 </body>
 </html>

http://git-wip-us.apache.org/repos/asf/struts/blob/e3682f94/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/webapp/js/controllers.js
----------------------------------------------------------------------
diff --git a/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/webapp/js/controllers.js b/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/webapp/js/controllers.js
index 9a5b71f..ed57b00 100644
--- a/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/webapp/js/controllers.js
+++ b/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/webapp/js/controllers.js
@@ -24,15 +24,13 @@ angularStrutsApp.controller('HomeController', function ($scope) {
     $scope.name = "Sunshine";
 });
 
-angularStrutsApp.controller('ApacheProjectsController', function ($scope, $http) {
+angularStrutsApp.controller('ApacheProjectsController', function ($scope, $http, DataService) {
     this.init = function() {
-        $http({method: 'GET', url: 'projects'}).
-            success(function(data) {
-                $scope.projects = data.projectNames;
-            }).
-            error(function(data, status, headers, config) {
-                alert("Could not receive project names");
-            });
+        DataService.getProjects().then(function(data) {
+            $scope.projects = data.data.projectNames;
+        }, function(data) {
+            console.log('Could not receive project names.')
+        });
     };
 
     this.init();

http://git-wip-us.apache.org/repos/asf/struts/blob/e3682f94/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/webapp/js/services.js
----------------------------------------------------------------------
diff --git a/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/webapp/js/services.js b/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/webapp/js/services.js
new file mode 100644
index 0000000..e3be1de
--- /dev/null
+++ b/archetypes/struts2-archetype-angularjs/src/main/resources/archetype-resources/src/main/webapp/js/services.js
@@ -0,0 +1,57 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+angularStrutsApp.factory('DataService', ['$http', '$q', function($http, $q) {
+
+    var DataService = {
+        urls : {
+            projects : "data/projects"
+        }
+    };
+
+    DataService._request = function(url, method, model){
+        if(!method) {
+            method = 'GET';
+        }
+        var def = $q.defer();
+        if(method === 'GET') {
+            return $http.get(url).success(function(data) {
+                DataService.data = data;
+                def.resolve(data);
+            }).error(function() {
+                def.reject("Failed to get data");
+            });
+        } else if(method === 'POST'){
+            $http.post(url, model).success(function(data) {
+                DataService.data = data;
+                def.resolve(data);
+            }).error(function() {
+                def.reject("Failed to post data");
+            });
+        }
+        return def.promise;
+    };
+
+    DataService.getProjects = function () {
+        return this._request(this.urls.projects);
+    };
+
+    return DataService;
+}]);
\ No newline at end of file