Web Development with Apache Struts 2
-
Upload
fabriziogiudici -
Category
Technology
-
view
4.345 -
download
3
description
Transcript of Web Development with Apache Struts 2
![Page 1: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/1.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 1
Web Development withApache Struts 2
Fabrizio Giudici
Tidalwave sas, CEONetBeans Dream Team
Senior Java Architect, Blogger
www.tidalwave.itbluemarine.tidalwave.itweblogs.java.net/blog/fabriziogiudicistoppingdown.net
![Page 2: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/2.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 2
Agenda
● Java Web Frameworks● Struts basics● Struts 2● A small code example● Q&A
![Page 3: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/3.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 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
●
![Page 4: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/4.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 4
Java Web Frameworks
● Question: how many Java web frameworks are available?
– 1?
– 5?
– 10?
– Dozens?
![Page 5: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/5.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 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
![Page 6: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/6.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 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
![Page 7: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/7.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 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...
![Page 8: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/8.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 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
![Page 9: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/9.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 9
MVC
![Page 10: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/10.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 10
Struts 2.x benefits
● Actions are POJOs● Interceptors (from AOP)● Classes are now independent of HTTP● Simplified testing● Annotations, AJAX, Spring, Portlets,
etc...
![Page 11: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/11.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 11
Workflow
![Page 12: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/12.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 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
![Page 13: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/13.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 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>
![Page 14: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/14.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 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; }}
![Page 15: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/15.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 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>
![Page 16: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/16.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 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>
![Page 17: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/17.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 17
Interceptors
● Used to implement “common” behaviours
– Validations
– Multiple submit filters
– Logging
● Pre-defined interceptors
![Page 18: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/18.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 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>
![Page 19: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/19.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 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
![Page 20: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/20.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 20
Custom interceptors
public interface Interceptor extends Serializable { public void destroy();
public void init();
public String intercept (ActionInvocation inv) throws Exception; }
![Page 21: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/21.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 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>
![Page 22: Web Development with Apache Struts 2](https://reader034.fdocuments.in/reader034/viewer/2022052522/547b0d1cb479596d098b4d39/html5/thumbnails/22.jpg)
Nov 20, 2008 Web Development with Apache Struts 2 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/