1 / 19

Struts

Struts. J2EE web application framework “ Model 2 ” Model View Controller Controller Servlet Key features XML metadata Struts taglib Simplified form validation. Tools for this course. Tomcat (J2EE server) Ant (build tool) NetBeans (IDE). A basic web application .

alaire
Download Presentation

Struts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Struts • J2EE web application framework • “Model 2” Model View Controller • Controller Servlet • Key features • XML metadata • Struts taglib • Simplified form validation

  2. Tools for this course • Tomcat (J2EE server) • Ant (build tool) • NetBeans (IDE)

  3. A basic web application • struts-config.xml in WEB-INF • application.properties • Several .jar files from Struts and other Jakarta open source projects • A modified web.xml, with taglib mappings for the Struts taglibs and possibly JSTL.

  4. Struts MVC with Actions • Request is intercepted by controller • Form is data captured in an ActionForm • Form is data validated in ActionForm • Controller calls execute() method of appropriate Action class. • Method returns ActionForward of view component.

  5. Struts flow

  6. Important Struts classes • ActionForm • Capture form data • Returns ActionErrors in validate() • Action • Process the request • Forward to appropriate resource • Returns ActionForward • ActionForward • Abstract reference to a resource

  7. The Action class • Extend org.apache.struts.action.Action • Implement execute() method. • Parameters: • ActionMapping • ActionForm • HttpServletRequest • HttpServletResponse • Thows java.lang.Exception • Returns an ActionForward • Controller forwards request to this resource.

  8. Standard Actions • Struts provides standard Actions • ForwardAction • SuccessAction • FindForwardAction • Forwards based on presence of parameter • DispatchAction • Method executed is determined by parameter • Requires code

  9. Action Lab • Review JSP pages in application • Tasks: • Create ActionMappings for JSPs in struts-config.xml • Define Actions in struts-config.xml • ForwardAction • SuccessAction • FindForwardAction • Create a subclass of DispatchAction

  10. ActionForms • ActionForms are used to collect and validate data from a request, typically generated by an HTML form. • ActionForms resemble Java beans. • Getters and setters • Both not required • Setters can expose data to inadvertent/malicious manipulation

  11. ActionForm validation • ActionForms have a validate() method. • If validation is requested, this method is called after the ActionForm is populated with the request data. • The validate() method returns an ActionErrors object, which is a collection of error messages • If form validation fails, the request is forwarded back to the input form. • Failure is determined by the presence of one or more ActionError objects in the ActionErrors collection. • If the input form was constructed using the appropriate Struts tags, the form data is filled in. • The Struts taglib can be used to display ActionError messages to the user.

  12. ActionErrors • The application.properties file defines the application’s error messages. • error.userid.required=You must enter a user id • Using the ActionErrors • ActionErrors errors = new ActionErrors(); • errors.add ("userid", new ActionError("error.userid.required")); • Use a tag on the JSP page containing the input form • <html:errors />

  13. ActionForm lab • Augment the FindForwardAction from Lab 1 with an Action that checks the user id and password • Action will forward to warning page if credentials are not valid • Create an ActionForm that will validate the login form data • Validation should fail if user id or password is blank

  14. Database access • Access to a database requires a Connection. • Web applications require many concurrent DB connections • Opening a DB connection is slow • Connection pooling • Request a Connection from the pool • Use the Connection • Return the Connection to the pool

  15. Where to put the pool? • Persistence code needs access to the connection pool • Where should the application store the pool? • The web application’s application scope? • Object with static members • JNDI

  16. JNDI • Directory context • Java objects • Referenced by name • javax.naming.* • Naming • Aliases • java:comp/env/jdbc/mydb

  17. JNDI setup • Modify Tomcat’s server.xml file • Commercial servers usually have web interface to create connection pools • Modify web.xml file • Add <resource-ref> element • Other J2EE servers may require an additional XML file.

  18. Using JNDI • Get InitialContext object • InitialContext ic = new InitialContext() • Look up the DataSource object • Object o = ic.lookup(“java:comp/env/jdbc/mydb”); • Cast the object to the required class • DataSource ds = (DataSource) o; • Combine the lookup and the cast in one line for more compact code. • Context operations will throw javax.naming.NamingException

  19. JNDI Lab • Check that necessary JAR files are in Tomcat’s common/lib directory • Modify Tomcat’s server.xml • Set correct DB URL • Set password and username • Modify web application’s web.xml • Edit index.jsp • Edit SQL query • Add code to fetch data from ResultSet • Add code to display data

More Related