Skip to content

Commit

Permalink
Added controller support for validation
Browse files Browse the repository at this point in the history
Conflicts:
	src/main/resources/scaffold/spring/SpringControllerTemplate.jv
  • Loading branch information
Tejas committed Feb 4, 2013
1 parent b87187f commit ed09dc1
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jboss.forge.scaffold.spring.metawidget.layout;

import org.metawidget.statically.spring.widgetbuilder.SpringTag;

public class FormErrorTag
extends SpringTag {

//
// Constructor
//

public FormErrorTag() {

super( "errors" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,41 @@
import static org.metawidget.inspector.spring.SpringInspectionResultConstants.*;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.lang.model.type.PrimitiveType;

import org.jboss.forge.env.Configuration;
import org.jboss.forge.parser.java.util.Strings;
import org.jboss.forge.scaffold.spring.SpringScaffold;
import org.jboss.forge.scaffold.spring.metawidget.layout.FormErrorTag;
import org.jvnet.inflector.Noun;
import org.metawidget.iface.MetawidgetException;
import org.metawidget.statically.BaseStaticXmlWidget;
import org.metawidget.statically.StaticXmlStub;
import org.metawidget.statically.StaticXmlWidget;
import org.metawidget.statically.html.widgetbuilder.HtmlDiv;
import org.metawidget.statically.html.widgetbuilder.HtmlInput;
import org.metawidget.statically.html.widgetbuilder.HtmlTableBody;
import org.metawidget.statically.html.widgetbuilder.HtmlTableCell;
import org.metawidget.statically.html.widgetbuilder.HtmlTableRow;
import org.metawidget.statically.jsp.StaticJspMetawidget;
import org.metawidget.statically.jsp.StaticJspUtils;
import org.metawidget.statically.jsp.widgetbuilder.CoreOut;
import org.metawidget.statically.jsp.widgetprocessor.StandardBindingProcessor;
import org.metawidget.statically.layout.SimpleLayout;
import org.metawidget.statically.spring.StaticSpringMetawidget;
import org.metawidget.statically.spring.widgetbuilder.FormCheckboxTag;
import org.metawidget.statically.spring.widgetbuilder.FormInputTag;
import org.metawidget.statically.spring.widgetbuilder.FormOptionTag;
import org.metawidget.statically.spring.widgetbuilder.FormOptionsTag;
import org.metawidget.statically.spring.widgetbuilder.FormPasswordTag;
import org.metawidget.statically.spring.widgetbuilder.FormSelectTag;
import org.metawidget.statically.spring.widgetbuilder.FormTextareaTag;
import org.metawidget.statically.spring.widgetbuilder.SpringWidgetBuilder;
import org.metawidget.util.CollectionUtils;
import org.metawidget.util.WidgetBuilderUtils;
import org.metawidget.util.simple.StringUtils;

Expand All @@ -66,6 +77,10 @@ public class SpringEntityWidgetBuilder
//
// Private statics
//
private final static String MAX_LENGTH = "maxlength";


private static final List<Boolean> LIST_BOOLEAN_VALUES = CollectionUtils.unmodifiableList( Boolean.TRUE, Boolean.FALSE );

/**
* Current Forge configuration. Useful to retrieve <code>targetDir</code>.
Expand Down Expand Up @@ -268,7 +283,7 @@ public StaticXmlWidget buildWidget(String elementName, Map<String, String> attri
}
}

if (clazz != null)
if (clazz != null && !HIDDEN.equals(attributes.get(HIDDEN)))
{
if (Collection.class.isAssignableFrom(clazz))
{
Expand Down Expand Up @@ -296,6 +311,54 @@ public StaticXmlWidget buildWidget(String elementName, Map<String, String> attri
{
return new StaticXmlStub();
}

// Primitives

if (clazz.isPrimitive()) {

if (char.class.equals(clazz)) {
attributes.put(MAXIMUM_LENGTH, "1");
return createFormInputTag(attributes);
}

return createFormInputTag(attributes);
}

// String

if (String.class.equals(clazz)) {
if (TRUE.equals(attributes.get(LARGE))) {
return createFormTextareaTag(attributes);
}

if (TRUE.equals(attributes.get(MASKED))) {
FormPasswordTag passwordTag = new FormPasswordTag();
passwordTag.putAttribute(MAX_LENGTH,
attributes.get(MAXIMUM_LENGTH));
return passwordTag;
}

return createFormInputTag(attributes);
}

// Character

if (Character.class.equals(clazz)) {
attributes.put(MAXIMUM_LENGTH, "1");
return createFormInputTag(attributes);
}

// Dates

if (Date.class.equals(clazz)) {
return createFormInputTag(attributes);
}

// Numbers

if (Number.class.isAssignableFrom(clazz)) {
return createFormInputTag(attributes);
}
}

return null;
Expand Down Expand Up @@ -334,4 +397,40 @@ private boolean widgetIsLink(BaseStaticXmlWidget widget)

return false;
}

private HtmlDiv createFormInputTag( Map<String, String> attributes ) {

HtmlDiv row = new HtmlDiv();
FormInputTag input = new FormInputTag();
row.getChildren().add(input);
if ( !"".equals( attributes.get( MAXIMUM_LENGTH ))) {
input.putAttribute( MAX_LENGTH, attributes.get( MAXIMUM_LENGTH ) );
}
HtmlDiv innerDiv = new HtmlDiv();
innerDiv.putAttribute("class", "error");
FormErrorTag error = new FormErrorTag();
innerDiv.getChildren().add(error);
row.getChildren().add(innerDiv);
return row;
}


private StaticXmlWidget createFormTextareaTag( Map<String, String> attributes ) {

FormTextareaTag textarea = new FormTextareaTag();

String rows = attributes.get( "rows" );

if ( rows != null ) {
textarea.putAttribute( "rows", rows );
}

String cols = attributes.get( "cols" );

if ( cols != null ) {
textarea.putAttribute( "cols", cols );
}

return textarea;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public class SpringFacetImpl extends BaseFacet implements SpringFacet
private static final Dependency SPRING_WEB = DependencyBuilder.create("org.springframework:spring-web:${spring.version}");

private static final Dependency SPRING_WEB_MVC = DependencyBuilder.create("org.springframework:spring-webmvc:${spring.version}");

private static final Dependency JAVA_VALIDATION = DependencyBuilder.create("javax.validation:validation-api");

@Inject
public SpringFacetImpl(final DependencyInstaller installer)
Expand Down Expand Up @@ -137,7 +139,7 @@ public boolean install()
protected List<Dependency> getRequiredDependencies()
{
return Arrays.asList(SPRING_ASM, SPRING_BEANS, SPRING_CONTEXT, SPRING_CONTEXT_SUPPORT,
SPRING_CORE, SPRING_EXPRESSION, SPRING_ORM, SPRING_TX, SPRING_WEB, SPRING_WEB_MVC);
SPRING_CORE, SPRING_EXPRESSION, SPRING_ORM, SPRING_TX, SPRING_WEB, SPRING_WEB_MVC, JAVA_VALIDATION);
}

/*
Expand Down
36 changes: 26 additions & 10 deletions src/main/resources/scaffold/spring/SpringControllerTemplate.jv
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import @{topLevelPackage}.repo.@{clazz}Dao;
@end{}

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -38,9 +41,13 @@ public class @{entity.getName()}Controller
@end{}
@RequestMapping(method=RequestMethod.GET)
public @ModelAttribute("@{entityPlural.toLowerCase()}")
List<@{entity.getName()}> view@{entityPlural}(@ModelAttribute("search") @{entity.getName()} search, @RequestParam(required=false) Long first,
List<@{entity.getName()}> view@{entityPlural}(@Valid @ModelAttribute("search") @{entity.getName()} search, BindingResult result, @RequestParam(required=false) Long first,
@RequestParam(required=false) Integer max, Model model)
{

if(result.hasErrors()){
return @{ccEntity}Dao.getAll();
}
if (max == null || max <= 0)
{
max = 10;
Expand Down Expand Up @@ -77,9 +84,13 @@ public class @{entity.getName()}Controller

@RequestMapping(method=RequestMethod.POST, params="search")
public @ModelAttribute("@{entityPlural.toLowerCase()}")
List<@{entity.getName()}> search@{entity.getName()}(@ModelAttribute("search") @{entity.getName()} search, @RequestParam(required=false) Long first,
List<@{entity.getName()}> search@{entity.getName()}(@Valid @ModelAttribute("search") @{entity.getName()} search, BindingResult result, @RequestParam(required=false) Long first,
@RequestParam(required=false) Integer max, Model model)
{

if(result.hasErrors()){
return @{ccEntity}Dao.getAll();
}
if (max == null || max <= 0)
{
max = 10;
Expand Down Expand Up @@ -140,8 +151,11 @@ public class @{entity.getName()}Controller
}

@RequestMapping(value="/{id}", method=RequestMethod.POST)
public String update@{entity.getName()}(@PathVariable("id") Long id, @{entity.getName()} @{ccEntity})
public String update@{entity.getName()}(@PathVariable("id") Long id, @Valid @{entity.getName()} @{ccEntity}, BindingResult result)
{
if(result.hasErrors()){
return "edit@{entity.getName()}";
}
@{ccEntity}Dao.update(@{ccEntity});
return "view@{entity.getName()}";
}
Expand Down Expand Up @@ -171,20 +185,22 @@ public class @{entity.getName()}Controller
}

@RequestMapping(value="/create", method=RequestMethod.POST)
public String create@{entity.getName()}(@{entity.getName()} @{ccEntity})
public String create@{entity.getName()}(@Valid @{entity.getName()} @{ccEntity}, BindingResult result)
{
@foreach{name : notntomany}
if(@{ccEntity}.get@{name.substring(0,1).toUpperCase()}@{name.substring(1)}().getId() == null){
@{ccEntity}.set@{name.substring(0,1).toUpperCase()}@{name.substring(1)}(null);
}
@end{}
if(result.hasErrors()){
return "create@{entity.getName()}";
}
@{ccEntity}Dao.create(@{ccEntity});
return "redirect:@{targetDir}@{entityPlural.toLowerCase()}";
}

@RequestMapping(method=RequestMethod.POST, params="create")
public String createNew@{entity.getName()}(@ModelAttribute("search") @{entity.getName()} search, HttpSession session)
public String createNew@{entity.getName()}(@Valid @ModelAttribute("search") @{entity.getName()} search, BindingResult result, HttpSession session)
{
if(result.hasErrors()){
session.setAttribute("@{ccEntity}", new @{entity.getName()}());
return "redirect:@{targetDir}@{entityPlural.toLowerCase()}/create";
}
session.setAttribute("@{ccEntity}", search);
return "redirect:@{targetDir}@{entityPlural.toLowerCase()}/create";
}
Expand Down
Loading

0 comments on commit ed09dc1

Please sign in to comment.