![]() |
| ||
| struts tutorials Intro Web application development was once simple, there was HTML and a browser. There was no dynamic or business data, so no business applications. But this simplicity lasted for a small while. Today we are forced to do many complicated application developments. This tutorial assumes that you have the knowledge of JSP, Servlet, custom tag libraries and XML What is a struts? Struts from the Jakarta project is a development framework for java servlet application based upon the model-view-controller (MVC). The aim of MVC architecture is to separate the business logic and data of the application from the presentation of data to the user. Following is the small description of each of the components in MVC architecture: Model The model represents the data of an application. Anything that an application will persist becomes a part of model. The model also defines the way of accessing this data ( the business logic of application) for manipulation. It knows nothing about the way the data will be displayed by the application. It just provides service to access the data and modify it. View The view represents the presentation of the application. The view queries the model for its content and renders it. The way the model will be rendered is defined by the view.The view is not dependent on data or application logic changes and remains same even if the business logic undergoes modification. Controller All the user requests to the application go through the controller. The controller intercepts the requests from view and passes it to the model for appropriate action. Based on the result of the action on data, the controller directs the user to the subsequent view Composition Struts is comprised of controller servelet, beans and others java classes, configuration files, and tag libraries. This means when you download struts you will have the following: With all these together Struts uses a set of configuration files. Together this gives you the skeleton that you can use to struts to your application. A strut has been designed to give you modularity and loses coupling in your application. If yours building a simple, small application you might find it complicated to have to create and handle so may files. You might even be tempted to put all together to put all your code in a single jsp-file. But my advice is “don’t do it”. I know you can build single page application faster using only one jsp page. But if were talking ballot more complex application, the extra effort put in by using modular framework will soon be rewarded Downloading and installing struts Struts is downloaded form the JAKARATA PROJECT and the simplest way to install struts is to copy a small struts application, packed in struts-blank.war, form the downloaded file to your servlet container. if you are using TOMCAT and many other servlet containers you place the war file in the “webapps” folder and you now only need to restart your server and you will have struts-blank running. On tomcat you can check your installation with this URL: http://localhost:8080/struts-blank/index.jsp Our first struts application Our first small application will be a page with an HTML form containing all the well known controls. This is the form below with struts application. The example code is shown below freewebschools register page <form method=”post”><br><br> When we submit this page it should redirect itself without losing the data that we have entered. Starting a new struts application The web.xml file is where servlets and other stuff are defined to the servlet container. <web app> The file contains 3 sections: A the definition of the struts servlet named "actionservlet"You will see that the servlet will be called if our browser requests a file when we submit the form in our one page application we will decide to use the action name "sumit.do" The mapping of the request to a specific action and action form class is done in the struts-config.file. I have edited the file form “struts-blank” to suit our one page application <struts-config> As you see the file contains two section: the form-beans section, that lists the actionForm beans and the action-mappings. Building the jsp page In order for the ActionForm bean to work, you must use struts own tags for creating the HTML form and the controls in it <%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %> Coding the actionform class This class must extend the struts actionForm, and must have setters and getters for all the form controls in the jsp-page. Package Freewebschools.playground; This class is placed in the WEB-INF/classes/Freewebschools/playground directory Coding the action class The action class is seen form the application programmers prospective the heart of the application. This is where you must decide how you will separate your own application code. most often the action class should be kept as thin as possible, placing business logic in other beans or even EJBS’S on other servers. The implementation of the action class must contain a “perform” method, which receives the request and response objects, the instance and the action mapping information from the configuration file. A very simple ActionForm class, which simply lets the request pass unaltered, is given as following Import javax.servlet.http.*; This class is also placed in WEB-INF/classes/Freewebschools/playground directory An important thing to remember is that struts only creates a single instance of the action class, shared amongst all users of the application. Testing your application Restart your servlet container To test your example enter this url in the browser http://locslhost:8080/myproject/submit.jsp. If you application works, you will see that last name is filled with “Freewebschools”(which means that struts has already created an instance of the actionForm bean, and extracted the data form it) Now enter all the data in the controls and press submit. Note that url changes to Submit.do and that all the data stays unchanged in the form |
| ||
| Re: struts tutorials I am able to see the submit.do in the url. However, the page itself is coming blank after the submit button. This is the log msg, [ActionServlet] Loading chain catalog from jar:file:/C:/jboss-4.0.4.GA/server/default/tmp/deploy/tmp9910Practice-exp.war/WEB-INF/lib/struts-core-1.3.8.jar!/org/apache/struts/chain/chain-config.xml 22:59:51,876 INFO [ComposableRequestProcessor] Initializing composable request processor for module prefix '' 22:59:51,913 INFO [CreateAction] Initialize action of type: org.biswa.orem.ut.SubmitAction |
| All times are GMT -4. The time now is 6:19 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC