【问题标题】:Spring framework upgrade - Modifying the codeSpring框架升级——修改代码
【发布时间】:2023-12-05 01:55:02
【问题描述】:

尝试将 springframe 工作项目从非常旧的版本升级到最新版本 5.3。用新的替换旧的 jar 文件后,我看到编译错误。下面是示例代码。感谢您帮助使用最新框架更新以下代码。

错误@1. SimpleFormContrller - 未找到 2. retun new RedirectView(getSuccessView() - Notfound.

public class RangeFormController extends SimpleFormController {

   Protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {

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

 Spring-Context.xml
<bean id="RangeFormController " 
 class="org.test.controller.RangeFormController ">
  <property name="sessionForm"><value>true</value></property>
  <property name="commandName"><value>fileUpload</value></property>      
  <property name="formView"><value>FileUpload</value></property>
  <property name="successView"><value>upload.html</value></property>      
</bean>

【问题讨论】:

    标签: spring frameworks upgrade


    【解决方案1】:

    SimpleFormController 在 3.0 中已弃用。您应该改用@Controller,如下所示:

    @Controller
    public class RangeFormController {
    
    }
    

    因此getSuccessView() 也将不可用。只需使用字符串作为RedirectView 的参数。参考文档:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/view/RedirectView.html#RedirectView-java.lang.String-.

    【讨论】:

    • 用 springcontext.xml 更新了我的帖子。我还需要添加@Mapping“RangeFormController”吗?我应该根据 spring-context 文件替换 getSuccessView() 什么?
    • 请不要在 Spring 5.3 中使用 XML。注释和 JavaConfig 很久以前就取代了 XML。