Navigation Bar

Sunday, November 8, 2009

Struts 2 : The source code

 LookupAction.java

package src;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.parameter.StrutsParameter;

public class LookupAction extends ActionSupport {
	
	private Double price = null;
	private String symbol;
	private String target = new String ("success");
			
    protected Double getQuote ( String symbol) {
        if (symbol != null && symbol.equalsIgnoreCase("SUNW")) {
            return Double.valueOf (25.00);
        }
        return null;
    }
	
	public void validate() {
		if (symbol == null || symbol.trim().length() == 0) {
			addFieldError("symbol", "Symbol is required");
		}
	}
	
    public String getSymbol(){
		return (symbol);
	}
     
	@StrutsParameter  // ← Add this annotation 
	public void setSymbol (String symbol) {
		this.symbol = symbol;
	}
	
	public Double getPrice(){
		return (price);
	}
  
    @StrutsParameter  // ← Add this annotation
	public void setPrice (Double price) {
		this.price = price;
	}


    public String execute (){
        System.out.println("Symbol received: " + symbol);   
		if (hasFieldErrors()) {
            return INPUT;
        }	
		price = getQuote(symbol);
		//Set the target to failure
		if (price == null) {
			target = new String ("failure");
		}
		else {
			target = new String ("success");
		}
		return target;
    } //execute
}
index.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" %>

<html>
<head>
    <title>Stock Quote Struts Application</title>
</head>

<body>
    <table width="500" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td height="68" width="48%">
                <div align="left">
                    <img src="images/stockquote.jpg" alt="Logo">
                </div>
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>		
    </table>

    <h2>Enter a Stock Symbol</h2>

    <!-- Begin Struts form -->
	<form action="lookup" method="post">
        <label for="symbol">Symbol:</label>
        <input type="text" name="symbol" id="symbol"/>
        <input type="submit" value="Lookup"/>
    </form>
</body>
</html>
quote.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Stock Quote Struts Application</title>
</head>

<body>
    <table width="500" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td height="68" width="48%">
                <div align="left">
                    <img src="images/stockquote.jpg" alt="Logo">
                </div>
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>		
    

		<h1>Stock Quote Results</h1>
			<p>Symbol: <s:property value="symbol"/></p>
			<p>Price: <s:property value="price"/></p>
        <tr>
            <td>&nbsp;</td>
        </tr>		
	</table>	
</body>
</html>

<body>

</body>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee 
         https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
         version="6.0">

    <display-name>Struts 2 - Get Quote Example</display-name>
    
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

struts.xml
<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.ui.theme" value="simple" />
    <package name="default" extends="struts-default">
        <action name="lookup" class="src.LookupAction">
            <result name="success">/quote.jsp</result>
			<result name="failure">/quote.jsp</result>
        </action>
    </package>
</struts>

Copy all these components in the tomcat webapps folder as below
In <CATALINA_HOME>/webapps folder create the project structure for the getquote project as below

DirectoryContains
/getquoteThe root directory of the web application. All JSP and HTML files are stored here
/getquote/WEB-INFThis directory contains all resources related to the application that are not in the document root of the application. It is where the web application deployment descriptor web.xml is located. For the struts project, this folder will include the struts .tld. .dtd and .xml files.
/getquote/WEB-INF/classesThis is where the servlet and other classes are located. In struts 1.2 the struts.xml file is also placed in this directory.
/getquote/WEB-INF/libThis directory contains Java Archive (JAR) files that the web application is dependent on. The jars from the blank struts project are copied here.
/getquote/srcThe directory having the java source code.
/getquote/imagesImage files are kept in this folder

We create the ant build file using the script below
<project name="getquote" default="compile-single" basedir=".">

<property name="src.dir" value="${basedir}/src"/>
<property name="build.dir" value="${basedir}/WEB-INF/classes"/>
<property name="lib.dir" value="${basedir}/WEB-INF/lib"/>
<property name="tomcat.lib" value="E:/install/tomcat/tomcat11013/lib"/>

<path id="classpath">
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${tomcat.lib}">
            <include name="*.jar"/>
        </fileset>
    </path>

   <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="init">
        <mkdir dir="${build.dir}"/>
    </target>


<target name="compile-single">
    <echo message="Compiling LookupAction.java ..."/>
    <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath" includeantruntime="false">
	<include name="src/LookupAction.java"/>
	</javac>
</target>

</project>

God's Word for the day
A call to Repentance
Turn back to the Lord and forsake your sins;
  pray in his presence and lessend your offense.
Return to the most high and turn away from iniquity
  and hate intensely what he abhors
Sirach 17:25-26

The Golden Rule 
In everything do to others as you would have them do to you; 
  For this is the law and the prophets
Mathew 7:12

No comments:

Post a Comment