Struts
Struts
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786          Page 1
1
                                                                        JAVA Means DURGASOFT
Struts
                                        Syllabus:
     DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786          Page 2
 2
                                                                       JAVA Means DURGASOFT
    DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786          Page 3
3
                                                                              JAVA Means DURGASOFT
When designing an app by the developers for the same problem diff developers
provide diff designs. So designs may not be same but they output the app same.
If one developer wants to understand the designs given by another developer then
feel uncomfortable, because the designs may not be I the same way as that
developer expected.
So in order to solve the designing problems and also to inform the developers about
what technology is used at what place, we got web application development models.
MODEL1:- this model introduced along with JSP technology, in this model a JSP is a
responsible to provide all functionality required for a web application may contain
any no. of (or) series of JSP pages in order to provide the functionalities.
Bean DB
           DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786          Page 4
       4
                                                                            JAVA Means DURGASOFT
Client JSP
P.L
+- Bean DB
B.L
   1) Limitation of Model1:- the application may contain a series of pages an &if any
   changes are done in one page then, if need the changes other pages also we call
   this problem as a page-centric problem.
   Because one page is depends on another, all the pages should be developed by a
   simple developer only, it means division of labor is no possible.
   Each page is responsible for providing (or) implementing the logics like input
   verification, Session management, securityetc. so it increases the size of a JSP
   and also burden on the developer this application code we call as redundancy.
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786          Page 5
     5
                                                                          JAVA Means DURGASOFT
  Implementing B.L in a JSP is not in industry standard, because JSP should contain
  tags, but not code.
Client servlet
MODEL:-2
  Client                     servlet
DB
  Actually sun micro system introduced, JSP technology as a replacement for servlet
  technology, but industries realize that for the web app development both servlet
  and jsp required, and introduced model3 that is MVC architecture.
       DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786          Page 6
   6
                                                                         JAVA Means DURGASOFT
In the MVC applications, controller is a servlet, iew is JSP (or) html and javaBean
(or) ejb (or) spring are Model .
In Model2 P.L and B. Logic are separated from each other, so page centric
problem is eliminated.
all logics are separated, so changes from one logic does not effect other logic, it
means we got flexibility.
Division of laboure is possible, because the logics are devided into diff component
, so that application time reduced.
      DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786          Page 7
  7
                                                                         JAVA Means DURGASOFT
1) An application can contain any number view and Model components, but it
   contain only single controller
2) Two presentation pages (jsps) can not communicate each other directly if
   communicated then we get page centric Problem again.
3) Model and view components can not talk each other. It should be done all
   ways through controller as a mediator.
4) Each component should contain, its specific logic. We can not mix multiple
   logics in a single component.
1) If a jsp used as a controller, then we need to insert java code in jsp. But this is
   against industry standard. So jsp is not suitable as a controller.
2) If we use java bean as controller then it cannot receive (or) handle http
   request. So bean cannot be used as controller.
3) Finally servelt is suitable for used controller.
1) If we use servlet as view then we need to write presentation logic inside java
   code. the second reason is if we modify the presentation logic then we need
   reduced play and re compile and restart the server.
2) If javaBean is used as view then it cannot given reponse page.
3) Finally jsp is suitable to use as view, because if any presentation logic is
   modified, then it will be automatically effected.
      DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786          Page 8
  8
                                                                          JAVA Means DURGASOFT
1) If we use servlet as a model then we cannot get middle ware services required
   for the business logic.
2) If we use jsp as a model then again we should insert java code in jsp this
   against industry standard
3) Finally a Bean component is suitable for implementing business logic of an
   application. So it used as model.
PREVIOUS:-
B.L persisentlogic
PRESENT:--
PREVIOUS:-
beanDB
View controller
B.L persisentlog
       DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786          Page 9
  9
                                                                          JAVA Means DURGASOFT
1) Every system (i.e pc) will follow some commonalities. All applications
     will accept inputs from key board, produces output on to the screen
     and shares a common memory.
2)   The above concept is the foundation per the development of the
     frame work.
3)   A frame work contain set of libraries (or) classes. Each library will
     provide a basic foundation for the development of an application.
4)   We can define a framework as, it is a software, provides reusable
     components with basic functionality and the makes an application
     development faster and easyer.
5)   A framework will provide an abstraction layer and allowes the
     programmer to develop their application as faster and reduces
     burden on the developers.
       DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 10
10
                                                                           JAVA Means DURGASOFT
The same rule applicable for all frameworks like spring, hibernateetc.
STRUTS:-
Configurationfile
SPRING:-
Configurationfile
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 11
   11
                                                                             JAVA Means DURGASOFT
q) what is struts?
Struts is a frame work , used to develop web applications for java using
mvc2 , architecture.
  1) Commons.
  2) Struts.
Struts provided two sub projects inside it, those are validation and files.
Jakarta
Commons
Logging
Collection
Dbcp
Io
Struts
Validation
  Tiles
          DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 12
   12
                                                                        JAVA Means DURGASOFT
Types of frameworks:--
Literarly (or) generally frame work are devided into two types
1) Invasive
2) Non-invasive
Invasive frame work means it will force the programmer to create their
classes by extending (or) implementing their classes from predefined
classes (or) interfaces.
For example, struts is an invasive frame work and hibernate and spring
are non-invasive frame work.
     DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 13
13
                                                                           JAVA Means DURGASOFT
Strutscalanahanmay-2004 introduced.
  1) Validation
  2) I 18n(internationalization)
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 14
   14
                                                                        JAVA Means DURGASOFT
Jsp tag libries are the given for the development view component
either for accepting input (or) produce o/p.
Internal frame works:- are validation frame work and files frame
works these frame will add additional capabilities to a struts
application.
Note:- struts frame works is capable to integrate with any after frame
works, that is available in the market today(or) even that is going to
come tomorrow.
It is controller
Ft
C2 req Remove
Strutsconfig.xml
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 16
    16
                                                                            JAVA Means DURGASOFT
Object of RP;
Initialize
P v doet()
Process()
P v do post()
Process of RP
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 17
    17
                                                                            JAVA Means DURGASOFT
This component is acting as entry point for the request and exit point for
the request.
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 18
    18
                                                                           JAVA Means DURGASOFT
This component will get the flow details of an application through a struts
configuration file.
This component is used to store the input vales given by a client at a client
side, on the input values in this component only.
If an app need every small b.logic then we implement it inside action class
only and if complex b.logics are required then we will communicate the
outside model components like, ejb, spring..etc from action class.
Action class is a place from the control goes out of struts frameworks to
out side model and the action class place in to where the control comes
back to the struts frame work
Action class is not a servlet but followes servlet model. It means single
instance and multiple threads model.
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 20
   20
                                                                           JAVA Means DURGASOFT
real time services like transaction and security from the container
automatically.
In case of real time app action class access mediator to call the b.logic
implemented at out side model componenets.
By default action class is not thread safe but it is possible to convert action
class into thread safe, by applying synchronization mechanism.
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 21
   21
                                                                           JAVA Means DURGASOFT
<servlet>
<servlet.name>action</->
<servlet-class org.apache.struts.action.actionservlet<-
<load-on-startup>/
<init-param>
<param-name>config</>
<param-value>/WEB-INF/struts.config.xml</-
</init-param>
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 22
   22
                                                                            JAVA Means DURGASOFT
</servlet>
<servlet.mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url.>
</jer-mapping>
</web-app>
Struts.config.xml
               view
                               Login form
Input.jsp
Customer id cid
Customername cname
Customeraddress cadd
Sond
Cidd, cname,cadd
Setters&getters
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 23
    23
                                                                             JAVA Means DURGASOFT
Reset()
Validate()
Reset method is used for before sending the data the default values are
assinge.
1) actionform objects in struts is for starting client input and then to apply
validation on the given input.
1)setter methods.
2) getter methods.
3) reset method
4) validate method.
i.q) action form object will be created by the controller servlet (or) A.S and
by default
this object will be stored in session scope. It means form bean object per
each client session.
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 25
   25
                                                                              JAVA Means DURGASOFT
    1)   Org.apache.struts.actionform.
    2)   Org.apache.struts.action.dynaactionform.
    3)   Org.apache.struts.validator.validatorform
    4)   Org.apache.struts.validator.dynavalidatorform
    5)   Org.apache.struts.validator.validatorActionform
    6)   Org.apache.struts.validator.dynavalidatorActionform
Setters
Getters
           DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 26
    26
                                                                            JAVA Means DURGASOFT
<strut-config>
<form-beans>
<form-beanname=frm1type=loginform/>
</form-beanss>
</struts-config>
<strut-config>
<form-beans>
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 27
    27
                                                                            JAVA Means DURGASOFT
</form-beans>
</strut-config>
Action clas is a place where we insert either b.logic (or) integration logic.
Inside actionclass the only method, in which are we should implement our
logic is execute().
Throws exception
--
--
If(---)
Else
          DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 29
     29
                                                                            JAVA Means DURGASOFT
Example:-struts-config.xml
<s-c>
<formBeans>
<action-mapping>
</action></action-mapping>
</s-c>
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 30
   30
                                                                              JAVA Means DURGASOFT
Public sample()
If(flag)
Flag=true;
Else
Singleton:-if a class allowes as to create only one object then that class is
said to be a singleton class.
           DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 31
    31
                                                                            JAVA Means DURGASOFT
1)by throwing an exception , when typing to create more than one object
Private sample()
{
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 32
    32
                                                                            JAVA Means DURGASOFT
Returns;
Sample ob2=sample.getinstance()
In the above code ob1 and ob2 will containe same objects which means
ob1==ob2 simple address same. With two objects are equal to==then we
say that both are rep single object.
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 33
    33
                                                                           JAVA Means DURGASOFT
The reason is , inn case of an ordinary servlet a web container will make it
as singleton. But in case of actionservlet, this strutsframework developers
only madeit as singleton.
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 34
   34
                                                                           JAVA Means DURGASOFT
Validation
                  Session                         one.jsp
                  presentation
validation
session
presentation
Validation presentation
Two.jsp
Application controller :-
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 35
   35
                                                                           JAVA Means DURGASOFT
While developing MVc based applications, it may containe more than one
module. So if there is only frontcontroller then the front controller has to
maintaine the entire flow. It becomes complex.
So, in order to reduce the burden on frontcontroller each module flow will
be maintained controller are maintained by frontcontroller.
INVERSION OF CONTROLL(IOC):-
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 36
   36
                                                                           JAVA Means DURGASOFT
  1) Dependency lookup
  2) Dependency injection
  In lookup mechanism, explicitly one object with get the other object.
  Mostly this lookup we are going to observer when struts communicate
  with ejb
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 37
   37
                                                                        JAVA Means DURGASOFT
I.Q)Note:- the common design pattern used by all java applications (or)
projects is DTO.
Struts instalization:-www.struts.org.apach
     DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 38
38
                                                                           JAVA Means DURGASOFT
  Working with struts framework means, adding jar files into the lib
  folder of our application and adding the jar files required at the class
  path.
  Unzip the above zip file and take struts-blank-version.war fro apps
  folder.
  Copy the above war file into a newly created folder and extract the war
  file by using jartool.
under WEB-INF/lib folder, we will get the jar files related to struts
framework.
Into our web app, we need add these jar files into lib folder.
Example:- the following example is based on MVc model and the example
is creating for our front controller servlet.
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 39
   39
                                                                          JAVA Means DURGASOFT
Submit
Product.html sample2m
Submit
{0r)
{failures.jsp}- view.
Directory structure:-
fcExample
WEB-INF
Web.xml
Classes
Fcservlet
Sample.java
Sample.java
*.classes
login.html
       DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 40
  40
                                                                           JAVA Means DURGASOFT
product.html
success.jsp
failure.java
hashmap() object
m.put(/sathya.do,newsample())
m.put(/product,newsample());
login.html:-
<center>
<formaction=sathya.do>
Entername:<inputtype=textname=t1><br>
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 41
   41
                                                                            JAVA Means DURGASOFT
</form>
</center>
Product.html:-
<center>
<form action=prod.do>
</form>
</centr>
Web.xml:-
<web-app>
<servlet>
<servlet.name>frontcontroller</
<servlet-class>fcservlet</s.>
<load-on-startup>1<1>
</servlet>
<s.m>
<or>*.do>
<15-m>
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 42
   42
                                                                            JAVA Means DURGASOFT
</web-app>
Fcservlet.java:-
Import java*.servlet*;
Import java.io
Import java.urtil.*;
Map m;
M=new hashmap();
m.put(prod.do,new sample2());
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 43
    43
                                                                            JAVA Means DURGASOFT
String result;
String rpath=req.get.servletpath();
Objecto=m.get(r.path);
Sample1 s1=(sample1)o;
Result=s1 handle1(req,res);
Else{
Sample2 s2=(sample2)o;
Result=s2.handle2(req,res);
Rd.forward(req.res);
Sample1.java:-
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 44
    44
                                                                            JAVA Means DURGASOFT
Import javax.servlet.http.x;
String str=req.getparameter(t1);
If(str.equals(satya));
Returnsuccess
Else
Return failure;
Sample.java
Import
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 45
    45
                                                                            JAVA Means DURGASOFT
String str=req.getparameter(p1);
If(str.equals(p123))
Return success;
Else
Return failure;
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 46
    46
                                                                             JAVA Means DURGASOFT
Success.jsp:-
<%out.println(<h1>success</h1>);
%>
Failure.jsp:-
<%out.println(<h1>failure</h1>);
If we want compile the above servlet and both sample1 and sample2
          DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 47
     47
                                                                           JAVA Means DURGASOFT
http://localhash:Rov/Fcexample/logic.html
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 48
   48
                                                                            JAVA Means DURGASOFT
Success.jsp
res (or)
Failure.jsp
struts.config.xml:-
<s-c>
<form-beans>
</form-beans>
<action mapping>
</action>
</action.mappinng>
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 49
   49
                                                                            JAVA Means DURGASOFT
</s-c>
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 50
   50
                                                                          JAVA Means DURGASOFT
       DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 51
  51
                                                                          JAVA Means DURGASOFT
                            Loginapp
                                   Src
                                                  Loginform.java
                                                   Loginaction.java
                                                      WEB.INF
WEB.XML
                                                                    STRUTS.CONFIG.XML
                                                                   CLASSES
                                                                   LIB
                                                                           *.JAR
                                                LOGIN.JSP
                                                SUCCESS.JSP
                                                 FAILURE.JSP
<init-params>
<p-n>config</p-n>
<p-v>/<WEB-INF/classes/struts.config.xml<p-v>
</init-params>
Application development in eclipse IDE:-
Step1:-start eclipse ide dbl click on eclipse.exe from eclipse folder and
enter same work space nameok
Step2:-click on file menu new project javaprojectnext enter
projectname if version is change(select version)(loginapp) nextfinish
Step3:- RT click o WEB-INF newfolderenter folder name as
classes. Similarly lib folder also
Note:-to shift (or) transfer from package explorer to navigator ,click an
windowmenushow viewnavigator.
       DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 52
  52
                                                                           JAVA Means DURGASOFT
Struts provided same predefined tags those tags we can use the JSP pages
Step5:-click an
projectnamenewfileenterfilename(login.jsp)finish
<!login.jsp---
%@taglib uri=http://struts.apache.org/tag-html prefix=html%
<html:form action=login.do>
Username:<html:text property=uname/><br>
Password:<html:password property=pwd/><lrr>
<html:submit value=chick/>
</html:form>
Struts html.tld no need write from struts words.
Note:- while creating the above jsp, we used html tags given by struts
framework. The reason is , when we submit the request then immediately
these input values will be stored automatically in a framebean object.
2)if we use ordinary html tags in jsp then the input values will not be
stored in a formbean object
3)to use these html tags provided by struts, we need to add a standard uri
and prefox in our jsp
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 53
   53
                                                                           JAVA Means DURGASOFT
Failure.jsp:-<%out.println(<h1>failure</h1>); %>
  1) Struts-core-1.3.8-jar
  2) Servlet-api.jarok
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 54
   54
                                                                          JAVA Means DURGASOFT
       DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 55
  55
                                                                          JAVA Means DURGASOFT
       DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 56
  56
                                                                             JAVA Means DURGASOFT
<servlet-class>org.apache.struts.action.Actionservlet</->
<load-on-startup>1</
<init-param>
<param-name> config</param-name>
<param-value>/WEB-INF/struts-config-xml<1->
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</->
<url-pattern>*.do</->
</serv-mapping>
</web-app>
STEP:- RT click on web-inf newfile enter filenamestruts-
config.xml)ok
Copy the DTD in the existing project.
</DOCTYPE
---1-3>
<struts-config>
<form-beans>
          DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 57
   57
                                                                           JAVA Means DURGASOFT
</form-beans>
<action-mapping>
</action>
</action-mapping>
</struts-config>
STEP :- copy struts jar file into lib folder of our web application. Because
container does not know about the struts fws that way we inform the
tomcat by processing some jar files.
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 58
   58
                                                                           JAVA Means DURGASOFT
2) in a resource bundle, the data should be stored in the form of key value
pairs, where both key and values are used defined.
3) while creating a jsp, instated of manually typing lables, we can get them
from a resourcebundle.
5)while creating input jsp pages, if we want to get lables from a resource
bundle, then we need to use<bean:message>
The bean tags are provided by struts fw in a told file called struts-bean-tld.
If we want use bean tags in our jsp then we need the following taglib
directive in our jsp.
<%taglib uri=http://struts.apache.org/tags.beanprefix=bean%>
<bean:message key=input.user/>
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 59
   59
                                                                           JAVA Means DURGASOFT
<message-resources parameter-sathya/>
while applying validation, if any errors are occure then we need to get
error messages from the resource bundle only.
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 60
   60
                                                                            JAVA Means DURGASOFT
If(uname.length()==0)
Errors.add(uname,am1);
Return errors;
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 61
    61
                                                                             JAVA Means DURGASOFT
--
Bydefault false
</action>
NOTE:- while displaying the error messages, we can display either all
errors at one place (or) individual errors for each property separately
          DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 62
     62
                                                                           JAVA Means DURGASOFT
</-login.jsp
<html:form action=logindo>
<html:submit value=click/>
<html:form>
LOGIN FORM.JAVA:-
Import org.apache.struts.action.actionfrom;
Import org.apache.struts.action.ActionMapping;
Import org.apache.struts.action.ActionMessage;
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 63
   63
                                                                            JAVA Means DURGASOFT
Import org.apache.struts.action.ActionErrors;
Import javax.servlet.http.HttpservletRequest;
If(equals(uname) \\uname.length()==0)
Aes.add(uname,am1);
If(.equals(pwd)|| pwd.length()==0
Aes.add(pwd,am2);
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 64
    64
                                                                            JAVA Means DURGASOFT
Return aes;
</action-mappings>
<message-resources parameter=sathya/>
#SATHYA.PROPERTIES:-
Input.user=username
Input.pwd=password
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 65
    65
                                                                           JAVA Means DURGASOFT
  1) If the number input values are more then the size of the validate()
     will be increased. And it increases the burdon on the programmer.
  2) In validate() mostly we duplicate the same logic for multiple fileds.
     this increases to data redundancy more input values.
  3) In order to over comes the above problem, in struts we can apply
     declaration validation.
<form-bean
name=f1type=org.apache.struts.action.dynaActionform>
<form-property name=unametype=java.lang.string/>
</form-bean>
</form-beans>
Dynavalues.set(key,value)
Return dynavalues()
P AF execute(------)
Object o1=daf.get(uname);
String u=(string)o1;
     DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 68
68
                                                                        JAVA Means DURGASOFT
--
--
Collection map
in DAF class, we do not have setters and getter method for each
property. Instated, we have set and get method
in DAF class, another method given called get map() it return the
HashMap object and it containe all input values given by a client in the
form of (key, values) pair.
     DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 69
69
                                                                        JAVA Means DURGASOFT
Return dynavalues.get(key);
Return dynavalues;
     DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 70
70
                                                                        JAVA Means DURGASOFT
Class java
Nested class
--
--
Durga.java
     DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 71
71
                                                                        JAVA Means DURGASOFT
Public AF execute(----)--------exception
DAF daf=(DAY)form;
Object 01=daf.get(uname);
String u=(string)01;
String p=(string)daf.get(pwd);
--
--
     DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 72
72
                                                                        JAVA Means DURGASOFT
in this case we need write validations at action class level. But writing
input validation in action level is not good approach.
Note:- if we apply client side validations using java script then we can
eliminate the draw back of dynaActionForm.
In order to provide losse coupling b/w Action class and servlet API, we
do not use request object in the execute() for reading input.
If(-----)
--
--
If(! Aes.isEmpty())
saveErrors(req,res);
      DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 74
74
                                                                             JAVA Means DURGASOFT
--
--
}}
  Enter.jsp
  Username:-                 as             salAction
Salary:- DAF
Enter.jsp
Uname:-
Sal:-
          DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 75
   75
                                                                           JAVA Means DURGASOFT
-->save Errors(req,res) this method is given base class. Ie. Action that
way no need to necessary objects directly we can call
Ex 3:- the following example is for using DAForm and validating the input
at action class.
If input salary is valid then it return success and bonus value will be
printed if not valid then againe input page will be given back and dynamic
error message is printed on the browser.
EX2:- EXAMPLE2
SRC
salAction.java
WEB-INF
Web.xml
Struts-config.xml
Classes
*.classes
Sathya .properties
Lib
x.jar
enter.jsp
                                                  success.jsp
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 76
   76
                                                                           JAVA Means DURGASOFT
<html.submit value=click/>
</html:form>
STRUTS.CONFIG.XML:-
<strut-config>
<form-beans>
</form-bean>
</form-beans>
<forward.name=failure path=/enter.jsp/>
<action>
</action mapping>
</s-c>
salAction.java
import org.apache.struts.action.*;
import javax.servlet.httpx;
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 78
   78
                                                                            JAVA Means DURGASOFT
Public AF execute(--------)
String on=(string)df.get(ename);
Double d=(Double)df.get(esal);
If(S>8000)
Aes.add(esal,e);
If(!aes.is Empty())
saveErrors (req,res);
if(s<=8000)
Req.setAttribute(bonus,i1);
Else
Ref
}}
In the above Action class while constructing AM object, along with the
key, we are passing double object also as a parameter, because the error
message is a dynamic message. And if we want replace a value in place of
a parameter of message then we need to pass an object but not a
variable.
If the given salary is <: 8000 then we are assigning some bonus and this
bonus is printed in the browser by the success page. In order to transfer
the bounus value from Action. Class to jsp , we are using attributes api of
request object.
          DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 80
     80
                                                                           JAVA Means DURGASOFT
JSP:- durga.properties.file
# sathya.properties
Enter.name= username
Enter.sale=salary
Integer i=(integer)o;
Int r= i.intValue();
Out.println(bonus=+r %>
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 81
   81
                                                                            JAVA Means DURGASOFT
Upload.jsp
Select a file
Submit
P AF execute(----)
Formfile f=uf.getFf();
Enctype=multiplication/form-data>
<html.submit/>
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 82
    82
                                                                           JAVA Means DURGASOFT
</html:form>
forExample,we generally use java zoom API per uploading a file from client
side to serverside in awebapplication.
If we use third party ApIs then our application depends on third party
libraries.so we have to add those libraries in to our application lib folder
and as well as into the class path.
In struts fws a free defined class is provided for storing the uploaded file
path and its data called FarmFile.
1)in input jsp page, we should add enctype attribute for html.form with a
value multipart formdata.
In order to get a file text box that is a textbox with a browse button, we
need to use on html tag called <html.file property=ff/>In actionform
class,we need a property of type FormFile and then generate the setter
and getter per it.
setter
getter.
}
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 84
    84
                                                                              JAVA Means DURGASOFT
4)in action class of struts, we need get the formFile object and then by
using this methods of this class we need to read the uploadfile data.
Finally we need FileoutputStream per written the uploaded file date into a
serverside file.
Uploadform uf=(uploadForm)form;
FormFile f=uf.getFf()
Byte b[]=f.getFileDate();
String frame=f.getFileName();
Fos.write (b)
..
           DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 85
      85
                                                                            JAVA Means DURGASOFT
Step1:- open my eclipse ide and enter some work space name
UPLOADFORM.JAVA:-
Import
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 86
    86
                                                                            JAVA Means DURGASOFT
Return ff;
This.ff=ff;
UPLOAD ACTION:-
Public AF execute(.)
         DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 87
    87
                                                                            JAVA Means DURGASOFT
uploadForm uf=(uploadForm)form;
FormFile f=uf.getFf();
Byte b[]=f.getFileData();
fos.write(b);
return mapping.findforward(success);
<!upload.jsp
<center>
<html:submit valueUPLOAD/>
<html:form>
</center>
Success.jsp:-
Failure.jsp:-
<%out.println(<h2>upload is failed</h2>);%>
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 89
   89
                                                                           JAVA Means DURGASOFT
http:// localhost:2013/uploadExample/upload.jsp.</
whenever deployee the app then the tiie container will be created of
object of action servlet.
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 90
   90
                                                                           JAVA Means DURGASOFT
RESET() OF FORMBEAN:-
If we dont write reset() then setter methos will be called directly and
previous values are overridden with the new values.
The problem aries, when input page contains checkbox parameter. The
problem in if we dont select a check box then that parameter does not
come to server. So its related setter method will be not called. And hence
the previous values will not be overridden with the new value. So the
previously selected value remains same.
If the previous value is not overridden with new value then we will get
unexpected output. So inorder to overcome this problem, we need reset()
in the FormBean class.
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 91
   91
                                                                         JAVA Means DURGASOFT
 Exception are rised in Action class but not ActionForm becaz we are
  implementing b.login Action class.
 When we write throws Exception are can be handle that exception
 If really exception is raised in execute() of action class.
P AF execute(----)
Try
}catch (Exception) {}
}}
      DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 92
92
                                                                          JAVA Means DURGASOFT
Type=java.lang.Exception
       DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 93
  93
                                                                            JAVA Means DURGASOFT
<f..>
<f..>
</action>
<global-exception>
<exception key=key
Type=..
Path=/error.jsp/>
</global-exception>
<action mapping>
If an exception occurs then the error message should be taken from the
resource bundle and we need to define an error separately and that error
page displace error message by using<html:error/>
<f------/>
<f------/>
</action>
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 95
   95
                                                                           JAVA Means DURGASOFT
Ex:- <global-exception>
</global-exception>
For an action if both local and global forward are written then the
preference will be given for the <local-forwards>
Ex:- <global-forward>
</global-forward>
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 96
   96
                                                                           JAVA Means DURGASOFT
ValidatorFramework in Struts:
In order to apply input validations,we can use any one of the following
two approachs.
1)programatic approach.
2)Declarative approach.
In
        DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 97
   97
                                                                        JAVA Means DURGASOFT
     DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 98
98
                                                                        JAVA Means DURGASOFT
     DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786      Page 99
99