Docsity

Struts Basics Part 4-Introduction to Java Programming-Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Struts Basics Part 4-Introduction to Java Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!

execute(..) method of Action class

(Struts 1.1 only)

 Invoked by controller

public ActionForward execute(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws Exception;

perform(..) method of Action class

(Struts 1.0 only)

 Deprecated

public ActionForward perform(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException;

Example: Action Class

1.package submit; import javax.servlet.http.; import org.apache.struts.action.; public final class SubmitAction extends Action {  public ActionForward execute(ActionMapping mapping,  ActionForm form,  HttpServletRequest request,  HttpServletResponse response) {  SubmitForm f = (SubmitForm) form; // get the form bean  // and take the last name value  String lastName = f.getLastName();  // Translate the name to upper case

  1. //and save it in the request object  request.setAttribute("lastName", lastName.toUpperCase());   // Forward control to the specified success target  return (mapping.findForward("success"));  } } docsity.com

Design Guidelines of Action Class

 The controller Servlet creates only one instance of

your Action class, and uses it for all requests

 Action class has to be in multi-threaded safe  Use local variables (as opposed to instanace variables)

 Make Action class a thin layer

 Use Model components for complex business logic handling

struts-config.xml: ActionMapping

Pre-built Action Classes

 ForwardAction

 DispatchAction

 We will learn more about these in Advanced Struts

session

Model Components

 Model divided into concepts

 Internal state of the system  Business logic that can change that state

 Internal state of system represented by

 JavaBeans  Enterprise JavaBeans  POJO's  JDO  JDBC  Whatever

source: Chuck Cavaness

docsity.com

 `

source: Chuck Cavaness

View Components

 JSP files which you write for your specific application

 Set of JSP custom tag libraries

 Resource files for internationalization

 Allows for fast creation of forms for an application

 Works in concert with the controller Servlet

Forms and FormBean Interactions

 If the user makes an error, the application should

allow them to fix just what needs to be changed

 With just JSP, you have to do

 With Struts, you can do

;

Example: submit.jsp

**1.<%@ page language="java" %> 2.<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> 3.<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> 4.<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

2.Submit example

1.Example Submit Page

2.Last Name: 3.Address: 4.Sex: Male**

5. Female

**6.Married: 7.Age: **

  • ** 0 - 19
  1. 20 - 49
  2. 50 -
  3. **

1. docsity.com

web.xml

Web App Deployment Descriptor

(web.xml)

 Struts application is a Web application

 Follows the same rule  Has to have web.xml deployment descriptor

 web.xml includes:

 Configure ActionServlet instance and mapping  Resource file as  servlet-config.xml file as  Define the Struts tag libraries

 web.xml is stored in WEB-INF/web.xml