Question

J'aimerais convertir ce SimpleFormController pour qu'il utilise le support d'annotation introduit dans Spring MVC 2.5

Java

public class PriceIncreaseFormController extends SimpleFormController {

    ProductManager productManager = new ProductManager();

    @Override
    public ModelAndView onSubmit(Object command)
            throws ServletException {

        int increase = ((PriceIncrease) command).getPercentage();
        productManager.increasePrice(increase);

        return new ModelAndView(new RedirectView(getSuccessView()));
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request)
            throws ServletException {
        PriceIncrease priceIncrease = new PriceIncrease();
        priceIncrease.setPercentage(20);
        return priceIncrease;
    }

}

Config de printemps

<!-- Include basic annotation support -->
<context:annotation-config/>        

<!-- Comma-separated list of packages to search for annotated controllers. Append '.*' to search all sub-packages -->
<context:component-scan base-package="springapp.web"/>  

<!-- Enables use of annotations on controller methods to map URLs to methods and request params to method arguments  -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<bean name="/priceincrease.htm" class="springapp.web.PriceIncreaseFormController">
    <property name="sessionForm" value="true"/>
    <property name="commandName" value="priceIncrease"/>
    <property name="commandClass" value="springapp.service.PriceIncrease"/>
    <property name="validator">
        <bean class="springapp.service.PriceIncreaseValidator"/>
    </property>
    <property name="formView" value="priceincrease"/>
    <property name="successView" value="hello.htm"/>
    <property name="productManager" ref="productManager"/>
</bean>

En gros, j'aimerais remplacer toute la configuration XML du bean /priceincrease.htm par des annotations au sein de la classe Java. Est-ce possible et quelles sont les annotations correspondantes que je devrais utiliser?

Merci, Don

Était-ce utile?

La solution

Cela ressemblera à ce qui suit, mais son fonctionnement dépendra un peu de votre configuration (résolveur de vue, etc.). Je dois également noter qu'il existe environ huit milliards de manières valables d'écrire cette chose. Voir la documentation de printemps, 13.11. 4 "Arguments et types de retour de méthode de gestionnaire pris en charge" pour une vue d'ensemble de la folie. Notez également que vous pouvez autoriser le validateur

@Controller
@RequestMapping("/priceincrease.htm")
public class PriceIncreaseFormController {

    ProductManager productManager;

    @Autowired
    public PriceIncreaseFormController(ProductManager productManager) {
        this.productManager = productManager;
    }

    // note: this method does not have to be called onSubmit
    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(@ModelAttribute("priceIncrease") PriceIncrease priceIncrease, BindingResult result, SessionStatus status {

        new PriceIncreaseValidator().validate(priceIncrease, result);
        if (result.hasErrors()) {
            return "priceincrease";
        }
        else {
            int increase = priceIncrease.getPercentage();
            productManager.increasePrice(increase);
            status.setComplete();
            return "redirect:hello.htm";
        }
    }

    // note: this method does not have to be called setupForm
    @RequestMapping(method = RequestMethod.GET)    
    public String setupForm(Model model) {
        PriceIncrease priceIncrease = new PriceIncrease();
        priceIncrease.setPercentage(20);
        model.addAttribute("priceIncrease", priceIncrease);
        return "priceincrease";
    }

}

Autres conseils

Quelqu'un a terminé ce projet avec un MVC récent et il est sur github, vous pouvez donc voir comment toutes les classes sont modifiées par rapport au tutoriel de Spring.

Lien: PriceIncreaseFormController

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top