Results 1 to 6 of 6

Thread: webflow 2.2 simple jsp page routing

  1. #1

    Default webflow 2.2 simple jsp page routing

    hi guys

    Any help will be appreciated
    I am using spring webflow 2.2
    I have 2 links on the jsp page but each link should point to a different jsp. I have only one flow xml file (app.xml).
    Here is the code what i have so far
    1.)
    My jsp is
    <form action="app.htm" method="post">
    <center>
    <h4>

    </h4>
    </center>
    <DIV align="left">
    <br>
    <a href="app.htm">App 1 </a> //when i click this should invoke my app1.jsp in my web flow

    <br>
    <br>
    <a href="app.htm">App 2</a> //when i click this should invoke my app2.jsp in my web flow
    <br><br>
    </DIV>
    </form>

    2.)
    this is my spring config file

    <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
    <webflow:flow-location path="/WEB-INF/flows/app.xml"/>
    </webflow:flow-registry>

    <webflow:flow-builder-services id="flowBuilderServices" conversion-service="conversionService" view-factory-creator="viewFactoryCreator"/>
    <bean id="conversionService" class="nj.lwd.courts.services.ApplicationConversio nService">
    </bean>

    <bean id="viewFactoryCreator" class="org.springframework.webflow.mvc.builder.Mvc ViewFactoryCreator">
    <property name="viewResolvers" ref="myviewResolver" />
    </bean>

    <!-- Resolves flow view names to .jsp templates -->
    <bean id="myviewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
    </bean>


    3.)
    and this is my app.xml (my flow)

    <view-state id="search" view="\app1" model="searchCriteria"> //invoked by link App 1 from jsp
    <transition on="search" to="displaySearchResults">
    <evaluate expression="ActionBean.search(searchCriteria)" result="flowScope.resul" />
    </transition>
    </view-state>
    <view-state id="displaySearchResults" view="\app1">
    </view-state>

    <view-state id="research" view="\app2" model="searchCriteria"> //invoked by link App 2 from jsp
    <transition on="research" to="displayMemoryResults">
    <evaluate expression="ActionBean.research(searchCriteria)" result="flowScope.resultList" />
    </transition>
    </view-state>
    <view-state id="displayMemoryResults" view="\xyz">
    </view-state>


    so basically My requirment is on my jsp page when user clicks on App1 it should invoke app1.jsp and when user clicks on App2 it should invoke app2.jsp
    i am not sure how i should tweak my above code.

    Any help greatly appreciated

    thanks

  2. #2
    Join Date
    Nov 2008
    Posts
    742

    Default

    Well, there's a few things we'll need to fix here. I'm assuming this is your first time using webflow, and you have yet to get something up and running as expected. It would also help to tell us what happened when you tried to use the code and jsp you posted.

    First of all, you'll need to decide whether the links on this page should launch your flow, or if your first page should be a part of the flow itself, meaning you've already launched the flow by the time this page is reached.

    If you want each of the links to launch the flow, then you'll need your links to specify the URL corresponding with your "app" flow, and you'll need to add a different request parameter to each of these so you can figure out which view-state to render.

    The idea behind this is simple: you are launching a flow, so the starting state is the first state in your flow definition, in this case your "search" view-state. In order to support some kind of conditional behavior, YOU need to add this yourself to the flow definition, either with a decision-state or an action-state, evaluating the request parameter you passed in when the flow was launched. Then that decision (or action) state can transition to the corresponding view-state.


    As an alternate approach, if you have the ability to add this page to the flow itself, it will be better design. That way, the user already clicked a button or URL to launch the flow, and your first state will be a view-state corresponding with your page. Then you'll just need to change your form action and your links in order to invoke the correct transitions to your other view-states. Your form action would have to change to:
    action="${flowExecutionUrl}"

    and your links would have to change according to the guidlines in the SWF reference guide.

  3. #3

    Thumbs up nice

    great,got it working.

    I have a question on dependency injection. In a practical scenario like mine where would i use it ? My application is a simple j2ee app where user enters
    data on a jsp page and i am using webflow to call my business class and using jdbc to call my postgres sql.

    right now i have a search criteria object which is used to hold the user entered values in the jsp like id, description,ect, so i am using this peice of code in my spring config file to instantiate my searchcriteria object

    <bean id="searchCriteria" class="com.shopping.beans.SearchCriteria" />

    and using it in my jsp like this
    <form:form commandName="searchCriteria" method="post">
    ..

    Other than searchCriteria object what other objects can i instantiate to make use of dependency injection?

    thanks for the help.

    thanks.

  4. #4
    Join Date
    Nov 2008
    Posts
    742

    Default

    Well, for one, your search criteria bean is by default in Singleton scope, by the look of things, and I don't think this is at all what you want to do. If you have multiple users, they'll be binding values to the EXACT same object. Not good.

    A better way to do this, I think, is to define searchCriteria as a flow-scoped variable in your flow definition, like so.

    You should probably read up more on Spring dependency injection and go through the webflow reference guide. As far as scoping goes, it's best to make it as tight as possible so your attributes get cleaned up as soon as they are no longer needed.

    For a simple application like yours, you may not need much dependency injection. Still, going through the Spring documentation, or even google searching for examples or articles or usage might give you a better idea of approaches you might use.

  5. #5

    Smile

    >>>>If you have multiple users, they'll be binding values to the EXACT same object. Not good.

    but..if there are multiple users, they all have their own instances,right?
    How will they be binding to the EXACT same object?
    Every user has their own spring container environment right?

  6. #6
    Join Date
    Nov 2008
    Posts
    742

    Default

    I think you'll need to read up on the Spring IOC container. Also pay attention to bean scopes.

    The Spring container environment is app-wide. Beans where you don't specify a scope are singleton by default, like controllers, so there will only be a single instance of your search criteria bean.

    If you want a separate instance per user, you need some way to get a new instance of the bean in either session scope, conversation scope, flow scope, or view scope. Since you want to use it as a model object, you can't use view scope. Since you only want to use it for these two pages, and since these two pages are only used within your flow, flow scope seems to be the best bet.

    Now, there are a few ways to make this work the way you want.

    My suggestion is likely the easiest. Use the var element within your flow definition. That creates a new instance, and scopes it to the flow. At that point you won't even need to declare it as a bean.


    Alternatively, you can keep it as a bean, but scope it to the flow. There's some extra step you need to take to make this work, though, since Spring by default doesn't know about the extra scopes provided by SWF. You'll need to add something to your beans file, but I don't recall what it is, and I don't think it's documented well in the SWF reference guide. You might need to search through the forums here to find it. By now, maybe it's been changed so it automatically registers the extra scopes, but I'm not sure.


    The third way to do this (and the way I favor, for beans which require spring-supplied autowiring or configuration) is defining the bean in your beans file, but using prototype scope, and exposing the bean under the name: "new<beanName>".

    That way I can use a set action to put a new instance of the bean into whatever scope I wish, like so:

    Code:
    <set name="viewScope.searchCriteria" value="newSearchCriteria"/>
    I usually prefer this because I have good visibility into what beans are used by the flow. If I'm using annotations, or specifically scoped beans, I lose that visibility.

Posting Permissions

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