Slide 1

Slide 1 text

Java Web Development with Stripes Samuel Santos

Slide 2

Slide 2 text

About me • Senior Java Engineer and Web Advocate at Present Technologies • Open source enthusiast • Web standards contributor • Casual blogger Present Technologies 2

Slide 3

Slide 3 text

Agenda • Why • What is it • Goals • Setting up • Features • Extensions • Find more • Q&A Present Technologies 3

Slide 4

Slide 4 text

Why “Java web development doesn’t have to suck.” Tim Fennell, Stripes author Present Technologies 4

Slide 5

Slide 5 text

Why “Have you ever used a framework and felt you had to do too much work for the framework compared to what the framework gave you in return?” Freddy Daoud, Stripes Book Present Technologies 5

Slide 6

Slide 6 text

What is it • Stripes is a Model-View-Controller (MVC) framework • Stripes is not a “full-stack” framework • Stripes is an action-based framework Present Technologies 6

Slide 7

Slide 7 text

Goals • Make developing web applications in Java easy • Provide simple yet powerful solutions to common problems • Make the Stripes ramp up time for a new developer less than 30 minutes • Make it really easy to extend Stripes, without making you configure every last thing From Stripes Homepage Present Technologies 7

Slide 8

Slide 8 text

Setting up Present Technologies 8 StripesFilter net.sourceforge.stripes.controller.StripesFilter ActionResolver.Packages com.example.javapt09.stripes.action DispatcherServlet net.sourceforge.stripes.controller.DispatcherServlet 1 StripesFilter DispatcherServlet REQUEST FORWARD ERROR DispatcherServlet *.action

Slide 9

Slide 9 text

Features - Smart binding URLs binding Present Technologies 9 public class SmartBindingActionBean extends BaseActionBean { ... } Smart binding Smart binding

Slide 10

Slide 10 text

Parameters And Events

public class SmartBindingActionBean extends BaseActionBean { @ValidateNestedProperties({ @Validate(field = "firstName", required = true, maxlength = 100), @Validate(field = "lastName", required = true, maxlength = 100) }) private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @DefaultHandler @DontValidate public Resolution main() { return new ForwardResolution("/WEB-INF/jsp/binding-validation.jsp"); } public Resolution print() { getContext().getMessages().add(new LocalizableMessage( "com.example.javapt09.stripes.action.SmartBindingActionBean.print.success", user)); return new RedirectResolution(SmartBindingActionBean.class); } } Features - Smart binding Present Technologies 10

Slide 11

Slide 11 text

Features - Smart binding Localized buttons and labels Present Technologies 11

com.example.javapt09.stripes.action.SmartBindingActionBean.user.firstName=First name com.example.javapt09.stripes.action.SmartBindingActionBean.user.lastName=Last name com.example.javapt09.stripes.action.SmartBindingActionBean.print=Print

Slide 12

Slide 12 text

Features - Validation Frequently used @Validate attributes Present Technologies 12 Attribute Type Description field String Name of nested field to validate. required boolean true indicates a required field. on String[] Event handlers for which to apply. minlength int Minimum length of input. maxlength int Maximum length of input. expression String EL expression to validate the input. mask String Regular expression that the input must match. minvalue double Minimum numerical value of input. maxvalue double Maximum numerical value of input. converter Class Type converter to use on the input.

Slide 13

Slide 13 text

Features - Validation Automatic maxlength on text inputs Present Technologies 13 public class SmartBindingActionBean extends BaseActionBean { @ValidateNestedProperties({ @Validate(field = "firstName", required = true, maxlength = 100), @Validate(field = "lastName", required = true, maxlength = 100) }) private User user; ... }

First name

Last name

Slide 14

Slide 14 text

Features - Validation Custom Validation Present Technologies 14 @ValidationMethod public void validate(ValidationErrors errors) { if (user.getLastName().equals(user.getFirstName())) { errors.add("lastName", new SimpleError("First and last name must be different!")); } }

Slide 15

Slide 15 text

Features - Validation Displaying errors and messages • Messages • All errors • Specific field error Present Technologies 15

Slide 16

Slide 16 text

Features - Clean URLs Customizable URLs Present Technologies 16 DispatcherServlet /action/* @UrlBinding("/action/cleanURL/{$event}/{city}") public class CustomizableURLActionBean extends BaseActionBean { private String city; public Resolution delete() { ... } }

Slide 17

Slide 17 text

Features - Clean URLs DynamicMappingFilter Static resources can be delivered from the same namespace to which an ActionBean is mapped using clean URLs. Present Technologies 17 DynamicMappingFilter net.sourceforge.stripes.controller.DynamicMappingFilter ActionResolver.Packages com.example.javapt09.stripes.action DynamicMappingFilter /* REQUEST FORWARD INCLUDE

Slide 18

Slide 18 text

Features - Layouts Reusable layout Present Technologies 18 <%@include file="/WEB-INF/jsp/common/taglibs.jsp" %> ${title}
${title}

Slide 19

Slide 19 text

Features - Layouts Using a reusable layout to render a page Present Technologies 19 <%@include file="/WEB-INF/jsp/common/taglibs.jsp" %>

Main page content...

Slide 20

Slide 20 text

Features - Layouts Final result Present Technologies 20 JavaPT09 - Stripes » Layouts
JavaPT09 - Stripes » Layouts

Main page content...

Slide 21

Slide 21 text

Features - Exception handling Present Technologies 21 DelegatingExceptionHandler.Packages com.example.javapt09.stripes.exception public class DefaultExceptionHandler implements AutoExceptionHandler { public Resolution handle(Exception exception, HttpServletRequest request, HttpServletResponse response) { // Handle Exception return new ForwardResolution(ErrorActionBean.class); } public Resolution handle(IOException exception, HttpServletRequest request, HttpServletResponse response) { // Handle IOException return new ForwardResolution(ErrorActionBean.class); } }

Slide 22

Slide 22 text

Features - Exception handling Don’t catch your exceptions Present Technologies 22 public Resolution handledException() throws IOException { throw new IOException("Handled exception"); } public Resolution unhandledException() throws Exception { throw new Exception("Unhandled exception"); }

Slide 23

Slide 23 text

Features - Interceptors Built-in interceptors Stripes request processing lifecycle stages: Present Technologies 23 @Before(stages = LifecycleStage.BindingAndValidation) public void prepareSomeStuff() { // load data from the DB } RequestInit ActionBeanResolution HandlerResolution BindingAndValidation CustomValidation EventHandling ResolutionExecution RequestComplete

Slide 24

Slide 24 text

Features - Interceptors Custom interceptors Present Technologies 24 public interface Interceptor { Resolution intercept(ExecutionContext context) throws Exception; } @Intercepts(LifecycleStage.ActionBeanResolution) public class EJBInterceptor implements Interceptor { public Resolution intercept(ExecutionContext context) throws Exception { ... } }

Slide 25

Slide 25 text

Features - Easy Ajax integration Present Technologies 25 public class AjaxActionBean extends BaseActionBean { private List cities = new ArrayList(); public Resolution load() { return new JavaScriptResolution(cities); } }
var client = new XMLHttpRequest(); client.open("GET", "${contextPath}/Ajax.action?load="); client.onreadystatechange = function() { if (this.readyState == 4) { var cities = eval(client.responseText); var citiesList = ""; for (var i = 0; i < cities.length; i++) { citiesList += "<li>" + cities[i] + "</li>"; } document.getElementById("cities").innerHTML = "<ul>" + citiesList + "</ul>"; } } client.send(null);

Slide 26

Slide 26 text

Features - File download Present Technologies 26 public class DownloadActionBean extends BaseActionBean { @DefaultHandler public Resolution main() throws FileNotFoundException { String fileName = "stripes.png"; String filePath = getContext().getServletContext().getRealPath("/img/" + fileName); return new StreamingResolution("image/png", new FileInputStream(filePath)).setFilename(fileName); } }

Slide 27

Slide 27 text

Features - File upload File upload form Present Technologies 27

Slide 28

Slide 28 text

Features - File upload Saving the file Present Technologies 28 public class UploadActionBean extends BaseActionBean { private FileBean fileBean; public FileBean getFileBean() { return fileBean; } public void setFileBean(FileBean fileBean) { this.fileBean = fileBean; } @DefaultHandler public Resolution main() { return new ForwardResolution("/WEB-INF/jsp/file-upload.jsp"); } public Resolution upload() throws IOException { fileBean.getFileName(); fileBean.getSize(); fileBean.getContentType(); // fileBean.save(new File()); return new ForwardResolution("/WEB-INF/jsp/file-upload.jsp"); } }

Slide 29

Slide 29 text

Features - Extension/customization • Stripes uses auto-discovery to find extensions • The specified packages will be scanned for extensions like interceptors, formatters, type converters, exception handlers, etc Present Technologies 29 Extension.Packages com.example.javapt09.stripes.extensions @Validate(maxlength = 100, converter = EmailTypeConverter.class) private String email;

Slide 30

Slide 30 text

Extensions • Spring integration (built-in) http://www.stripesframework.org/display/stripes/Spring +with+Stripes • Stripes Injection Enricher (support for @EJB, @Inject and @Resource) https://github.com/samaxes/stripes-injection-enricher Present Technologies 30

Slide 31

Slide 31 text

Extensions • Stripes Security (roles based) http://www.stripesframework.org/display/stripes/Securi ng+Stripes+With+ACLs • Stripes Security (custom authorization) http://www.stripesframework.org/display/stripes/Securit y+Interceptor+for+custom+authorization Present Technologies 31

Slide 32

Slide 32 text

Extensions • Stripes-Reload Reloads modifications made to your Action Beans, Type Converters, Formatters, and Resource Bundles without having to restart your server. http://code.google.com/p/stripes-reload/ Present Technologies 32

Slide 33

Slide 33 text

Find more • Stripes Framework http://www.stripesframework.org • Stripes Book: Stripes ...and Java Web Development Is Fun Again http://pragprog.com/book/fdstr/stripes Present Technologies 33

Slide 34

Slide 34 text

Q&A Present Technologies 34

Slide 35

Slide 35 text

Contacts • Present Technologies http://www.present-technologies.com • Blog http://www.samaxes.com • Twitter http://twitter.com/samaxes Present Technologies 35