Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
Web Development with
                                Apache Struts 2

                                Fabrizio Giudici

  ...
Agenda
               ●   Java Web Frameworks
               ●   Struts basics
               ●   Struts 2
               ...
NetBeans 6.5 is out
               ●   Get it while it's hot! www.netbeans.org
                      –   Faster!
         ...
Java Web Frameworks
               ●   Question: how many Java web
                   frameworks are available?
          ...
Java Web Frameworks
               ●   Answer: more than 50!
                       –   www.manageability.org/blog/stuff/h...
My (subjective) take
               ●   Wicket
                       –   You really want to be agile
                    ...
JEE
               ●   JEE Web components:
                      –   “Foundation”: Servlet, JSP, Filter
                  ...
Struts
               ●   Struts 1 appeared in June 2001
                       –   Apache License
                       ...
MVC




Nov 20, 2008   Web Development with Apache Struts 2     9
Struts 2.x benefits
               ●   Actions are POJOs
               ●   Interceptors (from AOP)
               ●   Cla...
Workflow




Nov 20, 2008   Web Development with Apache Struts 2          11
Components
               ●   web.xml
                      –   Installs a Filter on /*
               ●   struts.xml
    ...
web.xml
               <?xml version="1.0" encoding="UTF-8"?>
               <web-app ... >
                   <filter>
  ...
A simple Action
         import com.opensymphony.xwork2.ActionSupport;

         public class MyAction extends ActionSuppo...
struts.xml


               <struts>

                  <package name="/" extends="struts-default">
                    <a...
A simple view JSP
               <%@page contentType="text/html" pageEncoding="UTF-8"%>
               <%@taglib prefix="s...
Interceptors
               ●   Used to implement “common”
                   behaviours
                      –   Validat...
Formal validation
                 ●    Declarative, save tons of code

               <validators>

                     ...
Interceptors
               ●   Code that is invoked across Actions
               ●   Many pre-defined interceptors
     ...
Custom interceptors


               public interface Interceptor
                 extends Serializable
                 {...
Interceptors in
                                                            struts.xml

               <struts>

         ...
Conclusion
               ●   Robust, Struts 1 heritage
               ●   Keeps up with the latest standards
            ...
Nächste SlideShare
Wird geladen in …5
×

Web Development with Apache Struts 2

4.305 Aufrufe

Veröffentlicht am

Veröffentlicht in: Technologie, Bildung
  • Loggen Sie sich ein, um Kommentare anzuzeigen.

Web Development with Apache Struts 2

  1. 1. Web Development with Apache Struts 2 Fabrizio Giudici Tidalwave sas, CEO NetBeans Dream Team Senior Java Architect, Blogger www.tidalwave.it bluemarine.tidalwave.it weblogs.java.net/blog/fabriziogiudici stoppingdown.net Nov 20, 2008 Web Development with Apache Struts 2 1
  2. 2. Agenda ● Java Web Frameworks ● Struts basics ● Struts 2 ● A small code example ● Q&A Nov 20, 2008 Web Development with Apache Struts 2 2
  3. 3. NetBeans 6.5 is out ● Get it while it's hot! www.netbeans.org – Faster! – Compile-on-save, multithreading Java debugger, visual deadlock indication – PHP support, JavaScript debugger and library manager, better Spring / Hibernate / JSF / JPA support – RESTful web services, SQL editor improvements, JavaFX, Groovy and Grails, Ruby and Rails improvements, GlassFish v3 ● Nov 20, 2008 Web Development with Apache Struts 2 3
  4. 4. Java Web Frameworks ● Question: how many Java web frameworks are available? – 1? – 5? – 10? – Dozens? Nov 20, 2008 Web Development with Apache Struts 2 4
  5. 5. Java Web Frameworks ● Answer: more than 50! – www.manageability.org/blog/stuff/how- many-java-web-frameworks ● Of course, those with a significant spread are not so many ● But choosing is difficult ● NIH, but also radically different approaches Nov 20, 2008 Web Development with Apache Struts 2 5
  6. 6. My (subjective) take ● Wicket – You really want to be agile – You routinely live on the leading edge ● Tapestry – You like agile, but consolidated ● Struts – You are “conservative” and like it easy ● Java Server Faces – You love visual designers – You can survive to high complexity Nov 20, 2008 Web Development with Apache Struts 2 6
  7. 7. JEE ● JEE Web components: – “Foundation”: Servlet, JSP, Filter – “High level”: JSF ● Foundation elements are enough, but you're going to write tons of code – Validation, flow control, etc... Nov 20, 2008 Web Development with Apache Struts 2 7
  8. 8. Struts ● Struts 1 appeared in June 2001 – Apache License – Strictly based on MVC pattern – Supported declarative validation, flow control ● Struts 2 is the “merge” of Struts 1 + WebWork Nov 20, 2008 Web Development with Apache Struts 2 8
  9. 9. MVC Nov 20, 2008 Web Development with Apache Struts 2 9
  10. 10. Struts 2.x benefits ● Actions are POJOs ● Interceptors (from AOP) ● Classes are now independent of HTTP ● Simplified testing ● Annotations, AJAX, Spring, Portlets, etc... Nov 20, 2008 Web Development with Apache Struts 2 10
  11. 11. Workflow Nov 20, 2008 Web Development with Apache Struts 2 11
  12. 12. Components ● web.xml – Installs a Filter on /* ● struts.xml – Maps Action names – Defines the navigation flow ● Actions – Execute a task ● Views (JSP or others) – Render the UI Nov 20, 2008 Web Development with Apache Struts 2 12
  13. 13. web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app ... > <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ... </web-app> Nov 20, 2008 Web Development with Apache Struts 2 13
  14. 14. A simple Action import com.opensymphony.xwork2.ActionSupport; public class MyAction extends ActionSupport { private final List<String> NBDTers = Arrays.asList(...); private String userName; private String message; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getMessage() { return message; } @Override public String execute() { message = userName; return NBDTers.contains(userName) ? "nbdt" : SUCCESS; } } Nov 20, 2008 Web Development with Apache Struts 2 14
  15. 15. struts.xml <struts> <package name="/" extends="struts-default"> <action name="MyAction" class="myexample.MyAction"> <result name="input">/index.jsp</result> <result name="success">/good.jsp</result> <result name="nbdt">/nbdt.jsp</result> </action> </package> </struts> Nov 20, 2008 Web Development with Apache Struts 2 15
  16. 16. A simple view JSP <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <html> <body> <h2>Welcome to JUG Lugano</h2> Hello, <s:property value="message" default="Guest" />, welcome to the first meeting of JUG Lugano! <s:form method="GET" action="MyAction.action"> Would you be so kind to tell me your name? <s:textfield name="userName" /> <s:submit value="Submit" /> </s:form> <s:actionerror/> <s:fielderror/> </body> </html> Nov 20, 2008 Web Development with Apache Struts 2 16
  17. 17. Interceptors ● Used to implement “common” behaviours – Validations – Multiple submit filters – Logging ● Pre-defined interceptors Nov 20, 2008 Web Development with Apache Struts 2 17
  18. 18. Formal validation ● Declarative, save tons of code <validators> <field name="userName"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>Your name is required.</message> </field-validator> </field> </validators> Nov 20, 2008 Web Development with Apache Struts 2 18
  19. 19. Interceptors ● Code that is invoked across Actions ● Many pre-defined interceptors – Parameter rename, cookie management, component behaviours (e.g. Checkboxes), background actions – Validation itself is an interceptor Nov 20, 2008 Web Development with Apache Struts 2 19
  20. 20. Custom interceptors public interface Interceptor extends Serializable { public void destroy(); public void init(); public String intercept (ActionInvocation inv) throws Exception; } Nov 20, 2008 Web Development with Apache Struts 2 20
  21. 21. Interceptors in struts.xml <struts> <package name="/" extends="struts-default"> <interceptors> <interceptor name="logger" class="..."/> </interceptors> <action name="MyAction" class="myexample.MyAction"> <interceptor-ref name="logger"/> <result name="success">/good.jsp</result> <result name="nbdt">/nbdt.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts> Nov 20, 2008 Web Development with Apache Struts 2 21
  22. 22. Conclusion ● Robust, Struts 1 heritage ● Keeps up with the latest standards (POJOs, AOP, annotations, Spring, ...) ● “Conservative” approach, but easy ● Relevant sites: – struts.apache.org – beans.seartipy.com/2008/08/04/struts- 2-plugin-for-netbeans-ide- nbstruts2support/ Nov 20, 2008 Web Development with Apache Struts 2 22

×