Creating the JDBC TableView Servlet

Một phần của tài liệu web development with sas by example, 2nd edition (2006) (Trang 211 - 215)

The JDBC TableView Servlet Template creates a complete Web application using the Model 2 (Model-View-Controller) Web Application Architecture. Models (including data vewers) are objects that provide a way to access data, while views provide a visual representation of this data.6 Models negotiate data transfer via interfaces. A local model contains its own data (such as a list of strings), while a remote model retrieves its data from a remote server (such as the SAS AppDev Studio Version 2 DataSetInterface). See http://support.sas.com/rnd/appdev/V2/webAF/ModelView.htm for a detailed explanation of this concept.

In J2EE, a Web application can be divided into three layers—model, view and controller—each with their own responsibilities:

ƒ A model represents business data and business logic or operations that govern access and modification of this business data. Often the model serves as a software approximation to real-world functionality. The model notifies views when it changes and enables the view to query the model about its state. It also enables the controller to access application functionality encapsulated by the model.

ƒ A view renders the contents of a model. It accesses data from the model and specifies how that data should be presented. It updates data presentation when the model changes. A view also forwards user input to a controller.

ƒ A controller defines application behavior. It dispatches user requests and selects views for presentation. It interprets user inputs and maps them into actions to be performed by the model. In a stand-alone GUI client, user inputs include button clicks and menu selections. In a Web application, they are HTTP get and post requests to the Web tier. A controller selects the next view to display based on the user interactions and the outcome of the model operations. 7

6 Do not confuse "view" in this sense with the term as it is used in the DATA step and in PROC ACCESS. In this case, we are talking about components that are used to create a user interface. Database views, in contrast, are SQL-based structures that provide user-specific access to data.

7Inderjeet Singh, Beth Stearns, Mark Johnson, and the Enterprise Team, Designing Enterprise Applications with the J2EETM Platform, 2nd ed. (Boston, MA: Addison-Wesley Professional, 2002).

Chapter 9 Developing Java Server-Side Applications with webAF Software 197

In this case the model is the underlying SAS data, while the view is the JSP. The name of the new controller servlet logically enough defaults to ControllerServlet.

To create a servlet, follow these steps:

1. Select JDBC TableView Servlet, and then click Next.

Display 9.22 Selecting the JDBC TableView Servlet Template

2. Select Basic Servlet in Step 5 as shown, then click Next.

3. Click Next in Step 6, Specify Basic Servlet Options.

4. Click Next in Step 7, Specify WebApp Project Options.

5. Click Finish in Step 8 to build the application. This concludes the WebApp Project Wizard.

Modifying the Controller Servlet

The SAS template generates a JavaServer Page index.jsp as the view and a controller servlet called ControllerServlet.java that uses a JDBC connection to access SAS data on the server. To view the generated servlet, click the Files tab in the webAF Project Navigator and then select

<webappbase>/WEB-INF\classes\servlets\ControllerServlet.java.

You need to make three changes to the generated code in order to point to a specific SAS data set and to allow table editing:

1. Locate the static variable JDBC_DATABASE_URL. Change the value to point to your SAS Workspace Server:

private static final String

JDBC_DATABASE_URL = "jdbc:sasiom://HUNDING:8591";

2. Change the value of the variable jdbcQuery to read the desired data—in this example the sample shoe sales data set:

//Setup the query for the connection, // such as "select * from sashelp.class"

String jdbcQuery = "select * from sashelp.shoes";

3. Locate the initializer of the variable adapter. Add the following line to allow editing of the data:

//Create the model adapter and set it on the session adapter = new JDBCToTableModelAdapter(

sas_JDBCConnection, jdbcQuery);

adapter.setReadOnly(false);

This completes the necessary changes to the Java servlet code!

If you need more information on any of the Java components in the servlet or JSP, highlight the term you want to search and press F1 for help. SAS has provided context- sensitive help and links to the API to make it somewhat easier to learn about the components and their methods.

Creating the JavaServer Page

Select <webappbase>/index.jsp in the webAF Project Navigator. This page contains the custom tags required for the application. In order to enable row-level editing, add the following code to the TableViewComposite tag:

<sas:TableView>

<sas:Edit enabled="true" singleRowEditing="false"/>

</sas:TableView>

Testing the Application

To build the project using the Java Ant tool,8 select Build X Build Project from the webAF menu, or just press F7.

The sample templates all assume you are using a local SAS server. If your data resides on a remote host, you need to change the properties of the project by selecting File X Project Properties. Select Startup in the left-hand navigator widow. In the box labeled For this project, pass additional arguments to the Java interpreter, change the name of the local host to that of the remote server.

Start the built-in Tomcat 4 servlet engine by going to Tools X Services X Start Java Web Server. Now run the application by selecting Build X Execute in browser or by clicking on the exclamation point on the Build toolbar.

Depending on the speed of your processor, it can take a very long time to display the page for the first time. This is because the JSP must be parsed, validated, translated into a servlet and finally executed. After the first time the page is displayed, however, it should load much more quickly.

8 Ant is a Java-based build tool for compiling complex applications. For more information about Ant, see the Apache documentation at http://ant.apache.org/manual/.

Chapter 9 Developing Java Server-Side Applications with webAF Software 199

If you have followed all of these directions, the Web page shown in Display 9.23 should appear in your browser window. Note that the URL for this page points to the

ControllerServlet, not to index.jsp. That is because the servlet is delegating the HTTP request to the page.

Display 9.23 Table of SASHELP.SHOES

The format of this page is determined by a CSS file supplied as part of the default application. In this case, the file is located under the project directory in

webapp\styles\sasComponents.css. You can change the appearance of the page by editing this style sheet; scroll down about 900 lines to the following comment:

TABLEVIEW STYLES (applies to

com.sas.servlet.tbeans.tableview.html.TableView)

Changing the font, color or weight of the visual components is just a matter of locating the correct element tag and making the desired changes.

SAS has also provided a template for using what they call Renderers to display rows and columns in alternating colors and fonts (see http://support.sas.com/rnd/appdev/examples/

ServletJSP/DisplayingaTableusingJDBC_BLD.htm). This template can be loaded in the same way as the TableView example we have been using.

The next display shows the bottom of the screen. Note the navigation arrows, these are used to scroll up and down over the records in the data set. The three icons in the lower left (3, X and +) run JavaScripts that are generated by the wizard. If the check box in the first column of the page is checked, clicking the 3 symbol at the bottom of the form deletes the row; otherwise, this action commits any changes made to the record. The X symbol cancels any edits, while the + symbol is used to add a new record to the data set.

Clicking on a column heading brings up a menu for sorting the data or moving the

column. Scroll up to the top of the page, as shown in Display 9.23. The small symbol in the upper left can be used to export the entire data set to Microsoft Excel.

Một phần của tài liệu web development with sas by example, 2nd edition (2006) (Trang 211 - 215)

Tải bản đầy đủ (PDF)

(361 trang)