문제

이 SimpleFormController를 변환하여 Spring MVC 2.5에 도입 된 주석 지지대를 사용하고 싶습니다.

자바

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;
    }

}

스프링 구성

<!-- 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>

기본적으로 모든 XML 구성을 대체하고 싶습니다. /priceincrease.htm Java 클래스 내에 주석이있는 콩. 이것이 가능합니까? 그렇다면 사용해야하는 해당 주석은 무엇입니까?

고마워요, 돈

도움이 되었습니까?

해결책

작동하는지 여부에 관계없이 다음과 비슷해 보일 것입니다. 또한이 글을 쓸 수있는 약 80 억 개의 유효한 방법이 있습니다. 스프링 문서를 참조하십시오. 13.11.4 "지원되는 핸들러 메소드 인수 및 반환 유형" 광기에 대한 개요. 또한 유효성 검사기를 자동화 할 수 있습니다

@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";
    }

}

다른 팁

누군가가 최근 MVC 로이 프로젝트를 완료했으며 Github에 있으므로 Spring의 자습서에 비해 모든 수업이 어떻게 변경되는지 확인할 수 있습니다.

링크: priceincreeaseformcontroller

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top