You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Framework升级问题:旧版SimpleFormController代码适配5.3版本

错误原因

  • SimpleFormController 是Spring 2.x版本提供的控制器类,从Spring 3.0开始被标记为废弃,Spring 5.x版本已经完全从官方包中移除,因此会报类找不到的错误。
  • getSuccessView() 是 SimpleFormController 父类提供的内置方法,父类被移除后自然无法调用该方法。

适配Spring 5.3的修改方案

Spring 3.0+版本推荐使用注解驱动的控制器实现,完全可以替代原有继承 SimpleFormController 的所有能力。

1. Controller代码修改

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.view.RedirectView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// 对应原配置的sessionForm=true
@SessionAttributes("fileUpload")
@Controller
public class RangeFormController {

    // 对应原配置的formView,处理GET请求进入表单页
    @GetMapping("/你项目实际的表单访问路径")
    public String showForm(Model model) {
        // 此处替换为你自己的表单实体类,对应原command对象
        model.addAttribute("fileUpload", new 你的表单实体类());
        return "FileUpload";
    }

    // 对应原onSubmit方法,处理表单提交请求
    @PostMapping("/你项目实际的表单提交路径")
    public RedirectView handleSubmit(HttpServletRequest request, 
                                     HttpServletResponse response,
                                     @ModelAttribute("fileUpload") 你的表单实体类 command) throws Exception {
        // 此处保留你原有的提交逻辑代码
        return new RedirectView("upload.html");
    }
}

注意把代码里的路径和表单实体类替换为你项目实际的内容即可。

2. Spring-Context.xml配置修改

删除原来的RangeFormController 相关bean配置,新增以下配置即可:

<!-- 开启Controller所在包的组件扫描 -->
<context:component-scan base-package="org.test.controller" />
<!-- 开启Spring MVC注解驱动 -->
<mvc:annotation-driven />

可选优化(保留xml配置视图的习惯)

如果不想硬编码视图名称,可以通过注解注入配置值:

  1. Controller中新增属性注入
@Value("${form.successView}")
private String successView;
@Value("${form.formView}")
private String formView;
  1. Spring-Context.xml中新增配置
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="properties">
        <props>
            <prop key="form.formView">FileUpload</prop>
            <prop key="form.successView">upload.html</prop>
        </props>
    </property>
</bean>

注入后直接在返回视图的位置使用对应的变量即可。

内容的提问来源于stack exchange,提问作者user3067524

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 14:48:03