Framework is a tool that provides common required features like Internalization, validation, form backup, Exceptions and supports in building applications quickly.
Structs is a MVC framework to build web application. It is an MVC framework and provides support to build web application UI and Controller layers. It is not useful for building Model
EJB Session beans & EJB entity beans
EJB Session beans & Hibernate
Spring & Hibernate
Spring & Spring ORM
JSP Model1:
Multiple form request need to map to one controller and controller will be jsp.
JSP Model2:
Multiple form request need to map to one controller and controller will be servlet.
JSP Model3:
Multiple form request need to map to one controller and controller will be filter.
JSP Model4:
Multiple form request need to map to one controller and controller will be tags.
Model 1 & 4 are not recommended and no frameworks are built using them.
Structs 1.x, JSF and Spring designed using JSP model2. The controller names is ActionServlet(Structs), FacesServlet(JSF), DispatcherServlet(Sprint MVC)
Structs 2.x designed using JSP model3. The filters are FilterDispatcher & StructsPrepareAndExecuteFilter
Default controllers(ActionServlet) will support common required features like form backup support, exceptions and etc. For user specific operations one should provide user specific controllers.
ActionServlet has some helper classes as well. Using ActionServlet and helper classes, we can achieve all the features.
Architecture:
Directory structure for Structs:
JSP Model architecture rules:
<structs-config>
<form-beans>
<form-bean name="<bean ref name>" type="<class-name>" </form-bean>
<form-bean name="EMP" type="pkg1.hello" </form-bean>
</form-beans>
<action-mappings>
<action path="<form action>" name="<bean ref name>" input="/index.jsp" </action>
<action path="/hello" name="EMP" validate="true" input="/index.jsp" </action>
</action-mappings>
<message-resources> parameter="Messages" </message-resources> //file name without extn
</structs-config>
Structs Flow of Control
Programmatic - ActionForm, DynaActionForm: These can support application level(server side) validations & form backup support
Declarative - ValidatorForm, DynaValidatorForm, ValidatorActionForm, DynaValidatorActionForm : These can support client side validation, application level(server side) validations , form backup & Localization support
ValidatorForm, DynaValidatorForm are for single form operations
ValidatorActionForm, DynaValidatorActionForm are for multi form operations
How does Validations execute in case of Declartive approach:
Application using ValidatorForm
Components required:
<form-validation>
<formset>
<form name="RF">
<field name="id" depends="intRange">
<arg key="id">
<arg key="${var.min}>
<arg key="${var.max}>
<var>
<var-name>min</var-name>
<var-value>111</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>200</var-value>
</var>
<structs-config>
<form-beans>
<form-bean name="<bean ref name>" type="<class-name>" </form-bean>
<form-bean name="EMP" type="pkg1.hello" </form-bean>
</form-beans>
<action-mappings>
<action path="<form action>" name="<bean ref name>" input="/index.jsp" </action>
<action path="/hello" name="EMP" validate="true" input="/index.jsp" </action>
</action-mappings>
<plug-in classname="validatorplugin">
<set-property property="pathnames"
value="validation-rules.xml, empvalidation.xml"/>
</set-property
</plug-in>
<message-resources> parameter="Messages" </message-resources> //file name without extn
</structs-config>
Structs is a MVC framework to build web application. It is an MVC framework and provides support to build web application UI and Controller layers. It is not useful for building Model
- View Layer/presentation support
- Form tags //to provide validation and form backup support
- It provides Form beans and Validator classes
- I18N tags //Multiple language support
- Logical tags //
- Tile tags
- Ajax tags in Structs 2.0
- OGNL (Object Graph Notation language) in Structs 2.0
- Controller support
- Multi action contollers
- Multi button controllers
- Multi form controllers
- Exception handler support
- plugin support -- to integrate with Model layer
- Pre handling support
- Post handling (interceptors) support(In structs 2.0)
- I18N controller classes //to change browser language
EJB Session beans & EJB entity beans
EJB Session beans & Hibernate
Spring & Hibernate
Spring & Spring ORM
JSP Model1:
Multiple form request need to map to one controller and controller will be jsp.
JSP Model2:
Multiple form request need to map to one controller and controller will be servlet.
JSP Model3:
Multiple form request need to map to one controller and controller will be filter.
JSP Model4:
Multiple form request need to map to one controller and controller will be tags.
Model 1 & 4 are not recommended and no frameworks are built using them.
Structs 1.x, JSF and Spring designed using JSP model2. The controller names is ActionServlet(Structs), FacesServlet(JSF), DispatcherServlet(Sprint MVC)
Structs 2.x designed using JSP model3. The filters are FilterDispatcher & StructsPrepareAndExecuteFilter
Default controllers(ActionServlet) will support common required features like form backup support, exceptions and etc. For user specific operations one should provide user specific controllers.
ActionServlet has some helper classes as well. Using ActionServlet and helper classes, we can achieve all the features.
Architecture:
Directory structure for Structs:
- Application Folder -> Its name will be application name.
- view resources -> html, css, java script, jsp, image, audio & video
- WEB-INF -> folder
- web.xml -> Configure action servlet (Front controller) & and init parameter Config with structs-config.xml as value
- structs-config.xml
- classes -> folder. This will have servlet classes & Model(jdbc, spring, simple java) classes
- lib -> folder -> additional api's. like file upload api, spring related jar files.
JSP Model architecture rules:
- Multiple input form requests need to be mapped to single controller. The controller should be Servlet. (ActionServlet is the controller in Structs 1.x). ActionServlet reads structs-config.xml to find bean and user defined Controller classes for the form request.
- ActionServlet will read the request object data and hold the values in Bean Object. (User need not read from request object as required in Servlets/jsps)
- After storing the values in bean object, it will execute the validations on bean data. If it finds validation errors, it will return to ActionServlet. ActionServlet inturn return and reflect the same on input pages.
- ActionServlet then invokes user defined Controller by passing Bean, Request and Response objects. User defined Controller will in turn invoke model for execution and it can pass the Bean reference as required. (For Controller to pass bean to Model, we need DTO object(a simple serializable object). DTO will be sent to Model. Model reads DTO and performs business operations. After completion of Business operations to hold data into database table the data should be sent to DAO. For sending to DAO the data need to be placed/moved to Model object. Model object can be persisted by using Hibernate.)
- User defined Controller just returns Success/Fail to the ActionServlet. ActionServlet will use the value and then invoke respective jsp defined in structs-config.xml for the return value. The output of jsp will be returned as output for the form request.
- Create dynamic web project, add structs jar to lib folder
- Create index.jsp with action="hello"
- Create HelloBean extends ActionForm class (Action form classes are ActionForm/DynaActionForm/ValidatorForm/DynaValidatorForm/ValidationActionForm/DynaValidationActionForm)
- Create HelloController extends Action Class (Action, DispatchAction, LookupDispatchAction, MappingDispatchAction, EventDispatchAction)
- web.xml - > Configure FrontController class i.e ActionServlet org.apache.structs.action.ActionServlet
- Add following mappings in structs-config.xml
- Input form to bean
- Bean to input form
- Bean to controller
- Controller to success/failure page
- properties file for error messages
<structs-config>
<form-beans>
<form-bean name="<bean ref name>" type="<class-name>" </form-bean>
<form-bean name="EMP" type="pkg1.hello" </form-bean>
</form-beans>
<action-mappings>
<action path="<form action>" name="<bean ref name>" input="/index.jsp" </action>
<action path="/hello" name="EMP" validate="true" input="/index.jsp" </action>
</action-mappings>
<message-resources> parameter="Messages" </message-resources> //file name without extn
</structs-config>
Structs Flow of Control
- On first user form request, ActionServlet will be instantiated by calling init(ServletConfig).
- init() reads the location of servlet configuration file(structs-config.xml) from config object, and structs-config.xml file data will be loaded into ModuleConfig object
- After init(), either doGet() or doPost() method on ActionServlet will be called. They inturn delegate redirect it to Process(request,response)
- The Process(), creates form reference Bean object as declared in structs-config file. The bean object will be stored in session scope.
- Process() will then create/get RequestProcessor object by calling RequestProcessorUtil(ModuleConfig) factory method
- Process() inturn delegates the control to RequestProcessor.process(req,res)
- RequestProcessor.process() calls following methods
- processPreProcess(req,res) where security checks can be performed
- processLocale(req, res). This prepare Locate object with Browser language value. The locale object will be stored in session object.
- processFormBean(req,res) - This will store request scope data in Bean Object.
- processValidation(req,res) - This validates the data by calling validate() on bean. This method returns ActionErrors object. If ActionErrors object has data then it redirects control to same page using requestDispatcher.Forward. In the page, if the bean has valid data it will populate other wise it will just clear the value for the invalid data field. Along with the bean data it will show ActionErrors data.
- (ProcessController) If Validation is successful, it creates Controller object configured in structs-config file. The execute() in controller class will be executed by passing Bean object as parameter. This method returns ActionForward object to RequestProcessor. Based on ActionForward defined in structs-config file, the request processor returns the output to user.
Programmatic - ActionForm, DynaActionForm: These can support application level(server side) validations & form backup support
Declarative - ValidatorForm, DynaValidatorForm, ValidatorActionForm, DynaValidatorActionForm : These can support client side validation, application level(server side) validations , form backup & Localization support
ValidatorForm, DynaValidatorForm are for single form operations
ValidatorActionForm, DynaValidatorActionForm are for multi form operations
How does Validations execute in case of Declartive approach:
- Create validation.xml for every input file
- predefined validations are given under validation-rules.xml file
- Validator plugin to read the validation files
- Validator plugin will be started by Action Servlet init(). It will create object of Validator calls init() on Validator plugin class. The validations will be stored in ServletContext scope object.
- RequestProcessor stores data in bean and then executes validations stored in Context scope.
- Advantage with validation.xml files is that, they generate java script validation. This will help reduce network traffic between server and browser.
Application using ValidatorForm
Components required:
- index.jsp -
- success.jsp
- bean extending from ValidatorForm
- Messages.properties
- validation.xml
- validator-rules.xml
- web.xml
- structs-config.xml
- controller by extending from Action class
<form-validation>
<formset>
<form name="RF">
<field name="id" depends="intRange">
<arg key="id">
<arg key="${var.min}>
<arg key="${var.max}>
<var>
<var-name>min</var-name>
<var-value>111</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>200</var-value>
</var>
<structs-config>
<form-beans>
<form-bean name="<bean ref name>" type="<class-name>" </form-bean>
<form-bean name="EMP" type="pkg1.hello" </form-bean>
</form-beans>
<action-mappings>
<action path="<form action>" name="<bean ref name>" input="/index.jsp" </action>
<action path="/hello" name="EMP" validate="true" input="/index.jsp" </action>
</action-mappings>
<plug-in classname="validatorplugin">
<set-property property="pathnames"
value="validation-rules.xml, empvalidation.xml"/>
</set-property
</plug-in>
<message-resources> parameter="Messages" </message-resources> //file name without extn
</structs-config>
No comments:
Post a Comment