4

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" />

ただし、マップされたURLを開くと、例外が発生します。

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

私はSpring2.5.6を使用しており、それほど遠くない将来にSpring3.0に移行する予定です。ただし、このJIRA https://jira.springsource.org/browse/SPR-4182によると、春の2.5.1ですでに可能であるはずです。

デバッグは、InitBinderメソッドが正しく呼び出されていることを示しています。

私は何が間違っているのですか?

アップデート:

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からログに記録されたエラーはありません。

4

1 に答える 1

3

fieldの引数を指定したくないと思いますWebDataBinder.registerCustomEditor()。これはフォームバッキングオブジェクトと一緒に機能することを目的としており、使用していません。

代わりに、より単純な2引数の方法を試してください。これで、うまくいくはずです。

binder.registerCustomEditor(UserAccount.class, new UserAccountPropertyEditor(userManager)); 
binder.registerCustomEditor(Profile.class, new ProfilePropertyEditor(profileManager));
于 2010-06-08T20:10:09.430 に答える