resorte tiene una potente capacidad de StringUtils clase
observa lo siguiente matriz que contiene un conjunto de Identificación de
String [] idArray = new String [] {"0", "1", "2", "0", "5", "2"}
y quiere eliminar las referencias duplicadas. Sólo hazlo
idArray = StringUtils.removeDuplicateStrings(idArray);
Ahora idArray contendrá { "0", "1", "2", "5"}.
Y mucho más.
¿Es posible utilizar una clase de controlador sin declararlos en el archivo de contexto de la aplicación web?
Sí, sólo hay que poner @Component en su declaración (@Controller no funciona como se esperaba)
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(...) {}
}
Así que en el archivo web contexto de aplicación puso el siguiente
<beans ...>
<context:component-scan base-package="br.com.spring.view"/>
<context:annotation-config/>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="order" value="0"/>
<property name="caseSensitive" value="true"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver"/>
</beans>
Nada más
¿Validación en formas dinámicas?
Usar la siguiente
public class Team {
private List<Player> playerList;
}
Así que en equipo validador poner
@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();
}
}
}
herencia en los formularios web?
Uso ServlerRequestDataBinder junto con el indicador de forma oculta
public class Command {
private Parent parent;
}
public class Child extends Parent { ... }
public class AnotherChild extends Parent { ... }
Y en su controlador haga lo siguiente
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());
}
primavera tiene un sistema incorporado en la clase del analizador ClassPathScanningCandidateComponentProvider. Puede usarlo para buscar anotaciones, por ejemplo.
ClassPathScanningCandidateComponentProvider scanner =
new ClassPathScanningCandidateComponentProvider(<DO_YOU_WANT_TO_USE_DEFAULT_FILTER>);
scanner.addIncludeFilter(new AnnotationTypeFilter(<TYPE_YOUR_ANNOTATION_HERE>.class));
for (BeanDefinition bd : scanner.findCandidateComponents(<TYPE_YOUR_BASE_PACKAGE_HERE>))
System.out.println(bd.getBeanClassName());
resorte tiene una clase útil org.springframework.util.FileCopyUtils. Se puede copiar un archivo cargado mediante el uso de
public ModelAndView action(...) throws Exception {
Command command = (Command) command;
MultiPartFile uploadedFile = command.getFile();
FileCopyUtils.copy(uploadedFile.getBytes(), new File("destination"));
Si utiliza un controlador de anotación basada (primavera 2.5+) o controlador Spring MVC normal, se puede utilizar un WebBindingInitializer para registrar los editores de propiedades globales. Algo así como
public class GlobalBindingInitializer implements WebBindingInitializer {
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy", true);
}
}
Así que en el archivo de contexto de aplicación Web, declarar
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="GlobalBindingInitializer"/>
</property>
</bean>
De esta manera cualquier controlador basado en anotación puede utilizar cualquier editor de propiedades declaradas en GlobalBindingInitializer.
Y así sucesivamente ...
Cosas útiles, esto. El código que hace el trabajo es 'SimpleApplicationEventMulticaster', que puede usar opcionalmente un grupo de subprocesos para distribuir los eventos de forma asincrónica. Por mi parte, estoy aburrido de escribir código para hacer esto. – skaffman