简体   繁体   中英

Session attribute as method argument with Spring MVC

I'm using Bozho solution to create a session attribute annotation under Spring mvc.
I've a problem when the object is not in the session and I've something like

@SessionAttribute(required = false) MyFormObject myFormObject

The annotation is defined as

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SessionAttribute {
    String value() default "";
    boolean required() default true;
    String defaultValue() default ValueConstants.DEFAULT_NONE;
}

I get an exception since defaultValue() returns a String and my custom ArgumentResolver tries to assign this String to MyFormObject .

How can I instruct Spring to assign null to myFormObject instead of ValueConstants.DEFAULT_NONE ?

您不能简单地修改自定义WebArgumentResolver以在找不到属性时返回null而不是defaultValue吗?

the defaultValue can be the username of the user, which you can find by injecting a dao into the resolver. But having a default user doesn't make much sense anyway, so just return null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM