Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 863197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:16:18+00:00 2026-05-15T09:16:18+00:00

Here goes a command object which needs to be populated from a Spring form

  • 0

Here goes a command object which needs to be populated from a Spring form

public class Person {

    private String name;
    private Integer age;    

    /**
      * on-demand initialized
      */
    private Address address;

    // getter's and setter's

}

And Address

public class Address {

    private String street;

    // getter's and setter's

}

Now suppose the following MultiActionController

@Component
public class PersonController extends MultiActionController {

    @Autowired
    @Qualifier("personRepository")
    private Repository<Person, Integer> personRepository;

    /**
      * mapped To /person/add
      */
    public ModelAndView add(HttpServletRequest request, HttpServletResponse response, Person person) throws Exception {
        personRepository.add(person);

        return new ModelAndView("redirect:/home.htm");
    }

}

Because Address attribute of Person needs to be initialized on-demand, i need to override newCommandObject to create an instance of Person to initiaze address property. Otherwise, i will get NullPointerException

@Component
public class PersonController extends MultiActionController {

    /**
      * code as shown above
      */

    @Override
    public Object newCommandObject(Class clazz) thorws Exception {
        if(clazz.isAssignableFrom(Person.class)) {
            Person person = new Person();
            person.setAddress(new Address());

            return person;
        }
    }

}

Ok, Expert Spring MVC and Web Flow says

Options for alternate object creation include pulling an instance from a BeanFactory or using method injection to transparently return a new instance.

First option

  • pulling an instance from a BeanFactory

can be written as

@Override
public Object newCommandObject(Class clazz) thorws Exception {
    /**
      * Will retrieve a prototype instance from ApplicationContext whose name matchs its clazz.getSimpleName()
      */
    getApplicationContext().getBean(clazz.getSimpleName());
}

But what does he want to say by using method injection to transparently return a new instance ??? Can you show how i implement what he said ???

ATT: I know this funcionality can be filled by a SimpleFormController instead of MultiActionController. But it is shown just as an example, nothing else

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:16:19+00:00Added an answer on May 15, 2026 at 9:16 am

    I’m pretty sure he means using the lookup-method system as documented in chapter 3 of the spring reference manual

    Only down side is that <lookup-method> requires a no arg method rather than the newCommandObject(Class) method of MultiActionController.

    this can be solved with something like:

    public abstract class PersonController extends MultiActionController {
    
        /**
          * code as shown above
          */
    
        @Override
        public Object newCommandObject(Class clazz) thorws Exception {
            if(clazz.isAssignableFrom(Person.class)) {
                return newPerson();
            }
        }                          
    
        public abstract Person newPerson();
    }
    

    in the context file:

    <bean id="personController" class="org.yourapp.PersonController">
      <lookup-method name="newPerson" bean="personPrototype"/>
    </bean>
    

    The down side is that using this sort of thing is you are kinda stuck with configuring the controller bean via xml it’s not possible (certainly in < 3) to do this with annotations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 471k
  • Answers 471k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can't auto-crop algorithmically which is why sites that need… May 16, 2026 at 3:20 am
  • Editorial Team
    Editorial Team added an answer echo ‘yes’; is not a valid sintax try: echo 'yes';… May 16, 2026 at 3:20 am
  • Editorial Team
    Editorial Team added an answer You would simply embed a Silverlight application containing your grid… May 16, 2026 at 3:20 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.