- Revision
- 476
- Author
- mauro
- Date
- 2007-12-14 09:03:28 -0600 (Fri, 14 Dec 2007)
Log Message
WAFFLE-47: Made PRG use configurable via action method annotations.
Modified Paths
- trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/UploadController.java
- trunk/examples/simple-example/src/main/webapp/upload.jspx
- trunk/pom.xml
- trunk/waffle-core/src/main/java/org/codehaus/waffle/action/annotation/ActionMethod.java
- trunk/waffle-core/src/main/java/org/codehaus/waffle/action/annotation/DefaultActionMethod.java
- trunk/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java
- trunk/waffle-core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java
Diff
Modified: trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/UploadController.java (475 => 476)
--- trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/UploadController.java 2007-12-14 00:36:46 UTC (rev 475) +++ trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/UploadController.java 2007-12-14 15:03:28 UTC (rev 476) @@ -1,5 +1,6 @@ package org.codehaus.waffle.example.simple; +import java.util.Collection; import java.util.List; import org.apache.commons.fileupload.FileItem; @@ -9,21 +10,26 @@ public class UploadController { private FileUploader uploader; + private Collection<String> errors; + private List<FileItem> files; public UploadController(FileUploader uploader) { this.uploader = uploader; } - @DefaultActionMethod - public void upload(){ - List<FileItem> fileItems = uploader.getFileItems(); - for ( FileItem file : fileItems ){ - System.out.println(file.getName()); - System.out.println(new String(file.get())); - } - if ( uploader.hasErrors() ){ - System.out.println(uploader.getErrors()); - } + @DefaultActionMethod(prg=false) + public void upload(){ + // PRG needs to be disabled to allow request-scope content to be accessible in referring view + files = uploader.getFileItems(); + errors = uploader.getErrors(); } + + public Collection<String> getErrors() { + return errors; + } + + public List<FileItem> getFiles() { + return files; + } }
Modified: trunk/examples/simple-example/src/main/webapp/upload.jspx (475 => 476)
--- trunk/examples/simple-example/src/main/webapp/upload.jspx 2007-12-14 00:36:46 UTC (rev 475) +++ trunk/examples/simple-example/src/main/webapp/upload.jspx 2007-12-14 15:03:28 UTC (rev 476) @@ -30,6 +30,15 @@ <br/> + <table> + <c:forEach var="file" items="${files}" varStatus="status"> + <tr class="${status.index % 2 eq 0 ? 'even' : 'odd'}"> + <td>${file.name}</td> + <td>${file.string}</td> + </tr> + </c:forEach> + </table> + </form> </body>
Modified: trunk/pom.xml (475 => 476)
--- trunk/pom.xml 2007-12-14 00:36:46 UTC (rev 475) +++ trunk/pom.xml 2007-12-14 15:03:28 UTC (rev 476) @@ -269,7 +269,7 @@ <plugin> <groupId>org.codehaus.mojo.groovy</groupId> <artifactId>groovy-maven-plugin</artifactId> - <version>1.0-beta-2</version> + <version>1.0-beta-3</version> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId>
Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/action/annotation/ActionMethod.java (475 => 476)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/action/annotation/ActionMethod.java 2007-12-14 00:36:46 UTC (rev 475) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/action/annotation/ActionMethod.java 2007-12-14 15:03:28 UTC (rev 476) @@ -19,4 +19,5 @@ @Retention(RetentionPolicy.RUNTIME) public @interface ActionMethod { String[] parameters() default {}; + boolean prg() default true; }
Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/action/annotation/DefaultActionMethod.java (475 => 476)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/action/annotation/DefaultActionMethod.java 2007-12-14 00:36:46 UTC (rev 475) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/action/annotation/DefaultActionMethod.java 2007-12-14 15:03:28 UTC (rev 476) @@ -19,4 +19,5 @@ @Retention(RetentionPolicy.RUNTIME) public @interface DefaultActionMethod { String[] parameters() default {}; + boolean prg() default true; }
Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java (475 => 476)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java 2007-12-14 00:36:46 UTC (rev 475) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java 2007-12-14 15:03:28 UTC (rev 476) @@ -10,14 +10,25 @@ *****************************************************************************/ package org.codehaus.waffle.servlet; -import org.codehaus.waffle.ComponentRegistry; import static org.codehaus.waffle.Constants.VIEW_PREFIX_KEY; import static org.codehaus.waffle.Constants.VIEW_SUFFIX_KEY; + +import java.io.IOException; +import java.lang.reflect.Method; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.codehaus.waffle.ComponentRegistry; import org.codehaus.waffle.action.ActionMethodExecutor; import org.codehaus.waffle.action.ActionMethodInvocationException; import org.codehaus.waffle.action.ActionMethodResponse; import org.codehaus.waffle.action.ActionMethodResponseHandler; import org.codehaus.waffle.action.MethodDefinition; +import org.codehaus.waffle.action.annotation.ActionMethod; +import org.codehaus.waffle.action.annotation.DefaultActionMethod; import org.codehaus.waffle.bind.DataBinder; import org.codehaus.waffle.bind.RequestAttributeBinder; import org.codehaus.waffle.context.ContextContainer; @@ -30,12 +41,6 @@ import org.codehaus.waffle.view.RedirectView; import org.codehaus.waffle.view.View; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - /** * Waffle's FrontController for handling user requests. * @@ -165,20 +170,25 @@ View view = null; if (errorsContext.hasErrorMessages() || methodDefinition == null) { - view = buildViewToReferrer(controllerDefinition); + view = buildReferringView(controllerDefinition); } else { actionMethodExecutor.execute(actionMethodResponse, controllerDefinition); if (errorsContext.hasErrorMessages()) { - view = buildViewToReferrer(controllerDefinition); - } else if (actionMethodResponse.getReturnValue() == null) { + view = buildReferringView(controllerDefinition); + } else if (actionMethodResponse.getReturnValue() == null) { // Null or VOID indicate a Waffle convention (return to referring page) + // unless PRG is disabled if (request.getMethod().equalsIgnoreCase(POST)) { - // PRG (Post/Redirect/Get): see http://en.wikipedia.org/wiki/Post/Redirect/Get - String url = "" - view = new RedirectView(url, controllerDefinition.getController()); + if ( usePRG(methodDefinition) ){ + // PRG (Post/Redirect/Get): see http://en.wikipedia.org/wiki/Post/Redirect/Get + view = buildRedirectingView(request, controllerDefinition); + } else { + // PRG is disabled + view = buildReferringView(controllerDefinition); + } } else { // was a GET - view = buildViewToReferrer(controllerDefinition); + view = buildReferringView(controllerDefinition); } } } @@ -198,11 +208,49 @@ } /** - * Build a view back to the referring page (use the Controller's name as the View name). + * Determine if PRG paradigm is used from the "prg" attribute of the action method annotations + * + * @param methodDefinition the MethodDefinition + * @return A boolean flag, defaults to <code>true</code> */ - protected View buildViewToReferrer(ControllerDefinition controllerDefinition) { + private boolean usePRG(MethodDefinition methodDefinition) { + Method method = methodDefinition.getMethod(); + // first check ActionMethod annotation + ActionMethod actionMethod = method.getAnnotation(ActionMethod.class); + if ( actionMethod != null ){ + return actionMethod.prg(); + } + // then check DefaultActionMethod annotation + DefaultActionMethod defaultActionMethod = method.getAnnotation(DefaultActionMethod.class); + if ( defaultActionMethod != null ){ + return defaultActionMethod.prg(); + } + // else default to true + return true; + } + + /** + * Build a view back to the referring page, using the Controller's name as the View name. + * + * @param controllerDefinition the ControllerDefinition + * @return The View + */ + protected View buildReferringView(ControllerDefinition controllerDefinition) { String controllerValue = viewPrefix + controllerDefinition.getName() + viewSuffix; return new View(controllerValue, controllerDefinition.getController()); } + + /** + * Build redirecting view, used by PRG paradigm. + * + * @param request the request + * @param controllerDefinition the ControllerDefinition + * @return The RedirectView + */ + protected View buildRedirectingView(HttpServletRequest request, ControllerDefinition controllerDefinition) { + String url = "" + return new RedirectView(url, controllerDefinition.getController()); + } + }
Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java (475 => 476)
--- trunk/waffle-core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java 2007-12-14 00:36:46 UTC (rev 475) +++ trunk/waffle-core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java 2007-12-14 15:03:28 UTC (rev 476) @@ -508,7 +508,7 @@ viewSuffixField.set(waffleServlet, "-suffix"); ControllerDefinition controllerDefinition = new ControllerDefinition("foobar", null, null); - View view = waffleServlet.buildViewToReferrer(controllerDefinition); + View view = waffleServlet.buildReferringView(controllerDefinition); Assert.assertEquals("prefix-foobar-suffix", view.getValue()); }
To unsubscribe from this list please visit:
