Show TOC

Developing the Web ModuleLocate this document in the navigation structure

Prerequisites

The Start of the navigation path Window Next navigation step Open Perspective  Next navigation step Other Next navigation step Java EE End of the navigation path perspective is opened.

Procedure

1. Create and Configure the Web Module Project

The Web resources are developed in a Web project.

  1. Choose Start of the navigation path File Next navigation step New Next navigation step Project End of the navigation path from the main menu.

    The New Project dialog opens.

  2. Select Start of the navigation path Web Next navigation step Dynamic Web Project  End of the navigation path. Choose Next .

  3. Enter ConverterWeb in the Project name field.

  4. To assign the Web project to the previously created Enterprise Application project, select the Add project to an EAR checkbox and select ConverterEAR from the EAR project name dropdown list.

  5. Choose Finish to create your Web project structure.

  6. Add and configure the JavaServer Faces facet to the Web project.

    1. In the Project Explorer , select the ConverterWeb project.

    2. In the context menu, choose Properties .

      The Properties for ConverterWeb dialog opens.

    3. In the properties tree, select Project Facets .

    4. In the Project Facets area, select the JavaServer Faces checkbox, and select version 1.2 .

    5. Choose Further configuration required .

      The Modify Faceted Project dialog opens.

    6. In the JSF Implementation Library area, select the SAP Component Library for JSF checkbox. Choose OK .

    7. Choose Apply , then choose OK .

2. Create a Web Client

The Web client in this tutorial is a JSP.

  1. In the Project Explorer , select Start of the navigation path ConverterWeb Next navigation step WebContent End of the navigation path

  2. In the context menu, choose Start of the navigation path New Next navigation step Other End of the navigation path.

    The New dialog opens.

  3. Select Start of the navigation path Web Next navigation step JSP End of the navigation path. Choose Next .

  4. Enter index.jsp in the File name field.

  5. Choose Finish .

    The Developer Studio creates the index.jsp file and opens it for editing.

  6. Implement the JSP as follows:

                         <%@ page import="com.sap.tutorial.javaee.ConverterLocal, java.math.*, javax.naming.*"%>
    <%@taglib uri="http://java.sap.com/jsf/html" prefix="sap-h"%><%@taglib uri="http://java.sap.com/jsf/core" prefix="f"%>
    
    <html>
    <head>
    <title>Currency Converter</title>
    </head>
    
    <body>
    
    <f:view><body><h1> Currency Converter</h1><hr>
    <sap-h:form >
            <sap-h:outputText value="Enter an amount to convert:"/><br><br/>
            <sap-h:inputText id="name" value="#{managedBean.amount}" />
            <sap-h:commandButton action="#{managedBean.convert}" value="Sumbit" />
    </sap-h:form>
    
    <sap-h:form rendered="#{managedBean.correctAmountSet}"><br/>
            <sap-h:outputText value="#{managedBean.amount} " escape="false"/>
            <sap-h:outputText value="USD are: " escape="false"/>
            <sap-h:outputText value="#{managedBean.convertedAmountInEUR} " escape="false"/>
            <sap-h:outputText value="EUR " escape="false"/><br/>
            <sap-h:outputText value="#{managedBean.amount}  " escape="false"/>
            <sap-h:outputText value="EUR are: " escape="false"/>
            <sap-h:outputText value="#{managedBean.convertedAmountInUSD} " escape="false"/>
            <sap-h:outputText value="USD "/><br/>
    </sap-h:form>
    <sap-h:form rendered="#{!managedBean.correctAmountSet}" ><br/>
            <sap-h:outputText value="Only digits are allowed." escape="false"/><br/>
            <sap-h:outputText value="Use a dot(.) as decimal separator." escape="false"/><br/>
    </sap-h:form>
    
    </f:view>
    
    </html>
                      
  7. Save the index.jsp file.

3. Configure the Project References

Since your Web project uses resources from the ConverterEJB project, you have to set these references:

  1. Select ConverterWeb in the Project Explorer .

  2. In the context menu, choose Properties .

  3. In the Properties for ConverterWeb tree, select Java Build Path .

  4. Choose the Projects tab.

  5. Choose Add .

  6. Select the ConverterEJB checkbox, then choose OK .

  7. In the properties tree, select Project References .

  8. On the Setting Java Build Path screen that pops up, choose Apply .

  9. Select the ConverterEAR and ConverterEJB checkboxes.

  10. Choose OK .

4. Create the ManagedBean

  1. In the Project Explorer , select the ConverterWeb project.

  2. In the context menu, choose Start of the navigation path New Next navigation step Class End of the navigation path.

    The New Java Class wizard opens.

  3. In the Package field, enter com.sap.tutorial.javaee.web .

  4. In the Name field, enter ManagedBean .

  5. Choose Finish .

    The Developer Studio creates the ManagedBean class file and opens it for editing.

  6. Implement the ManagedBean class as follows:

                         @EJB ConverterLocal converter;
    
    String amount;
    String convertedAmountInEUR;
    String convertedAmountInUSD;
    
    boolean correctAmountSet;
    
    public ManagedBean() {
            }
    
    public String convert() {
    
            setCorrectAmountSet(amount);
            
            convertedAmountInEUR = null;
            if (isCorrectAmountSet()) {
                    BigDecimal value = new BigDecimal(amount);
                    convertedAmountInEUR = converter.dollarToEuro(value).toPlainString();
                    }
            
            convertedAmountInUSD = null;
            if (isCorrectAmountSet()) {
                    BigDecimal value = new BigDecimal(amount);
                    convertedAmountInUSD = converter.euroToDollar(value).toPlainString();
                    }
            
            return "Submit";
            }
    
    public void setAmount(String amount) {
            this.amount = amount;
            }
    
    public String getAmount() {
            return amount;
            }
    
    public String getConvertedAmountInEUR() {
            return convertedAmountInEUR;
            }
    
    public void setConvertedAmountInEUR(String convertedAmountInEUR) {
            this.convertedAmountInEUR = convertedAmountInEUR;
            }
    
    public String getConvertedAmountInUSD() {
            return convertedAmountInUSD;
            }
    
    public void setConvertedAmountInUSD(String convertedAmountInUSD) {
            this.convertedAmountInUSD = convertedAmountInUSD;
            }
    
    public void setCorrectAmountSet(String amount) {
            Double dAmount = null;
            try {
                    dAmount = Double.valueOf(amount);
                    } catch (NumberFormatException nfe) {
                    correctAmountSet = false;
                    }
            
            if (dAmount != null) {
                    correctAmountSet = true;
                    }
            }
    
    public boolean isCorrectAmountSet() {
            return correctAmountSet;
            }
    
    public void setCorrectAmountSet(boolean correctAmountSet) {
            this.correctAmountSet = correctAmountSet;
            }
                      
  7. To import the additional classes that you refer in your code, choose Start of the navigation path Source Next navigation step Organize Imports End of the navigation path from the main menu.

  8. To save the ManagedBean.java file, choose Start of the navigation path File Next navigation step Save End of the navigation path from the main menu.

5. Edit the Web Module Deployment Descriptors

In the Project Explorer , expand the WEB-INF folder under WebContent of the ConverterWeb project.

Editing the web.xml

  1. Double-click the web.xml file to open it for editing.

  2. Choose the Source tab of the editor.

  3. Replace the existing <welcome-files-list> entries (if any) with the following one:

                         <welcome-file-list>
            <welcome-file>faces/index.jsp</welcome-file>
    </welcome-file-list>
                      
  4. Add the following code to the XML source to define a local EJB reference to the Converter bean:

                         <ejb-local-ref>
            <ejb-ref-name>Converter</ejb-ref-name>
            <ejb-ref-type>Session</ejb-ref-type>
            <local>com.sap.tutorial.javaee.ConverterLocal</local>
    </ejb-local-ref>
                      
  5. To save the file, choose Start of the navigation path File Next navigation step Save End of the navigation path from the main menu.

Editing the faces-config.xml

  1. Double-click the faces-config.xml file to open it for editing.

  2. Choose the Source tab of the editor.

  3. Implement the source of the faces-config.xml as follows:

                         <?xml version="1.0" encoding="UTF-8"?>
    
    <faces-config
            xmlns="http://java.sun.com/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
            version="1.2">
    <managed-bean>
            <managed-bean-name>managedBean</managed-bean-name>
            <managed-bean-class>com.sap.tutorial.javaee.web.ManagedBean</managed-bean-class>
            <managed-bean-scope>request</managed-bean-scope>
            <managed-property>
                    <property-name>amount</property-name>
                    <property-class>java.lang.String</property-class>
                    <value></value>
            </managed-property>
            <managed-property>
                    <property-name>convertedAmountInEUR</property-name>
                    <property-class>java.lang.String</property-class>
                    <value></value>
            </managed-property>
            <managed-property>
                    <property-name>convertedAmountInUSD</property-name>
                    <property-class>java.lang.String</property-class>
                    <value></value>
            </managed-property>
            <managed-property>
                    <property-name>correctAmountSet</property-name>
                    <property-class>boolean</property-class>
                    <value>false</value>
            </managed-property>
    </managed-bean>
    <navigation-rule>
            <display-name>index</display-name>
            <from-view-id>/index.jsp</from-view-id>
            <navigation-case>
                    <from-outcome>Submit</from-outcome>
                    <to-view-id>/index.jsp</to-view-id>
            </navigation-case>
    </navigation-rule>
    
    </faces-config>
    
                      
  4. To save the file, choose Start of the navigation path File Next navigation step Save End of the navigation path from the main menu.

Result

You have successfully created the Web module of the Converter application. You can continue with deploying and running the application.

For more information, see Deploying and Running the Application .