自定义属性编辑器不适用于 Spring MVC 中的请求参数?

2023-12-08

我正在尝试使用 Spring 注释创建一个多操作 Web 控制器。该控制器将负责添加和删除用户配置文件并为 jsp 页面准备参考数据。

@Controller
public class ManageProfilesController {
    @InitBinder
    public void initBinder(WebDataBinder binder) { 
        binder.registerCustomEditor(UserAccount.class,"account", new UserAccountPropertyEditor(userManager)); 
        binder.registerCustomEditor(Profile.class, "profile", new ProfilePropertyEditor(profileManager));
        logger.info("Editors registered");
    }

    @RequestMapping("remove")
    public void up( @RequestParam("account") UserAccount account,
                @RequestParam("profile") Profile profile) {
        ...
    }

    @RequestMapping("")
    public ModelAndView defaultView(@RequestParam("account") UserAccount account) {
        logger.info("Default view handling");
        ModelAndView mav = new ModelAndView();
        logger.info(account.getLogin());
        mav.addObject("account", account);
        mav.addObject("profiles", profileManager.getProfiles());
        mav.setViewName(view);
        return mav;
    }
    ...
}

这是我的 webContext.xml 文件的一部分:

<context:component-scan base-package="ru.mirea.rea.webapp.controllers" />
<context:annotation-config/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
            <value>
               ...
              /home/users/manageProfiles=users.manageProfilesController
            </value>
    </property>
</bean>

<bean id="users.manageProfilesController" class="ru.mirea.rea.webapp.controllers.users.ManageProfilesController">
    <property name="view" value="home\users\manageProfiles"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

但是,当我打开映射的网址时,出现异常:

java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [ru.mirea.rea.model.UserAccount]: no matching editors or conversion strategy found

我使用 spring 2.5.6 并计划在不久的将来迁移到 Spring 3.0。然而,根据这个JIRAhttps://jira.springsource.org/browse/SPR-4182在 Spring 2.5.1 中应该已经可以实现。

调试显示InitBinder方法被正确调用。

我究竟做错了什么?

Update:

public class UserAccountPropertyEditor extends PropertyEditorSupport {
    static Logger logger = Logger.getLogger(UserAccountPropertyEditor.class);

    public UserAccountPropertyEditor(IUserDAO dbUserManager) {
        this.dbUserManager = dbUserManager;
    }

    private IUserDAO dbUserManager;

    public String getAsText() {
        UserAccount obj = (UserAccount) getValue();
        if (null==obj) {
                return "";
        } else {
            return obj.getId().toString();
        }
    }

    public void setAsText(final String value) {
        try {   
            Long id = Long.parseLong(value);
            UserAccount acct = dbUserManager.getUserAccountById(id);
            if (null!=acct) {
                super.setValue(acct);
            } else {
                logger.error("Binding error. Cannot find userAccount with id  ["+value+"]");
                throw new IllegalArgumentException("Binding error. Cannot find userAccount with id  ["+value+"]");
            }
        } catch (NumberFormatException e) {
            logger.error("Binding error. Invalid id: " + value);
            throw new IllegalArgumentException("Binding error. Invalid id: " + value);
        }
    }
}

UserAccountPropertyEditor 没有记录任何错误。


我认为您不想指定field论证WebDataBinder.registerCustomEditor()。这旨在与表单支持对象一起工作,但您没有使用它。

尝试更简单的 2-arg 方法,它应该可以工作:

binder.registerCustomEditor(UserAccount.class, new UserAccountPropertyEditor(userManager)); 
binder.registerCustomEditor(Profile.class, new ProfilePropertyEditor(profileManager));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自定义属性编辑器不适用于 Spring MVC 中的请求参数? 的相关文章

随机推荐

  • IntelliJ IDEA 验证错误的 XSD

    我定义了以下事务管理器
  • IOException:读取失败,套接字可能已关闭 - Android 4.3 上的蓝牙

    目前 我正在尝试在使用 Android 4 3 Build JWR66Y 我猜是第二个 4 3 更新 的 Nexus 7 2012 上打开 BluetoothSocket 时处理一个奇怪的异常 我看过一些相关的帖子 例如https stac
  • 计算字符串中字符的出现次数 Haskell [关闭]

    Closed 这个问题需要多问focused 目前不接受答案 尝试确定如何计算 a 的出现次数char is a string 我是把它存储在一个列表中 char count countChars String gt Char Int 我是
  • Qt Creator 编辑器中的 CPU 使用率过高

    即使在简单的应用程序中 就像由向导制作的 QWidgets 应用程序一样简单 在编辑代码时 qtcreator exe 每隔一段时间就会飙升至 50 1 个核心 似乎即使在输入几个新字符后它也会解析整个项目 从而使其无响应并且在某种程度上无
  • 就地生成 2 个向量的笛卡尔积?

    如果我想得到这两个的笛卡尔积vector
  • 如何在曲面图上投影一条线?

    我有一个根据 CSV 文件中存储的点数据创建的曲面图 如果我想在 3D 创建的表面上投影一条线 漂浮在表面上方 方法是什么 我尝试过来自的代码以下帖子用于在 xy xz yz 平面上投影一条线 我可以看到它正在将线的端点投影到 xy xz
  • Bash-if 子句无法正常工作[重复]

    这个问题在这里已经有答案了 我想制作一个关闭脚本 但它没有按预期工作 这是我写的 echo Wanna shutdown y n read ANSWER if ANSWER y then sudo shutdown P now else p
  • CSS:“正确”属性拒​​绝申请?

    这真让我抓狂 我有两个 div 就在 body 标签之后 它们的位置和大小都是绝对的 它们都具有相同的 左 属性 但我后来声明了第二个 div 的 右 属性 因此 左 应该被忽略 这是 HTML div class previousPage
  • iOS Objective C - UIWebView 自动填充和执行

    我想知道是否有任何可能的方法可以让 webview 自动输入网站上文本框的值并提交 以便用户可以完全绕过搜索 特别是搜索表单 那么用户只能看到结果 一个例子是 http www eatwellguide org mobile 我注意到查看它
  • 泛型 > 在 collection.sort/ 类似的代码?

    我一直使用类似的接口通过 collection sort 为我的类提供自然排序 基本上 如果我有一个 person 类 我将让它实现 Comparable 接口并提供compareTo 的实现 但是在 javadocs 中 Collecti
  • 在hibernate中如何使用not in子句

    我有两张桌子 就业和生产 生产表有 jobid 字段 该字段引用作业表的 jobid 在hibernate中如何使用not in子句 实现如下sql查询 SELECT FROM jobs where job id not in 从生产中选择
  • 在Python中返回一个角色中的对象并在QML中获取另一个对象的引用

    我正在写一个 Twitter 客户端 我实施了TweetItem and TweetModel 问题是有一个角色TweetItem called original 我希望它指向原始推文 更新 我的代码中有一些拼写错误 现在我修复了它们 im
  • 如何定义静态运算符<<?

    是否可以定义一个仅对类的静态成员进行操作的静态插入运算符 就像是 class MyClass public static std string msg static MyClass operator lt lt const std stri
  • 在 OpenGL 中绑定零纹理

    在我的程序中 我使用 2 个纹理 t0 和 t1 t1 是附加的 仅在某些情况下需要 glActiveTexture GL TEXTURE1 if mDisplayMode EDM DEFAULT glBindTexture GL TEXT
  • 如何获取特定月份和年份的天数

    我有一个传递两个参数 Month 和 Year 的方法 我会这样调用这个方法 MonthDates January 2010 public static string MonthDates string MonthName string Y
  • iPhone——以编程方式创建UITabBar?

    如何做到这一点 请注意 我不想要控制器 只是一个标签栏 UITabBar aTabBar UITabBar alloc initWithFrame aFrame 您可能需要参考类文档例如像这样的方法 setItems animated 另外
  • 如何在 CSS 和 HTML 中创建价格标签形状

    所以我找到了这个答案 CSS3菜单形状 样式但不知道如何将其放在左侧 我已经搜索过了 但没有运气 这就是我想要实现的目标 我也发现了这个 改变三角形的形状 我怎样才能让它在另一侧工作 我的意思是箭头需要位于左侧 是否可以用一个来做到这一点d
  • 在matlab中用渐变灰度颜色绘制圆

    我想在matlab中画一个带有渐变颜色的圆 但我不能 有谁可以帮助我吗 示例图片可以在这里找到 这是一种方法 N 200 this decides the size of image X Y meshgrid 1 1 N 1 1 1 N 1
  • r 过滤具有唯一字段的数据框[重复]

    这个问题在这里已经有答案了 我给出了编码和 R 的第一步 但我遇到了一个问题 我有一个具有以下格式的数据框 Months Person April Person1 May Person2 April Person1 June Person
  • 自定义属性编辑器不适用于 Spring MVC 中的请求参数?

    我正在尝试使用 Spring 注释创建一个多操作 Web 控制器 该控制器将负责添加和删除用户配置文件并为 jsp 页面准备参考数据 Controller public class ManageProfilesController Init