Java springmvc中的注释
我想将这个SimpleFormController转换为使用SpringMVC2.5中引入的注释支持 JavaJava springmvc中的注释,java,spring,spring-mvc,annotations,Java,Spring,Spring Mvc,Annotations,我想将这个SimpleFormController转换为使用SpringMVC2.5中引入的注释支持 Java public class PriceIncreaseFormController extends SimpleFormController { ProductManager productManager = new ProductManager(); @Override public ModelAndView onSubmit(Object command)
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>
基本上,我想用Java类中的注释替换/priceincrease.htm
bean的所有XML配置。这可能吗?如果可能,我应该使用哪些相应的注释
谢谢,
Don它看起来会像下面这样,尽管它是否工作完全取决于您的配置(视图解析器等)。我还应该注意到,有大约80亿种有效的方法来编写这个东西。请参阅Spring文档,了解疯狂的概述。还请注意,您可以自动连接验证器
@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的教程相比,所有类都发生了怎样的变化
Link:你的问题是…?我鄙视的是80亿种有效的方式,我通常直接实现控制器——至少这样我可以适当地跟踪接口。注释和表单控制器层次结构在我看来都很混乱。对我来说,关键是基于注释的控制器可以比使用旧类层次结构的控制器更容易进行单元测试。当然,适应它需要时间。我不想再错过的另一个特性是扩展方法参数注入的能力,但测试对我来说是最有用的。