Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Spring has a powerful built-in <a href="http://static.springsource.org/spring/docs/2.5.6/api/org/springframework/util/StringUtils.html" rel="nofollow noreferrer">StringUtils</a> class</p> <p>Notice the following array containing a set of id's</p> <pre><code>String [] idArray = new String [] {"0", "1", "2", "0", "5", "2"} </code></pre> <p>And you want to remove duplicate references. Just do it</p> <pre><code>idArray = StringUtils.removeDuplicateStrings(idArray); </code></pre> <p>Now idArray will contain {"0", "1", "2", "5"}. </p> <p>And much more.</p> <hr> <p>Is it possible to use a Controller class without declare them in web application context file?</p> <p>Yes, just put @Component in its declaration (@Controller does not work as expected)</p> <pre><code>package br.com.spring.view.controller @Component public class PlayerController extends MultiActionController { private Service service; @Autowired public PlayerController(InternalPathMethodNameResolver ipmnr, Service service) { this.service = service; setMethodNameResolver(ipmnr); } // mapped to /player/add public ModelAndView add(...) {} // mapped to /player/remove public ModelAndView remove(...) {} // mapped to /player/list public ModelAndView list(...) {} } </code></pre> <p>So in web application context file put the following</p> <pre><code>&lt;beans ...&gt; &lt;context:component-scan base-package="br.com.spring.view"/&gt; &lt;context:annotation-config/&gt; &lt;bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"&gt; &lt;property name="order" value="0"/&gt; &lt;property name="caseSensitive" value="true"/&gt; &lt;/bean&gt; &lt;bean class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver"/&gt; &lt;/beans&gt; </code></pre> <p>Nothing else</p> <hr> <p>Validation in dynamic forms ?</p> <p>Use the following </p> <pre><code>public class Team { private List&lt;Player&gt; playerList; } </code></pre> <p>So in Team validator put</p> <pre><code>@Component public class TeamValidator implements Validator { private PlayerValidator playerValidator; @Autowired public TeamValidator(PlayerValidator playerValidator) { this.playerValidator = playerValidator; } public boolean supports(Class clazz) { return clazz.isAssignableFrom(Team.class); } public void validate(Object command, Errors errors) { Team team = (Team) command; // do Team validation int index = 0; for(Player player: team.getPlayerList()) { // Notice code just bellow errors.pushNestedPath("playerList[" + index++ + "]"); ValidationUtils.invokeValidator(playerValidator, player, errors); errors.popNestedPath(); } } } </code></pre> <hr> <p>Inheritance in web forms ?</p> <p>Use ServlerRequestDataBinder along with hidden form flag</p> <pre><code>public class Command { private Parent parent; } public class Child extends Parent { ... } public class AnotherChild extends Parent { ... } </code></pre> <p>And in your controller do the following</p> <pre><code>public class MyController extends MultiActionController { public ModelAndView action(HttpServletRequest request, HttpServletResponse response, Object command) { Command command = (Command) command; // hidden form flag String parentChildType = ServletRequestUtils.getRequiredStringParameter(request, "parentChildType"); // getApplicationContext().getBean(parentChildType) retrieves a Parent object from a application context file ServletRequestDataBinder binder = new ServletRequestDataBinder(getApplicationContext().getBean(parentChildType)); // populates Parent child object binder.bind(request); command.setParent((Parent) binder.getTarget()); } </code></pre> <hr> <p>Spring has a built-in scanner class <a href="http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.html" rel="nofollow noreferrer">ClassPathScanningCandidateComponentProvider</a>. You can use it to find annotations, for instance.</p> <pre><code>ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(&lt;DO_YOU_WANT_TO_USE_DEFAULT_FILTER&gt;); scanner.addIncludeFilter(new AnnotationTypeFilter(&lt;TYPE_YOUR_ANNOTATION_HERE&gt;.class)); for (BeanDefinition bd : scanner.findCandidateComponents(&lt;TYPE_YOUR_BASE_PACKAGE_HERE&gt;)) System.out.println(bd.getBeanClassName()); </code></pre> <hr> <p>Spring has a useful <a href="http://static.springsource.org/spring/docs/2.5.6/api/org/springframework/util/FileCopyUtils.html" rel="nofollow noreferrer">org.springframework.util.FileCopyUtils</a> class. You can copy a uploaded file by using</p> <pre><code>public ModelAndView action(...) throws Exception { Command command = (Command) command; MultiPartFile uploadedFile = command.getFile(); FileCopyUtils.copy(uploadedFile.getBytes(), new File("destination")); </code></pre> <hr> <p>If you use a annotation based controller (Spring 2.5+) OR plain MVC Spring controller, you can use a WebBindingInitializer to register global property editors. Something like</p> <pre><code>public class GlobalBindingInitializer implements WebBindingInitializer { public void initBinder(WebDataBinder binder, WebRequest request) { binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy", true); } } </code></pre> <p>So in your web application context file, declare</p> <pre><code>&lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt; &lt;property name="webBindingInitializer"&gt; &lt;bean class="GlobalBindingInitializer"/&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>This way any annotation based controller can use any property editor declared in GlobalBindingInitializer.</p> <p>And so on...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload