Results 1 to 3 of 3

Thread: Model for the View

  1. #1
    Join Date
    Jun 2006
    Posts
    3

    Default Model for the View

    Hi all,
    I'm quite new to WebFlow and the Spring Framework in general, and not very sure of my ideas yet.

    When defining a view-state within a flow, you can define a view for it.
    Is there also a convenient way to define model map available for this view?

    What I am doing now is subclassing org.springframework.webflow.executor.mvc.FlowContr oller and overriding its toModelAndView method so it associates models and views. It looks more or less like this:
    Code:
    public class MyFlowController extends FlowController {
        
        private MyModelClass myModel;
        ...
    
        protected ModelAndView toModelAndView(ResponseInstruction response, ExternalContext context) {
            if (response.isApplicationView()) {
                // forward to a view as part of an active conversation
                ApplicationView view = (ApplicationView)response.getViewSelection();
                Map model = new HashMap(view.getModel());
                
    <NEW CODE>  String viewName = view.getViewName();
                
                // set model for main view
                if(viewName.equals("main")) {
                    model.put("modelForMain", myModel.getForMain());
                }
                // set model for another view
                else if (viewName.equals("another")) {
                    model.put("modelForAnother", myModel.getForAnother());
                }
                // set model requiring a parameter
                else if (viewName.equals("viewWithParameter")) {
                    int id = Integer.parseInt(context.getRequestParameterMap().get("id"););
                    model.put("paramModel", myModel.getWithParameter(id));
    </NEW CODE> }
                
                this.getArgumentExtractor().put(response.getFlowExecutionKey(), model);
                this.getArgumentExtractor().put(response.getFlowExecutionContext(), model);
                return new ModelAndView(view.getViewName(), model);
            }
            else {
                return super.toModelAndView(response, context);
            }
        }
    Is it a proper way of using models ?
    and - Can it be done without writing any code in java?

  2. #2
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

    Default Use entry-actions instead

    Hi smk,

    Quote Originally Posted by smk
    Is it a proper way of using models ?
    No, not really.

    There is no need to extend FlowController in such situations. Instead, you should add new custom methods to a custom Action. Using the context (i.e., the RequestContext which is passed as a parameter to action methods), you can place data for your model in the corresponding scope (request, flow, etc.).

    Quote Originally Posted by smk
    Can it be done without writing any code in java?
    Yes, an alternative to coding custom action methods is to use POJO services as actions. Consult the reference manual for how to do this.

    This assumes that you have already implemented POJOs or POJO services that take care of your business logic. Of course, if your use case is complex, you will likely have to write some (new) Java code somewhere.

    As an example, take a look at the shipping rate sample provided with SWF. You'll see in getRate-flow.xml (from SWF 1.0 RC2), that you can use entry-actions to accomplish what you were trying to do with your custom FlowController. Here's an excerpt from that flow configuration:

    Code:
    	<view-state id="selectSender" view="selectSender">
    		<entry-actions>
    			<action bean="rateService" method="getCountries" result-name="countries"/>
    		</entry-actions>
    		<transition on="submit" to="selectReceiver">
    			<action bean="formAction" method="bindAndValidate">
    				<attribute name="validatorMethod" value="validateSender"/>
    			</action>
    		</transition>
    	</view-state>
    Hope this helps,

    Sam

  3. #3
    Join Date
    Jun 2006
    Posts
    3

    Default

    Quote Originally Posted by sbrannen
    Hope this helps
    It was exactly what I was looking for, big thanks.

    Took me some time to find out that it's result-name in RC2 and resultName in RC1 I was using, though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •