Deadron
Deadron

Reputation: 5289

Can I prevent a ModelAttribute method from being called during the Action phase?

I have the following class

@Controller
@RequestMapping( "VIEW" )
@SessionAttributes( "application" )
public class EditApplicationController {

    private static final Logger logger = LoggerFactory.getLogger( EditApplicationController.class );

    private final ApplicationService applicationService;

    @Autowired
    public EditApplicationController( ApplicationService appServ ) {
        this.applicationService = appServ;
    }

    @RenderMapping( params = "action=editApplicationForm" )
    public String showEditApplication() {
        return "editApplicationForm";
    }

    @ActionMapping( params = "action=editApplication" )
    public void editApplication( @Valid ApplicationDTO application ) {
        try {
            this.applicationService.updateApplication( application );
        } catch ( final ApplicationNotFound e ) {
            logger.error( "No application exists with the given application id." );
        }
    }

    @ModelAttribute( "application" )
    public Application getApplication( @RequestParam long appId ) throws ApplicationNotFoundException {
        final Application app = this.applicationService.findApplicationById( appId );

        if ( app == null ) {
            throw new ApplicationNotFoundException();
        }

        return app;
    }
}

The issue is when I am in the Action phase I do not want or need a ModelAttribute retrieved since I am dealing entirely with the submitted form object. I can make the request param non required and perform a null check or I could add a new controller solely for performing the action but both of these feel clunky.

Upvotes: 0

Views: 70

Answers (0)

Related Questions