210 likes | 623 Views
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 .
E N D
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 • 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.
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.
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
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.
Standard Actions • Struts provides standard Actions • ForwardAction • SuccessAction • FindForwardAction • Forwards based on presence of parameter • DispatchAction • Method executed is determined by parameter • Requires code
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
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
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.
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 />
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
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
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
JNDI • Directory context • Java objects • Referenced by name • javax.naming.* • Naming • Aliases • java:comp/env/jdbc/mydb
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.
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
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