Showing posts with label Struts. Show all posts
Showing posts with label Struts. Show all posts

Advantages of Spring MVC over Struts MVC

1. There is clear separation between models, views and controllers in Spring.
2. Spring’s MVC is very versatile and flexible based on interfaces but Struts forces Actions and Form object into concrete inheritance.
3. Spring provides both interceptors and controllers, thus helps to factor out common behavior to the handling of many requests.
4. Spring can be configured with different view technologies like Freemarker, JSP, Tiles, Velocity, XLST etc. and also you can create your own custom view mechanism by implementing Spring View interface.
5. In Spring MVC Controllers can be configured using DI (IOC) that makes its testing and integration easy.
6. Web tier of Spring MVC is easy to test than Struts web tier, because of the avoidance of forced concrete inheritance and explicit dependence of controllers on the dispatcher servlet.
7. Spring web layer built on top of a business object layer which is considered a good practice. In Struts framework you need to implement your business objects .
8. Struts forces your Controllers to extend a Struts class but Spring doesn’t, there are many convenience Controller implementations that you can choose to extend.
9. In struts a variety of Struts specific tags are used to assure that request parameters are bound to ActionForm fields and show binding/validation errors. In SpringMVC, there is one simple bind tag that handles all of this. Your JSP’s remain smaller and have more pure HTML content.
10. In Struts, Actions are coupled to the view by defining ActionForwards within a ActionMapping or globally. SpringMVC has HandlerMapping interface to support this functionality.
11. With Struts, validation is usually performed (implemented) in the validate method of an ActionForm. In SpringMVC, validators are business objects that are NOT dependent on the Servlet API which makes these validators to be reused in your business logic before persisting a domain object to a database.
Click here to View more...

Struts bean:write format attribute

I absolutely love the format and formatKey attributes of bean:write tag in Struts framework. You can use these attributes to format the display of your form bean properties. This works pretty well in displaying currency and date/time values. For example, you can display 1234.567 as $1,234.57 and 08/12/2006 as Aug 12, 2006. Here is how it works:
 


<bean:write name="myForm" property="amount" format="$#,000.00" />

OR

<bean:write name="myForm" property="amount" formatKey="myapp.currency.format" />

AND

myapp.currency.format=$#,000.00 (in resource bundle)



 
The preferred way is to use the second format because it makes the code portable. Plus, if you want to change the format, you can make the change in resource bundle without having to worry about changing all your JSPs.
 
So far so good. When I tried to use this feature in one of my recent projects, it didn't work! Why? Because the type of the property being formatted cannot be String. The format attribute simply uses default format (display the property as is) when the property is of type java.lang.String. In other words,
private double amount; // will be formatted correctly
private String amount; // will NOT be formatted at all
 
This could be a feature or a limitation but I haven't been able to figure out why String shouldn't work! Why do I expect the format attribute to do the type conversion? Because the framework automatically does the type conversion on form submission. Here is what I mean by that:
 
// action form code
public class MyForm extends ActionForm{
private double amount;
}
 
// jsp code
<html:text name="myForm" property="amount" /> 
 
Even though request.getParameter("amount") returns a String, the framework happily converts the String to double. If this conversion is automatic, why can't it convert String to double and try to display the value in currency format in the format attribute? I am sure this has been raised/discussed/considered somewhere but I can't find it. If some one knows let me know.
 
There is another problem (not really a Struts problem though). There is no way to format phone numbers. If you get a String 8001234567 from the database and would like to convert to (800) 123-4567 OR 800-123-4567, currently there is no tag or attribute to do that (or I don't know about it). I was hoping, since it's so easy to format currency and date objects, it would be easy to format phone numbers but looks like it's my challenge to write something like that.

Struts 2 in Action
Practical Apache Struts 2 Web 2.0 Projects (Practical Projects)Struts: The Complete Reference, 2nd Edition (Complete Reference Series)
Click here to View more...

Flow of Struts Appication

Struts Flow start with ActionServlet then call to process() method of RequestProcessor.

Step 1. Load ActionServlet using load-on-startup and do the following tasks.

Any struts web application contain the ActionServlet configuration in web.xml file.
On load-on-startup the servlet container Instantiate the ActionServlet .
First Task by ActionServlet : The ActionServlet takes the Struts Config file name as an init-param.
At startup, in the init() method, the ActionServlet reads the Struts Config file and load into memory.
Second Task by ActionServlet : If the user types http://localhost:8080/app/submitForm.do in the browser URL bar, the URL will be intercepted and processed by the ActionServlet since the URL has a pattern *.do, with a suffix of "do". Because servlet-mapping is

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


Third Task by ActionServlet : Then ActionServlet delegates the request handling to another class called RequestProcessor by invoking its process() method.

<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

Step 2. ActionServlet calls process() method of RequestProcessor.

The RequestProcessor does the following in its process() method:
a) The RequestProcessor looks up the configuration file for the URL pattern /submitForm (if the URL is http://localhost:8080/app/submitForm.do). and and finds the XML block (ActionMapping).

ActionMapping from struts-config.xml

<action path="/submitForm"
type="com.techfaq.emp.EmpAction"
name="EmpForm"
scope="request"
validate="true"
input="EmpForm.jsp">

<forward name="success" path="success.jsp"/>
<forward name="failure" path="failure.jsp" />
</action>


b) The RequestProcessor instantiates the EmpForm and puts it in appropriate scope – either session or request.
The RequestProcessor determines the appropriate scope by looking at the scope attribute in the same ActionMapping.
c) RequestProcessor iterates through the HTTP request parameters and populates the EmpForm.
d) the RequestProcessor checks for the validateattribute in the ActionMapping.
If the validate is set to true, the RequestProcessor invokes the validate() method on the EmpForm instance.
This is the method where you can put all the html form data validations.
If Validate fail the RequestProcessor looks for the input attribute and return to JSP page mentioned in input tag.
If Validate pass goto next step.
e) The RequestProcessor instantiates the Action class specified in the ActionMapping (EmpAction) and invokes the execute() method on the EmpAction instance.

signature of the execute method is

public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
//your logic
return mapping.findForward("success");
}

f) In return mapping.findForward("success")
RequestProcessor looks for the success attribute and forward to JSP page mentioned in success tag. i.e success.jsp.
In return mapping.findForward("failure")
RequestProcessor looks for the failure attribute and forward to JSP page mentioned in failure tag. i.e. failure.jsp

Click here to View more...

How Struts Work?

1) When a user submitted a jsp page. that page having
(attribute of )action="login.do". the container will call
to WEB.XML. in that web.xml thert is two section servlet
And servlet mapping

2) In servlet mapping it find *.do in the url-pattern. if
it found to take the name of servlet. and check the
corresponding class. in the servlet section. that class is
ActionServlet.

3) ActionServlet is the controller of Struts module
architecture. in Action servlet having the service method.
in that method we create RequestPrecessor class instance

4) Service(req,res)
RequestPrecessor rp = new RequestPrecessor();

5) We call a process method of RequestProcessor class
through the instance rp.process(req,res)

6) In the request processor class have the process method
with the parameter of req,res. then it has 1 if condition
in this class. that condition return always true. because
that is dummy method.

7)Inside that condition ther is 6 steps are processing
a)Create a action mapping instance in the "Struts-
Config.xml". it will kept all details of the action mapping
path, value, type forward, validation=true/false, input
="*.jsp" etc these r created instance
b)Then it will create Form class instance before it check
the name of action mapping and form name are coincidence or
not if it same it will create form instance
c)Then it will go to ActionMapping instace the ris mention
or not the validate =true/fale if false it will not execute
the this step else it will execute this step.
d) Then it will create action instance
e) Next it will take four parameters of execute Method it
will return ActionErrors instance. if it is not empty. it
will go to error page other wise it will got to
corresponding page. else if it is empty if will go further
and display corresponding value of page in jsp view.This is
struts flow.
Click here to View more...

Difference between JSF vs Struts

Struts is an open-source Java web application framework whose architecture is based on the Model-View-Controller design pattern in which requests are routed through a controller that provides overall application management and dispatches the requests to application components. JavaServer Faces technology is a user-interface framework for Java web applications. It is focussed on the view tier of an MVC-based architecture. The Struts and JavaServer Faces technology frameworks do have some overlapping functionality; however each framework has its advantages, and developers can use certain features of both frameworks in a single application.

The primary advantages of Struts as compared to JavaServer Faces technology are as follows:

  • Because Struts is a web application framework, it has a more sophisticated controller architecture than does JavaServer Faces technology. It is more sophisticated partly because the application developer can access the controller by creating an Action object that can integrate with the controller, whereas JavaServer Faces technology does not allow access to the controller. In addition, the Struts controller can do things like access control on each Action based on user roles. This functionality is not provided by JavaServer Faces technology.
  • Struts includes a powerful layout management framework, called Tiles, which allows you to create templates that you can reuse across multiple pages, thus enabling you to establish an overall look-and-feel for an application.
  • The Struts validation framework includes a larger set of standard validators, which automatically generate both server-side and client-side validation code based on a set of rules in a configuration file. You can also create custom validators and easily include them in your application by adding definitions of them in your configuration file.



The greatest advantage that JavaServer Faces technology has over Struts is its flexible, extensible UI component model, which includes:

  • A standard component API for specifying the state and behavior of a wide range of components, including simple components, such as input fields, and more complex components, such as scrollable data tables. Developers can also create their own components based on these APIs, and many third parties have already done so and have made their component libraries publicly available.
  • A separate rendering model that defines how to render the components in various ways. For example, a component used for selecting an item from a list can be rendered as a menu or a set of radio buttons.
  • An event and listener model that defines how to handle events generated by activating a component, such as what to do when a user clicks a button.
  • Conversion and validation models for converting and validating component data.

Because the JavaServer Faces technology architecture separates the definition of a component from its rendering, you can render your components in different ways or even to different clients, such as a WML client. Moreover, the extensible component APIs of JavaServer Faces technology allow you to extend the standard set of components and create entirely new components. None of this is possible with Struts. In fact, Struts has no notion of server-side components, which also means that it has no event model for responding to component events and no facility for saving and restoring component state. While Struts does have a useful tag library for rendering components on the page, these components have no object representation on the server and they can only be rendered to an HTML client.

Another distinct advantage of JavaServer Faces technology is that it is standard, which means that it has been developed through the Java Community Process (JCP) and has been designed to allow easy integration into tools. As a result, JavaServer Faces technology already has wide industry support and is being leveraged by several web application development IDEs (such as Sun Java Studio Creator).

Because both JavaServer Faces technology and Struts contribute such valuable features, developers might want to be able to use both of them in a single application. Developers might want to integrate the flexible component model of JavaServer Faces technology into their existing Struts applications while continuing to use the Struts controller architecture. Similarly, developers who have JavaServer Faces technology applications might want to integrate the more powerful client-side validation mechanism and Tiles layout framework found in the Struts architecture into their applications. These goals can be accomplished by using the stuts-faces integration library, which you can download from here.




Click here to View more...

General Tutorials, Java technologies

Java security in web applicationThis article gives an overview about different attack mechanisms against Java web applications and J2EE applications. It introduces available security concepts in Java like JAAS and Sandbox security. Furthermore, security related design patterns.
Date: 06-03-07
Log4j tutorial with Tomcat examplesThis tutorial explains how to set up log4j with email, files and stdout. It compares XML to properties configuration files, shows how to change LogLevels for a running application. Furthermore, we explain best practices on logging and exception handling.
Date: 06-03-07
Tips and tricks for eclipse and the plugin MyEclipseIn this Tutorial we present tips and trick for the development enviroment eclipse and the extension MyEclipse.
Date: 28-08-06
Java EncyclopaediaFirst pages of an encyclopaedia including javas common abbreviations and technolgies. We try to explain short and easily to understand the terms of the java world.
Date: 28-08-06
MyEclipse CSS editorThis paper describes the new and improved editors of MyEclipse for CSS files.
Date: 29-08-06
MyEclipse JavaScript editorThis paper describes the new and improved editors of MyEclipse for JavaScript and CSS files.
Date: 29-08-06

Struts, JSP and Servlets Tutorials

JavaServer Faces Tutorials

First Java Server Faces TutorialThis tutorial facilitates the first steps with the quite new framework JavaServer Faces (JSF). Step by step an example application (a library) will be created, which illustrate the different elements of the framework.
Date: 28-08-06
JavaServer Faces Navigation Tutorial (?)This tutorial explain the navigation handling in JSF and shows the usage with an small example application. We will show multiple and complex examples for navigation rules. For examples navigation outcomes depending on application logic, actions, static n
Date: 29-08-06
JavaServer Faces - Converter TutorialThis tutorial explains the usage of converters in JSF. It shows how you can use standard converters step by step using an example application.
Date: 29-08-06
JavaServer Faces - Validation & Error Handling (€)This tutorial explains the validation and the error handling in JSF and shows a step by step example application. The tutorials shows the standard validators but also the creation of custom validators.
Date: 12-01-08
JavaServer Faces - Developing custom converters (€)This tutorial explains how to develop your own converters. It shows the usage of own custom converter tags and overriding standard converter of basic types.
Date: 12-01-08
JavaServer Faces - Message ResourcesThis tutorial explains the internationalization of a web application using JSF message resource bundle.
Date: 12-01-08

Debugging, Testing and Tuning Tutorials

Debugging from JSP and Java ApplicationsThis tutorial gives you an overview of how to use the debugging feature of eclipse to debug your web or Java projects.
Date: 28-08-06
Eclipse Junit testing tutorialThis tutorial gives you an overview of the features of JUnit and shows a little example how you can write tests for your Java application using eclipse.
Date: 29-08-06
Junit Testing with Struts (StrutsTestCases extension)This tutorial gives you an overview about the StrutsTestCases extension of JUnit and example how you can use it to test your Struts application.
Date: 29-08-06
Junit Testing Enterprise Java Beans (EJB) (?)This tutorial will demonstrate how to use Junit to unit test Enterprise JavaBeans.
Date: 29-08-06


Database Development, EJB and Hibernate Tutorials


Jboss Tutorials

Jboss as windows serviceInstallation of Jboss as windows service on windows 2000 and windows XP
Date: 28-08-06
Click here to View more...