Registering Runtime Views Using Servlets
Use
You can register the custom runtime views you have created using a servlet that is executed each time the service starts.
The following procedure describes how you can do this using the SAP NetWeaver Developer Studio (NWDS).
Prerequisites
- You have created a content component or a contextual panel component. For more information, see Creating Custom Runtime Views Using the GP API .
- The versions of the Java Guided Procedures (GP) you use and your development track must match.
Procedure
- Open the Development Infrastructure perspective and create a development component (DC) of type J2EE → EnterpriseApplication.
- Create a second DC of type J2EE → Web Module.
- Add the Web module to the enterprise application:
- Open the J2EE perspective and in the Project Explorer, select the enterprise application component from step 1.
- Navigate to the Modules folder of the application and from the context menu choose Add/Remove.
- From the Available J2EE Modules list, select the Web module component from step 2 and choose OK.
- In the Development Infrastructure perspective, select the Web module DC and add the following build-, deploy- and runtime dependencies:
- GP-CORE → caf/eu/gp/api (external)
- ENGFACADE → tc/bl/exception/lib (default)
- GP-CORE → caf/eu/gp/api/wd (ContentComponent)
- Repeat the step above for the enterprise application DC.
- To create a servlet, open the context menu of the Web module DC and choose New → Servlet…
- Provide a servlet name and package.
- Set the type to HTTP Servlet.
- Enable the init() and destroy() servlet methods.
- In the new servlet class, replace the automatically-generated code with the one listed below:
| Example Servlet Code |
|---|
packagecom.testcustomer; importjava.io.IOException; importjava.util.Enumeration; importjavax.servlet.ServletConfig; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importcom.sap.caf.eu.gp.rtview.api.GPRTViewFactory; importcom.sap.caf.eu.gp.rtview.api.IGPRTView; importcom.sap.caf.eu.gp.rtview.api.IGPRTViewManager; importcom.sap.caf.eu.gp.wdcomponent.api.GPWDComponentFactory; importcom.sap.caf.eu.gp.wdcomponent.api.IGPWDComponent; importcom.sap.caf.eu.gp.wdcomponent.api.IGPWDComponentManager; importcom.sap.localization.ResourceAccessor; publicclass RegisterView extends HttpServlet {
// a unique identifier for view final static String TYPE_NAME = "TestView"; /** * init() is called once at the instantiation of the servlet * We register our new view here. */ public void init(ServletConfig cfg) throws ServletException {
super.init(cfg); boolean isRegistered = false; String viewId = TYPE_NAME; IGPRTViewManager vManager = GPRTViewFactory.getRTViewManager(); try {
// check if view is already registered: Enumeration rViews = vManager.getRTViews(); while (rViews.hasMoreElements()){
IGPRTView view = (IGPRTView)rViews.nextElement(); if (view.getRTViewId().equals(viewId)){
isRegistered = true; break; } } // if it is not registered if (isRegistered == false) {
// instantiate the appropriate managers IGPWDComponentManager mgr = GPWDComponentFactory.getWDComponentManager(); // create and register a Web Dynpro Component ResourceAccessor resourceAccessor = null; IGPWDComponent contentComponent = mgr.createWDComponent( "MyOwnContentPane", "com.sap.caf.eu.gp.runtime.view.CContentPane", "sap.com/caf~eu~gp~runtime~view", resourceAccessor, "FrameTitle"); // create and register a runtime view configuration IGPRTView viewConfiguration = vManager.createRTView( viewId, contentComponent, resourceAccessor, "PaneTitle", "PaneDescription", IGPRTView.MANDATORY_GROUP_PROCESS_STRUCTURE, false); vManager.registerRTView(viewConfiguration); } } catch (Exception e) {
e.printStackTrace(); } } /** * destroy() is called when the instance of the servlet is destroyed * We unregister our new view here. */ public void destroy() {
super.destroy(); String viewId = TYPE_NAME; IGPRTViewManager vManager = GPRTViewFactory.getRTViewManager(); try {
// unregister the runtime view vManager.unregisterRTView(viewId); } catch (Exception e) {
e.printStackTrace(); } } } |
- In the Project Explorer, expand the node of the Web module DC and select WebContent → WEB-INF → web.xml.
- From the context menu, choose Open and open the Web Objects tab page:
- Expand the Servlets node and select the servlet you created previously.
- In the load on startup field, enter 1 .
As a result, the servlet is executed every time the service starts.
- Save, build and deploy your enterprise application.
More Information