You are on page 1of 16

Struts Plugin

ExchangeRate

Objectives

Create a plug-in for Struts-base application

Home page

Result of Exchange

Source code: index.jsp (1/3)


1. <%@page contentType="text/html"%>
2. <%@page pageEncoding="UTF-8"%>
3.
4. <%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
5.
6.
7. <html>
8.
9.

<head>
<title>Online Exchange Rate</title>

10.

</head>

11.

<body>

12.

<h1>Exchange Rates</h1>

13.

<h2>Currency Converter</h2>

14.

<html:form action="Lookup.do">

15.

<table>

16.

<tr>

Source code: index.jsp (2/3)


<td>Enter the mount:</td>
<td><html:text property="amount"/></td>
</tr>
<tr>
<td>From:</td>
<td>
<html:select property="symbol">
<html:option value="US$">US Dollar</html:option>
<html:option value="Euro">Euro</html:option>
<html:option value="Yen">Japanese Yen</html:option>
</html:select>
</td>
</tr>
<tr>
<td>To</td>
<td>Indian Rupees</td>
</tr>

Source code: index.jsp (3/3)


<tr><td colspan="2" align="center"><html:submit/></td></tr>
</table>
</html:form>
</html>

Source code: DisplayCurrency.jsp (1/2)


1.

<%@page contentType="text/html"%>

2.

<%@page pageEncoding="UTF-8" %>

3.
4.
5.
6.

<html>
<head>
<title>Online Exchange Rates</title>

7.

</head>

8.

<body>

9.

<h1>Exchange Rates</h1>

10.

<h2>Currency Converter</h2><br/><br/>

11.

<table>

12.

<tr><b>Current Exchange Rate:</b></tr>

13.

<tr>

14.

1 <%= request.getAttribute("SYMBOL") %> = <%=


request.getAttribute("PRICE") %> Rs.

15.

</tr>

16.

<br/><br/>

Source code: DisplayCurrency.jsp (2/2)


<tr><b>The exchange rate for the amount entered:</b></tr>
<tr>
<%= request.getAttribute("AMOUNT") %>
<%= request.getAttribute("SYMBOL") %> = <%=
request.getAttribute("EXCHRATE") %> Rs.
</tr>
</table>
</body>
</html>

LookupForm.java
1.

package com.myapp.struts;

2.
3.

import javax.servlet.http.HttpServletRequest;

4.

import org.apache.struts.action.*;

5.
6.

public class LookupForm extends ActionForm {

7.

private String symbol=null;

8.
9.

private String amount=null;

10.
11.

// set/get methods

12.

public void reset(ActionMapping mapping, HttpServletRequest request){

13.

symbol=null;

14.

amount=null;

15.
16.

}
}
10

LookupAction.java (1/3)
1.

package com.myapp.struts;

2.

import java.util.Properties;

3.

import javax.servlet.ServletContext;

4.

import javax.servlet.http.*;

5.

import org.apache.struts.action.*;

6.
7.

public class LookupAction extends Action {

8.
9.

/* forward name="success" path="" */

10.

private final static String SUCCESS = "success";

11.
12.

public ActionForward execute(ActionMapping mapping, ActionForm form,

13.

HttpServletRequest request, HttpServletResponse response)

14.

throws Exception {

15.

String exchRate=null;

16.

String symbol=null;

17.

String amount=null;

LookupAction.java (2/3)
String target=new String("success");
if(form!=null){
LookupForm lookupForm=(LookupForm)form;
symbol=lookupForm.getSymbol();
amount=lookupForm.getAmount();
exchRate=getQuote(symbol, amount);
}
If (exchRate==null) {
target=new String("failure");
} else {
request.setAttribute("EXCHRATE", exchRate);
request.setAttribute("SYMBOL", symbol);
request.setAttribute("PRICE", globalPrice);
request.setAttribute("AMOUNT", amount);
}
return mapping.findForward(target);
}

LookupAction.java (3/3)
protected String getQuote(String symbol, String amount){
ServletContext context=servlet.getServletContext();
Properties prop=(Properties)context.getAttribute("info");
String price=prop.getProperty(symbol);
globalPrice=price;
Float exch=new Float((Float.parseFloat(price))*(Float.parseFloat(amount)));
String exchRate=exch.toString();
return exchRate;
}
String globalPrice=null;
}

Sample code of ConvertPlugin.java (1/2)


1. package com.myapp.struts;
2.
3. import java.util.Properties;
4. import javax.servlet.*;
5. import org.apache.struts.action.*;
6. import org.apache.struts.config.ModuleConfig;
7. public class ConverterPlugin implements PlugIn{
8.
9.

/** Creates a new instance of ConverterPlugin */

10.

public ConverterPlugin() {

11.

12.
13.

public void init(ActionServlet servlet, ModuleConfig applicationConfig)

14.

throws ServletException{

15.

String[] names={"US$", "Euro", "Yen"};

16.

String[] values={"38.5", "23", "19.6"};

17.

Properties prop=new Properties();

Sample code of ConvertPlugin.java (2/2)


for(int i=0; i<names.length; i++){
prop.setProperty(names[i], values[i]);
}
ServletContext context=servlet.getServletContext();
context.setAttribute("info", prop);
}
public void destroy(){
System.out.println("Plugin is Stopping");
}
}

Sample code of struts-config.xml


1.

<struts-config>

2.

<form-beans>

3.

<form-bean name="LookupForm" type="com.myapp.struts.LookupForm"/>

4.

</form-beans>

5.
6.
7.

<action-mappings>
<action path="/Lookup" type="com.myapp.struts.LookupAction"
name="LookupForm">

8.

<forward name="success" path="/DisplayCurrency.jsp"/>

9.

<forward name="failure" path="/index.jsp"/>

10.

</action>

11.

<action input="/index.jsp" name="LookupForm" path="/LookupActionForm"

12.

scope="session" type="com.myapp.struts.LookupAction"/>

13.
14.
15.

</action-mappings>
...

You might also like