Web Development with Apache Struts 2

4,129 views

Published on

Published in: Technology, Education
1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
4,129
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
129
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

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

×