gpt4 book ai didi

java - Spring MVC 中的注解

转载 作者:搜寻专家 更新时间:2023-11-01 02:18:38 26 4
gpt4 key购买 nike

我想将此 SimpleFormController 转换为使用 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;
}

}

Spring 配置

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

基本上,我想用 Java 类中的注释替换 /priceincrease.htm bean 的所有 XML 配置。这可能吗?如果可能,我应该使用哪些相应的注释?

谢谢,唐

最佳答案

它看起来像下面这样,尽管它是否完全按原样工作将取决于您的配置( View 解析器等)。我还应该注意到,大约有 80 亿种有效的方式来编写这个东西。请参阅 Spring 文档,13.11.4 "Supported handler method arguments and return types"疯狂的概述。另请注意,您可以 Autowiring validator

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

}

关于java - Spring MVC 中的注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/803495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com