blob: 0d31a19f27ebd23de97db3ddd960bd16407af1d4 [file] [log] [blame]
jsseidela3b65e42017-09-28 14:31:07 -04001Creating a new application
2==========================
Christopher Lott (cl778h)881d2192017-11-06 15:13:17 -05003
jsseidela3b65e42017-09-28 14:31:07 -04004Our tutorial application will consist of two parts:
5
61. Java code: We'll create a controller (in reality you might need to create many, depending on how complex your application is) that will handle many of our web requests. Most importantly, it will handle the top-level access request.
72. Web code: The web code consists of all the files that Tomcat serves when requests are made for our application. Files like HTML, CSS, JavaScript, images, and so on.
8
9If you're working with code command-line only (e.g. vim), you'll want to create symlinks to both parts of the code, :code:`java` and :code:`web`. While debugging, you'll frequently move between the two halves.
10
11Java code location
12------------------
13
14Our Java code will reside here:
15
16::
17
18 sdk/ecomp-sdk/epsdk-app-os/src/main/java/org/openecomp/myapp
19
20
21Create the :code:`myapp` directory.
22
23Web code location
24-----------------
25
26Our web code will reside here:
27
28::
29
30 sdk/ecomp-sdk/epsdk-app-os/src/main/webapp/app/fusion/scripts/myapp
31
32Create the :code:`myapp` directory.
33
34Inside this directory, we'll add our first subpage:
35
36::
37
38 sdk/ecomp-sdk/epsdk-app-os/src/main/webapp/app/fusion/scripts/myapp/myfirstpage
39
40Create the :code:`myfirstpage` directory.
41
42Spring controllers
43------------------
44
45We need a controller to handle the top-level access requests (e.g. :code:`localhost:8080/epsdk-app-os/somepage`). For every :code:`somepage` our app needs, we'll need to either designate an existing controller to handle the request or create a new one.
46
47Why would you need to create a new one?
48
49The controller is where you'll set up your database access, read from database tables, update database tables, and so on. It may make sense to use different controllers for different :code:`somepage` functions to increase readability and make things easier when others need to maintain the code.
50
51A basic controller
52~~~~~~~~~~~~~~~~~~
53
54Copy the following basic controller code into your Java code location as :code:`MyAppController.java`
55
56.. code-block:: java
57
kg811t3aa28e92018-02-08 13:25:08 -050058 package org.onap.myapp;
jsseidela3b65e42017-09-28 14:31:07 -040059
60 import java.util.HashMap;
61 import java.util.ArrayList;
62 import java.util.Map;
63 import java.util.List;
64 import java.io.IOException;
65
66 import java.beans.PropertyVetoException;
67
68 import org.springframework.stereotype.Controller;
69 import org.springframework.web.bind.annotation.RequestMapping;
70 import org.springframework.web.bind.annotation.RequestMethod;
71 import org.springframework.web.bind.annotation.PathVariable;
72 import org.springframework.stereotype.Repository;
73 import org.springframework.beans.factory.annotation.Autowired;
74 import org.springframework.web.servlet.ModelAndView;
75 import org.springframework.jdbc.core.JdbcTemplate;
76
77 import javax.servlet.http.HttpSession;
78 import javax.servlet.http.HttpServletRequest;
79 import javax.servlet.http.HttpServletResponse;
80 import javax.sql.DataSource;
81 import javax.annotation.PostConstruct;
82
83 import com.mchange.v2.c3p0.ComboPooledDataSource;
84
85 import org.json.JSONObject;
86 import org.json.JSONArray;
87
kg811t3aa28e92018-02-08 13:25:08 -050088 import org.onap.portalsdk.core.controller.RestrictedBaseController;
89 import org.onap.portalsdk.core.util.SystemProperties;
90 import org.onap.portalsdk.core.domain.User;
jsseidela3b65e42017-09-28 14:31:07 -040091
92 @Controller
93 @Repository
94 @RequestMapping("/")
95 public class MyAppController extends RestrictedBaseController {
96 ///////////////////////////////////////////////////////////////////////////////
97 // Constructor
98 //
99 public MyAppController() {
100 super();
101 }
102
103 ///////////////////////////////////////////////////////////////////////////////
104 // Handle 'myfirstpage' requests
105 //
106 @RequestMapping(value = { "/myfirstpage" }, method = RequestMethod.GET)
107 public ModelAndView myfirstpage(HttpServletRequest request) {
108 final String defaultViewName = null;
109 return new ModelAndView(defaultViewName);
110 }
111 }
112
113.. _definitions.xml:
114
115Request routing via definitions.xml
116-----------------------------------
117
118In order for the framework to route requests for :code:`myfirstpage` correctly, we'll need to create an entry in :code:`sdk/ecomp-sdk/epsdk-app-os/src/main/webapp/WEB-INF/defs/definitions.xml` that looks like this:
119
120::
121
122 <definition name="myfirstpage" template="/app/fusion/scripts/myapp/myfirstpage/myfirstpage.html" />
123
124Then, add the following to :code:`sdk/ecomp-sdk/epsdk-app-os/src/main/webapp/app/fusion/scripts/myapp/myfirstpage/myfirstpage.html`:
125
126.. code-block:: html
127
128 <html>
129 <body>
130 <p>It worked!</p>
131 </body>
132 </html>
133
134Now, build and install your application as before. If everything worked, you should see `It worked!` in your browser window when you visit myfirstpage_ after logging in.
135
136When the request from the browser comes in, the framework creates a mapping from :code:`myfirstpage` to the MyAppController, which in turn maps your definition name to a particular template. Soon, we'll fill in that template to do more interesting things.
137
138.. _myfirstpage: http://localhost:8080/epsdk-app-os/myfirstpage