Você está na página 1de 16

Birlasoft Ltd.

Struts By Sarangapani:
Understanding the code is one thing (and the easier thing at that) and actually executing the program, is quite a different thing altogether! We must do each step with maximum care, to avoid a lot of problems. -----------------------------------------We begin by creating a folder (say) g:\examplestruts> Our proposed package name is demo1; So create a subfolder named 'demo1'. Cd to g:\examplestruts\demo1 -Create the following six files in demo1 folder. i) query.jsp ii) QueryForm.java iii) QueryAction.java iv) sqlbean.java v) resultbean.java vi) result.jsp -----------------------------------We cannot compile QueryForm.java & QueryAction.java, unless we first install Struts in our system. --------------------------------How do we install Struts in our system? ----------------Steps. -------------

1) Transfer jakarta-struts1.1 zip file from Developer IQ CD to g: drive. 2) Unzip it into c:\unzipped. 3) It got unzipped as jakarta-struts1.1 folder. ---------------------------------------------We get three folders and 3 files. 4) The 3 folders are b) Lib Classification: Public a) contrib.

Birlasoft Ltd.
c) Webapps ============ The 3 files are: 5) a) readme.txt b) License.txt c) Instal.txt ----------------------------------------------6) open webapps folder. 7) we will find the following war files there.( web-application-archives) a)struts-blank b)struts-documentation c) struts-example d) struts-exercise-taglib e) struts-template f) struts-upload g) struts-validator ---------------------------------------------8) copy struts-blank.war into: g:\tomcat41\webapps folder.

---------------------------------------------9) In a DOS window, cd to g:\tomcat41\bin 10) >set JAVA_HOME=D:\JDK1.4 11) startup ----12) tomcat server gets started . 13) Now in another dos window , check up that struts-blank.war has automatically expanded by the server and we get a folder named struts-blank. 14) If you expand struts-blank folder, there are 3 subfolders there. i) META-INF

Classification: Public

Birlasoft Ltd.
ii) WEB-INF iii) pages There is also an index.jsp ----------------------------------------------15) start the browser and type the url as : http://localhost:8080/struts-blank/index.jsp ---------------------------------------------16) You will get the corresponding display as follows:
Welcome! To get started on your own application, copy the struts-blank.war to a new WAR file using the name for your application. Place it in your container's "webapp" folder (or equivalent), and let your container auto-deploy the application. Edit the skeleton configuration files as needed, restart your container, and you are on your way! (You can find the application.properties file with this message in the /WEBINF/src/java/resources folder

----------------------------------------------Thus, we have correctly installed struts in our tomcat server. =========================================== Inside the WEB-INF folder, we have the following subfolders and files. sub-folders i)classes ii) lib iii) src ----------------------files

=== tld files ( 6 files) ===== 1) struts-bean.tld 2) struts-html.tld 3) struts-logic.tld 4) struts-template.tld 5) struts-tiles.tld

Classification: Public

Birlasoft Ltd.
6) struts-nested.tld ------------------xml files ( 5 files). ===== i) web.xml ii) struts-config.xml iii) tiles-defs.xml iv) validation.xml v) validator-rules.xml ============================================ There are a number of jar files available in lib folder. i) commons-beanutils.jar ii) commons-collections.jar iii) commons-digester.jar iv) commons-fileupload.jar v) commons-lang.jar vi) commons-logging.jar vii) commons-validator.jar viii) jakarta-oro.jar ix) struts.jar ------------------------------------All this is just for information. ===========================================

When we want to build our own application, we first copy struts-blank folder in the webapps folder of tomcat & Rename the folder .(say) sam. And restart the Tomcat server.
struts.jar

was copied to c:\quick folder for easily remembering the classpath.

==============================================Now, we are ready to begin compiling. We are now in g:\examplestruts\demo1 >set path=c:\windows\command;d:\jdk1.4\bin >set classpath=g:\examplestruts;c:\quick\struts.jar;d:\tomcat41\common\lib\servlet.jar
..demo1>javac QueryForm.java

Classification: Public

Birlasoft Ltd.
..demo1>javac sqlbean.java ..demo1>javac resultbean.java ..demo1>javac QueryAction.java

( if we compile the files one after the other, in this sequence, we will be able to compile easily. After compiling, copy all the class files in this folder to:
g:\tomcat41\webapps\sam\WEB-INF\classes\demo1

copy query.jsp & result.jsp to:


'g:\tomcat41\webapps\sam' now in another dos window, cd to: g:\tomcat41\webapps\ sam\WEB-INF\struts-config.xml edit the struts-config.xml as follows: ==========================================

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation// DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/ dtds/struts-config_1_1.dtd"> <struts-config> <form-beans> <form-bean name="queryForm" type="demo1.QueryForm" /> </form-beans> <action-mappings> <action path="/Query" type="demo1.QueryAction" name="queryForm" scope="session" input="/query.jsp" > Classification: Public

Birlasoft Ltd.
<forward <forward </action> </action-mappings> </struts-config> ============================================== -After all these steps, restart the Tomcat server, so that the settings become effective. Type the URL in browser as: 'http://localhost:8080/sam/query.jsp' You should get a form with orange background, etc. If we type the password as 'ADMINISTRATOR' and the sql as 'select * from table1', we will get the query result. ( Ofcourse, the example presumes the usual creation of a simple Access database named 'dbdemo' with just table1 having two fields ( name, place) and registering it in ODBC. If the password is wrong, the page appears to be not responding just showing whatever we typed. So, our adventure with Struts has had a happy ending. If we get, this far, we can and must build up further by in-depth study of books.& articles. In the forthcoming instalments, we will see advanced Struts topics like : DynaForm Validator Framework Tiles RequestProcessor Scaffold Struts-Tags -----------------------name="success" path="/result.jsp" /> name="failure" path="/query.jsp" />

Developing Struts Application


Classification: Public

Birlasoft Ltd.
If we are asked to give a very brief outline of Struts, we can enumerate the following points. i) All requests are passed through a controller servlet, known as Action-servlet. This is achieved by suitable 'url-mapping' in web.xml file.We have been doing 'URL-pattern' in Tomcat4,when using servlets. And in j2ee webserver like Tomcat, this facilitates 'centralized-declarative change management', by editing the concerned XML files, without touching the source or class files of the servlets.or Struts Actions... ii) All data submitted by user are sent to corresponding ActionForm.There are many actionforms but only one ActionServlet(because, it is the controller). iii) The ActionServlet, examines the source of request and extracts the data from specified actionform and sends it to a specified instance of Action class.(as specified in struts-config.xml). iv) The action object carries out the business logic either directly or through helper classes , creates an instance of valuebean, populates this bean with data and sends the bean to the View JSP.( an instance of ActionForward). v) The important and distinctive feature of this arrangement is that the entire thing is done by 'Declarative-Management'.(and hence ActionMapping) vi) Sometimes, the data submitted has to be validated and error messages , generated. (ActionErrors). vii) We use tags in input side and also for output view.(The input form also belongs to 'view' category in MVC.)( Struts-tags) viii) The details about the ActionServlet and other servlets if any ,in our application are given in web.xml ix) Details about various action classes,forms, action-forwards etc are given in struts-config.xml --------------------------------------------It is now the right time ( & 'high time' at that!)to take up a simple and practical example. Our focus in this tutorial is actual implementation. In an illustration,we should not introduce more than one 'unfamiliar' tool or concept. Many tutorials, bring in tools like 'Ant', 'Eclipse' etc, which have their own learning curve! Our aim is to avoid such things and yet develop a useful lesson. -------------------------------------------We use the following six files, in this demo, in that sequence too. i) query.jsp ii) QueryForm.java(derived from ActionForm) iii)QueryAction.java(derived from Action) iv) sqlbean.java ( a utility bean) v) resultbean.java ( a value bean) vi) result.jsp ( besides the web.xml & struts-config.xml files)

Classification: Public

Birlasoft Ltd.

Where is the much-spoken-about ActionServlet? That is provided by Struts Framework itself. We do not subclass it, except for advanced work. It remains unobtrusively in the background , silently supervising things. As Ted Husted says, many developers leave it alone. The truth is that , we need hardly set our eyes on web.xml in this demo..or on the sourcecode of ActionServlet. -------------------------------------------In our example, the starting point is ' query.jsp', which is invoked by the URL, 'http://localhost:8080/sam/query.jsp'.The user fills up a form giving password and also an sql query.If the password is 'ADMINISTRATOR', the query is executed. Otherwise, the form is presented back to the user with the values already entered by him intact, so that he need not fill up the form again but needs to make only the required corrections. This is achieved by the FormBean. It is a Struts-specific class known as 'ActionForm'. We provide, getter and setter methods for each of the controls in the form. In our example, these are 'password' and 'query'.We are also using struts-tags named as 'html' tags. (for simplicity, we are using simple text rather than password control).Note the taglib directive. This is not JSTL but Struts Tag Library.We should note the following very carefully. The name of the form is given as 'queryForm' & action is 'Query'. (the case is extremely important!).There is automatic conversion of action from 'Query' to 'Query.do'.
============================================

//

query.jsp

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <html> <body bgcolor=yellow> This is query page <br> <html:form action="Query" name="queryForm" type="demo1.QueryForm" > Are you the Administrator?<br> Your password please!<br> <html:text property="password" /> <br> query <br> <html:text property="query" size="60" /> <br> <html:submit /> </html:form> </body> </html>

============================================== Usually, in normal html forms, the control has a 'name' attribute. But in html:text, it is known as 'property'. The corresponding formbean is given below. =========================================
All actions with extensions of '*.do', are automatically directed to the StrutsServlet(ie) ActionServlet. In the default web.xml file provided by the Struts application, the actionservlet is given 'URL-pattern' as '*.do'.( see web.xml)as given below.)

Classification: Public

Birlasoft Ltd.
------------------------------( We need not type even a single line of web.xml. It is already available in the Struts application.) If the full file is printed here, it will only look forbidding. So, the relevant portion alone is shown here.ActionServlet configuration mentions the name of the servlet as 'action' and gives the fully qualified class-name of servlet. This is followed by init-parameters section. where the 'config' param is indicated as 'struts-config.xml in WEB-INF folder of the application.(shown in bold).This is followed by servlet-mapping, as already mentioned. Finally, we have tag-library descriptors for struts custom-tag-libs 'bean' etc. like 'html','logic',

It is worth mentioning again that we do not have to type this file.It is equally important that we should not corrupt this file carelessly! It is best left alone. ------------------------------web.xml ===========================================

<?xml version="1.0" ....."?> ....... ....... ........ <!-- Action Servlet Configuration --> <servlet> <servlet-name>action</servlet-name> <servlet-class>
org.apache.struts.action.ActionServlet </servlet-class>

<init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config.xml</param-value> </init-param> ........ ........ ........ <!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> ..... ........ ........ <!-- Struts Tag Library Descriptors --> <taglib> <taglib-uri>/tags/struts-bean</taglib-uri> <taglib-location> /WEB-INF/struts-bean.tld

Classification: Public

Birlasoft Ltd.
</taglib-location> </taglib> <taglib> <taglib-uri>/tags/struts-html</taglib-uri> <taglib-location> /WEB-INF/struts-html.tld </taglib-location> </taglib> <taglib> <taglib-uri>/tags/struts-logic</taglib-uri> <taglib-location> /WEB-INF/struts-logic.tld </taglib-location> </taglib> .... ..... </web-app> ============================================== -When the user submits the query.jsp, the formbean is automatically filled up with the values from the jsppage and the flow goes to the ActionServlet.
// QueryForm.java package demo1;

import javax.servlet.http.*; import org.apache.struts.action.*; public class QueryForm extends ActionForm { String String password query =null; =null;

//-----------------------------public { return } public void setPassword(String b) { password=b; } //----------------------------public String { getQuery() password; String getPassword()

Classification: Public

Birlasoft Ltd.
return } public void setQuery(String { query=a; } //--------------------------public void reset(ActionMapping HttpServletRequest { password=null; query=null; } } ============================================== mapping, request) a) query;

In the struts-config.xml file, we make two entries. One entry is for the instance of QueryForm bean and the other entry is for the instance of QueryAction class.As the entry for 'query action' makes a reference to 'query form', let us first see the details of the entry for 'query form'.The struts-config.xml file in WEB-INF folder is created by us and is the nerve-center of customized functionality.
------------------------------------------

( this is the part dealing with the formbean)

<form-beans> <form-bean name="queryForm" type="demo1.QueryForm" </formbeans> />

-----------------------------------This means that our formbean is named 'queryForm' and is available in demo1 subfolder of classes folder. (WEB-INF\classes\demo1\QueryForm.class) Carefully note the name of the bean. It is the same name given in the instance of QueryForm class,as it appears in QueryAction.java,given separately.(all the authors follow uniform pattern of naming the bean.The class name begins with capital letter and the instance begins with lowercase).Since all these are inter-dependent, unless we are careful, we can never even get started with invoking the form! Classification: Public

Birlasoft Ltd.
------------------------------------The next segment of mapping in struts-config.xml deals with the action mapping for the instance of Action class, (ie) QueryAction. ---------------------------<action-mappings> <action path="/Query" type="demo1.QueryAction" name="queryForm" scope="session" input="/query.jsp" > <forward name="success" path="/result1.jsp" /> <forward name="failure" path="/query.jsp" /> </action> </action-mappings> -------------------------------------

It means that the request comes from the path "/Query.do", the corresponding action class is QueryAction class in demo1 subfolder of classes folder of webserver.The input is coming from query.jsp. Finally, it says that the matching form to be used is 'queryForm'. Therefore, the action class extracts the properties from queryForm and does some validation according to our code. If the user had correctly entered the password as 'ADMINISTRATOR', processing goes on. (the code snippet from QueryAction.java is as given below). --------------------------------------------QueryForm String String queryForm =(QueryForm) form; a = queryForm.getPassword(); b = queryForm.getQuery();

if(a.equals("ADMINISTRATOR")) { ------------------------------------( Though, at first, the Struts code looks unfamiliar and frightening,on repeated reading and familiarity, it

sounds like a song! Perhaps, the reason, why, users get addicted!) Classification: Public

Birlasoft Ltd.
Otherwise, the returned ActionForward in the action class is "failure" and we have mapped this string to query.jsp (ie) going back to the opening form itself! We must admit that , this declarative manipulation , is a really clever and inspired innovation,in flow management.By simply changing the entry in this struts-config file, the behaviour of the program can be easily changed.
RequestDispatcher class instances are NOT explicitly mentioned anywhere, but the same effect is obtained.

The full code for QueryAction has been given below.. -----------------------------------------// QueryAction.java demo1;

package

import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import org.apache.struts.action.*; public class QueryAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { QueryForm String String queryForm =(QueryForm) form; a = queryForm.getPassword(); b = queryForm.getQuery();

if(a.equals("ADMINISTRATOR")) { System.out.println (" now in QueryAction ========"); sqlbean bean1 = new sqlbean(); // business delegation System.out.println("bean1 ready"); String r= bean1.getresult(b); System.out.println ("function invoked on bean1"); System.out.println("value is..."+r); resultbean mathew = new resultbean();

System.out.println("mathew bean created"); mathew.setValue(r); System.out.println String ("value set for mathew");

m = mathew.getValue();

Classification: Public

Birlasoft Ltd.
System.out.println("verifying the value"); System.out.println(m); System.out.println ("--------ok -------------"); HttpSession session=request.getSession();

session.setAttribute("result",mathew); System.out.println("attribute for 'result' set as resultbean mathew");

bean=(resultbean)session.getAttribute("result");

System.out.println(bean.toString()); String v=bean.getValue();

System.out.println("verifying...."+v); System.out.println ("now sending mathew to result.jsp"); return }else{ return } (mapping.findForward("failure")); (mapping.findForward("success"));

} } ======================================== If the password is correct, the processing proceeds to create an instance of 'sqlbean'. This type of delegating the work to a functionbean is the recommended practice. We are advised not to let the Action class itself do any business-processing directly. Instead we create an instance of functionbean and just invoke a method on it by passing the parameter and getting the return value.We illustrated the same method in the tutorial on MVC in the last edition too and so it should not be difficult to follow. ------------------------------------// sqlbean.java demo1;

package

import java.io.*; import java.sql.*; public class sqlbean { public String getresult(String sql) { String r = ""; try {

Classification: Public

Birlasoft Ltd.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:dbdemo"; Connection connection=DriverManager.getConnection(url); Statement statement=connection.createStatement(); ResultSet rs = statement.executeQuery(sql); while(rs.next()) { String a = rs.getString(1); String b = rs.getString(2); r=r+a+"<br>"+b+"<br>"+"---------"+"<br>"; } }catch(Exception e1) {System.out.println(""+e1); } return r; } } ============================================== After getting the result from the utilitybean, the code creates an instance of valuebean known as 'resultbean'. This has just one property (ie) value. The name of this bean has been given as 'mathew'., just to make it standout from the crowd.And mathew's value is set as 'r'. --------------------------------------// resultbean.java package demo1;

public class resultbean { String value;

public resultbean(){ value=" "; }

public String

getValue(){

return value; }

Classification: Public

Birlasoft Ltd.
public void value=v; } } setValue(String v){

=================================== We now create a session context( though, some authors frown upon it ), and set the session-attribute of "result" as mathew! Thus, we are passing the bean itself to the destination (ie) result.jsp In result.jsp, we have used just jsp-tags like <jsp:useBean....> and <jsp:getProperty..> Carefully note the syntax. especially the 'id' and 'name'. The 'id' is NOT 'mathew' but 'result'!. We just extract the value and automatically display it.Thus, we have met the stringent requirement that our view pages should be absolutely free from 'scriptlets'. -----------------------------------------// result.jsp

<html> <body bgcolor=orange> id="result" scope="session" class="demo1.resultbean" /> />

<jsp:useBean

<jsp:getProperty <br> ok here </body> </html>

name="result"

property="value"

=====================================

Classification: Public

Você também pode gostar