+ All Categories
Home > Documents > Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

Date post: 11-Feb-2022
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
17
SAP Platform Ecosystem Migrating a Spring based Web Application to SAP NetWeaver
Transcript
Page 1: Migrating a Spring based Web Application to SAP NetWeaver

SAP Plat form Ecosystem

Migrating a Spring based Web Application to SAP NetWeaver

Page 2: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 2

Copyright © Copyright 2005 SAP AG. All rights reserved. No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. Microsoft, Windows, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation. IBM, DB2, DB2 Universal Database, OS/2, Parallel Sysplex, MVS/ESA, AIX, S/390, AS/400, OS/390, OS/400, iSeries, pSeries, xSeries, zSeries, z/OS, AFP, Intelligent Miner, WebSphere, Netfinity, Tivoli, and Informix are trademarks or registered trademarks of IBM Corporation in the United States and/or other countries. Oracle is a registered trademark of Oracle Corporation. UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C®, World Wide Web Consortium, Massachusetts Institute of Technology. Java is a registered trademark of Sun Microsystems, Inc. JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape. MaxDB is a trademark of MySQL AB, Sweden. SAP, R/3, mySAP, mySAP.com, xApps, xApp, SAP NetWeaver, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and in several other countries all over the world. All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary. These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

Page 3: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 3

Icons in Body Text

Icon Meaning

Caution

Example

Note

Recommendation

Syntax

Additional icons are used in SAP Library documentation to help you identify different types of information at a glance. For more information, see Help on Help → General Information Classes and Information Classes for Business Information Warehouse on the first page of any version of SAP Library.

Typographic Conventions

Type Style Description

Example text Words or characters quoted from the screen. These include field names, screen titles, pushbuttons labels, menu names, menu paths, and menu options.

Cross-references to other documentation. Example text Emphasized words or phrases in body text, graphic titles, and table

titles.

EXAMPLE TEXT Technical names of system objects. These include report names, program names, transaction codes, table names, and key concepts of a programming language when they are surrounded by body text, for example, SELECT and INCLUDE.

Example text Output on the screen. This includes file and directory names and their paths, messages, names of variables and parameters, source text, and names of installation, upgrade and database tools.

Example text Exact user entry. These are words or characters that you enter in the system exactly as they appear in the documentation.

<Example text> Variable user entry. Angle brackets indicate that you replace these words and characters with appropriate entries to make entries in the system.

EXAMPLE TEXT Keys on the keyboard, for example, F2 or ENTER.

Page 4: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 4

Table of Contents Introduction ............................................................................................................................................................. 5

Objectives .................................................................................................................................................................................................5 Prerequisites .............................................................................................................................................................................................5

Introducing the sample application........................................................................................................................ 6 Moving over to SAP Web AS 6.40.......................................................................................................................... 9

Creating the Web Module........................................................................................................................................................ 9 Create a new Web Module Project ...........................................................................................................................................................9 Import WEB-INF directory contents ........................................................................................................................................................9 Adjusting web.xml..................................................................................................................................................................................12

Importing the Java sources .................................................................................................................................................... 14 Procedure................................................................................................................................................................................................14 Result......................................................................................................................................................................................................14

Importing the web content ..................................................................................................................................................... 15 Procedure................................................................................................................................................................................................15 Result......................................................................................................................................................................................................15

Creating the Enterprise Application Project .......................................................................................................................... 15 Procedure................................................................................................................................................................................................15 Result......................................................................................................................................................................................................16

Deploying the sample application to SAP Web AS 6.40....................................................................................................... 17 Procedure................................................................................................................................................................................................17 Result......................................................................................................................................................................................................17

About the Author................................................................................................................................................... 17

Page 5: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 5

Introduction The Spring Framework offers lots of useful features to help developers with everyday development issues and Spring based applications become more and more widespread. Therefore, in the course of system consolidation and the consequent J2EE migration task on SAP Web AS 6.40, Spring related issues frequently emerge.

Objectives In this document we will:

• Move an existing Spring based application to the NetWeaver Developer Studio

• Deploy the application to SAP Web AS 6.40

Prerequisites SAP Web AS – Java 6.40 SP11

NetWeaver Developer Studio SP11

Download TutIntegratingSpring.zip. The archive contains the original application and the already migrated version (as NWDS EAR and Web Module Project).

Page 6: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 6

Introducing the sample application Our sample application is a most simple web application running on a Jakarta Tomcat 5.0.28. The business logic implements a single “hello world” POJO object Hello.java with a business method doSomeBusiness() simply returning a String. The web front end is implemented using the Spring MVC framework where AppController.java acts, guess what, as the Spring MVC Controller.

Hello.java package com.sap.spring.bl;

public class Hello {

public String doSomeBusiness(){

return "Doing some business...";

}

}

The bean configuration for Hello.java is done in springapp-servlet.xml and it is directly injected into the appController bean. Note that no explicit context configuration via any applicationContext.xml file is necessary.

springapp-servlet.xml <beans>

..

<bean id="appController"

class="com.sap.spring.web.AppController">

<property name="hello">

<ref bean="helloBean"/>

</property>

</bean>

<bean id="helloBean" class="com.sap.spring.bl.Hello" />

<bean id="messageSource"

class="org.springframework.context.support.ResourceBundleMessageSource">

<property name="basename">

<value>messages</value>

</property>

<bean>

<bean id="urlMapping"

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="mappings">

<props>

<prop key="/hello.htm">appController</prop>

</props>

</property>

</bean>

</beans>

Page 7: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 7

In AppController.java the business method doSomeBusiness()of Hello.java is called and the return value is propagated to a ModelAndView object which then is used by the DispatcherServlet which is defined in web.xml and is loaded on startup

AppController.java package com.sap.spring.web; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import com.sap.spring.bl.Hello; public class AppController implements Controller { private Hello hello; public ModelAndView handleRequest(HttpServletRequest request,

HttpServletResponse response)throws ServletException, IOException { Map model = new HashMap(); model.put("helloString", getHello().doSomeBusiness()); return new ModelAndView("hello", "model", model); } public void setHello(Hello hello) { this.hello = hello; } public Hello getHello() { return hello; } }

In hello.jsp the String finally is extracted from the model and printed to the screen (Figure 1):

Page 8: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 8

hello.jsp <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %> <html> <head><title><fmt:message key="title"/>SpringApp</title></head> <body> <h3><fmt:message key="heading"/></h3> Message from HelloBean in application context:<br> <b><c:out value="${model.helloString}"/></b> </body> </html

Figure 1 - Sample Application running on Jakarta Tomcat

Page 9: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 9

Moving over to SAP Web AS 6.40 In the following section we will move the sample application to the NetWeaver Developer Studio J2EE Project structure.

Creating the Web Module First of all we’re creating the Web Module. In order to follow the next steps you have to download the sample application archive and extract it anywhere on your file system.

Create a new Web Module Project Procedure

1. Open NWDS and Switch to J2EE Development Perspective.

2. Create a new Web Module Project called “TutIntegratingSpringWEB” in the (See Creating Web Module Projects on SAP Help).

Result A Web Module Project called “TutIntegratingSpringWEB” is visible in J2EE Explorer View (Figure 2).

Figure 2 - Web Module Project

Import WEB-INF directory contents Procedure

1. Switch from J2EE Explorer to Navigator View

2. Import the WEB-INF directory content from the sample application into the Web Module Projects WEB-INF directory. The import includes all the necessary libs as well as springapp-servlet.xml, spring.tld and web.xml.

Page 10: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 10

When you’re prompted to overwrite the original web.xml click “YES”.

Detailed Information on importing resources from the file system can be found in the Developer Studios Help by searching Importing resources from the file system.

3. Add the libraries to the projects Java Build Path.

4. To do so, open the project properties by right clicking the Web Module Projects root node.

5. Select Properties from the context menu.

6. Select Java Build Path

7. Select Libraries Tab

8. Select Add Libraries

9. Choose all Libraries from “TutIntegratingSpringWEB” Projects WEB-INF\lib folder

10. Press OK

Result You have successfully added all necessary libraries to your Web Module Projects Java Build Path build (see Figure 3). The WEB-INF directory contains the same files and folder structure as the sample applications WEB-INF directory (see Figure 4).

Page 11: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 11

Figure 3 - Libraries in Build Path

Figure 4 - WEB-INF directory import

Page 12: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 12

Adjusting web.xml

After having overridden the web.xml NetWeaver Developer Studio automatically removes all servlet definitions from web.xml source code and adds those to the Servlet Candidates folder in the J2EE Explorer (see Figure 5).

.

Figure 5 - Adding Servlet Candidates to web.xml

In our case the definition of DispatcherServlet has been removed so we have to add it back to the web.xml. To not getting into trouble with the NWDS XML parsing it is strongly recommended to edit all xml-files only within the NetWeaver Developer Studio.

Procedure 1. Expand node Servlet Candidates in the Web Module Project tree.

2. Right click DispatcherServlet and choose Add to web.xml (see Figure 5). The servlet definition has been added to the web.xml source. But still the startup parameter is missing. To add the startup parameter…

3. Double click web.xml in the Web Module Project tree and choose the Web Objects Tab.

4. Expand the Servlets node and click DispatcherServlet.

5. Edit the servlets properties in right handed pane (Figure 6)

a. Change name from “DispatcherServlet” back to springapp.

b. Set load on startup = 1.

Figure 6 - Editing servlet properties

Page 13: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 13

6. Save the Project.

Result You have successfully adjusted the web.xml file so that it has the original content again:

web.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>springapp</servlet-name> <servlet-class>

org.springframework.web.servlet.DispatcherServlet </servlet-class>

<load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springapp</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <taglib> <taglib-uri>/spring</taglib-uri> <taglib-location>/WEB-INF/spring.tld</taglib-location> </taglib> </web-app>

Page 14: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 14

Importing the Java sources In this step we import all the source code and other resources from the original folder structure into our Web Module Project.

Procedure 1. Import Java sources from the file system into the source directory (Figure 7)

Figure 7 – Import Java Sources

2. Choose Finish.

3. Import messages.properties from WEB-INF\classes of sample application into the source folder of the Web Module Project.

4. Change property “heading” in messages.properties from “SpringApp running on Jakarta Tomcat 5.0.28“ to „SpringApp running on SAP Web AS 6.40“

Result You have successfully imported the Java Sources and messages.properties into your Web Module Projects source folder (see Figure 8).

Page 15: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 15

Figure 8 - Imported Java Sources

Importing the web content Procedure

1. Import index.jsp and the jsp folder containing hello.jsp from the sample applications war folder into the Web Module Projects webContent folder.

Result You have successfully imported all JSPs into the Web Module Project (see Figure 9).

Figure 9 – Importing the web content

Creating the Enterprise Application Project Now that the import and adjustments are done our application is ready to be deployed onto the SAP Web AS 6.40. Note that SAP Web AS 6.40 only accepts applications and components packed in EAR files as deployable units. It’s not possible to deploy a single .war or .jar file.

Therefore the NetWeaver Developer Studio provides the Enterprise Application Project type. To deploy the sample application we will create such a project and add the Web Module Project to it.

Procedure 1. Create a new Enterprise Application Project named “TutIntegratingSpring” via the new project wizard. For

information on how to create this type of project see Creating Enterprise Application Projects on SAP Help.

Page 16: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 16

2. In the new project wizard add the Web Module Project “TutIntegratingSpring” as a referenced module to the Enterprise Application Project (see Figure 10).

Figure 10 - Referencing Web Module Project

3. Double click node application.xml under the newly created Enterprise Application.

4. Select Modules Tab.

5. Select TutIntenratingSpringWeb.

6. Set applications context to springapp (see Figure 11).

7. Save the project

Result A new Enterprise Application Project named “TutIntegratingSpring” is created. The project references the Web Module Project “TutIntegratingSpring” (see Figure 11) and the applications context root is set correctly.

Figure 11 - Editing the context root

Page 17: Migrating a Spring based Web Application to SAP NetWeaver

Migrating a Spring based Web Application to SAP NetWeaver

SAP AG 2005 17

Deploying the sample application to SAP Web AS 6.40 The last step in migrating the sample application to SAP Web AS 6.40 is to build the EAR archive and deploy it via the NetWeaver Developer Studio.

Procedure 1. Right click the TutIntegratingSpring Project to open the context menu.

2. Click Build Application Archive.

3. Right click the created TutIntegratingSpring.ear Project to open the context menu.

4. Click Deploy to J2EE engine.

Result The sample application now is deployed to SAP Web AS and is ready to run:

Open your Web-Browser and navigate to the application via

http://localhost:50000/springapp

The sample application appears in the browser window and is doing nothing but some business (See Figure 12). Congratulations!

Figure 12 - Sample application on SAP Web AS 6.40

About the Author Helge Martin is an expert in J2EE development and application migration on SAP Web AS 6.40. He has been recently working in the SAP Platform Ecosystem Market Development Engineering Team.


Recommended