Announcement Announcement Module
Collapse
No announcement yet.
"Just in time" SuccesssView in SearchProductsContr Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • "Just in time" SuccesssView in SearchProductsContr

    Hi ,

    May be this is a newbie question.

    There is a SearchProductsController which implement interface Controller in jpetstore . (most of the time the interface Controller use to do search action)

    The SearchProductsController in jpetstore return a ModelAndView.

    Code:
    public ModelAndView handleRequest(..){
    
    	...
    
    	return new ModelAndView("SearchProducts", "productList", productList);
    
    }
    The above "SearchProducts" is a view that hardcode in the controller ,if we don't want hardcode it , we can add a property successView in the class , and get/set it , then use IOC to inject the view "SearchProducts"
    into the SearchProductsController in petstore-servlet.xml.

    Since SearchProductsController in jpetstore just return SAME successView for each jsp pages , but in my imagination scenario , SearchProductsController can be reuse in many jsp pages to display products , it may need DIFFERENT successView.

    Let's consider the folowing scenario , l have two views , View1.jsp and View2.jsp .

    View1.jsp -- have two forms .

    form1 use SimpleformController to add something to DB.
    form2 use SearchProductsController to search products . After a success search , it will back to View1.jsp and display products.

    View2.jsp -- have two forms also.

    form3 use SimpleformController to edit something to DB.
    form4 use SearchProductsController to search products . After a success search , it will back to View2.jsp and display products.


    both REUSE the SAME SearchProductsController to display products in the SAME pages.


    If the description above OK , then l first came to the code bellow ,


    Code:
      <bean id="searchProductsControllerForView1" class="com.amazon.web.SearchProductsController">
                  <property name="store"><ref bean="store"/></property>
                  <property name="successView"><value>View1</value></property>
            </bean>
    
    	<bean id="searchProductsControllerForView2" class="com.amazon.web.SearchProductsController">
                  <property name="store"><ref bean="store"/></property>
                  <property name="successView"><value>View2</value></property>
            </bean>
    Then my question is , if l want to REUSE SearchProductsController in N jsp pages , then l have to write above code N times ? initialize MULTI SearchProductsController instances ?

    Or

    Make a "just in time SuccesssView" from View1.jsp/View2.jsp using request's parameter to "inject" a successView into view's SearchProductsController , it seen a lot security problem here ??

    Any thought ?

  • #2
    Note that the SearchProductsController is hardcoding a "logical view name", not a reference to the physical view resource. A view resolver will resolve the view name and select an appropriate view implementation.

    I see 2 possible solutions to your problem:

    1) low tech: Use a successView request parameter, as you already suggest. This is the approach taken by most web applications (e.g. Confluence uses an "os_destination" parameter). You could try to plug the security issue that this introduces by not using a clear text view name in the parameter, but some encoded form, and then have a custom ViewResolver to resolve that encoded view name.

    2) high tech: Use Spring web flow.

    Maybe you could also use a custom ViewResolver implementation to fix the problem.

    Erwin

    Comment


    • #3
      ya , l tried spring webflow , it don't have the problem to REUSE the SearchProductsController, but my question is how to do it in spring MVC without using parameter injection ? ( l personally don't like the idea , l did it before , but cause a lot problems).

      In struts , it seen no problem with this .

      from the code below , l can REUSE the controllers N times l like ( the code is from the Struts 2.4 example application)

      Code:
      <action path="/Welcome1"
      type="org.apache.struts.webapp.example.WelcomeAction">
      <forward name="failure" path="/Error1.jsp" />
      <forward name="success" path="/welcome2.jsp" />
      </action>
      
      <action path="/Welcome2"
      type="org.apache.struts.webapp.example.WelcomeAction">
      <forward name="failure" path="/Error2.jsp" />
      <forward name="success" path="/welcome3.jsp" />
      </action>
      those people using spring MVC did not encounter the problem l have ?

      moon

      Comment


      • #4
        l tested already , searchProductsControllerForView1 and searchProductsControllerForView2 are DIFFERENT instances if l wrote it the way in my first post , but spring promote SINGLETON , right ? or my understanding of the concept of singleton is wrong.

        neither the way below to REUSE the SearchProductsController is SINGLETON, they are DIFFERENT instances too !

        Code:
        <bean name="/store/searchProductsControllerForView1.htm" class="com.amazon.web.SearchProductsController"> 
                      <property name="store"><ref bean="store"/></property> 
                      <property name="successView"><value>View1</value></property> 
                </bean> 
        
        <bean name="/store/searchProductsControllerForView2.htm" class="com.amazon.web.SearchProductsController"> 
                      <property name="store"><ref bean="store"/></property> 
                      <property name="successView"><value>View2</value></property> 
                </bean>
        They are NO way to REUSE controllers in spring MVC without multi instants ?

        moon

        Comment


        • #5
          I don't think Spring promotes singletons, and certainly not when they're not appropriate.

          I don't see the reason why you are so keen in having a single instance of the controller, configured differently depending on the usage situation?

          I would argue that the piece of Struts config and the piece of Spring MVC config you posted are logically the same:
          * they reuse a single controller implementation class
          * and configure it differently for each usage

          Erwin

          Comment


          • #6
            Surely you actually don't want the same instance of the controller in both cases. Else how could you have different values for the successView property?

            But beans "/store/searchProductsControllerForView1.htm" and "/store/searchProductsControllerForView2.htm" are both singletons in the sense that a call to getBean("/store/searchProductsControllerForView2.htm") will always return the same instance.

            Comment


            • #7
              Sorry for late reply.

              I would argue that the piece of Struts config and the piece of Spring MVC config you posted are logically the same:
              * they reuse a single controller implementation class
              * and configure it differently for each usage
              Yes , you are right , Erwin.

              Struts , Webwork2 and Spring MVC are the ALL same about these. For Webwork2 , it stated in their docs.pdf (version 2.1.7) pg.185 ,

              Each action should be configured within a Spring application context as a prototype (because WebWork assumes a new instance of a class for every action invocation).
              , although ALL framework do the same thing , is that mean l cannot REUSE the simple searchProductsController ( just a simple controller that implements Controller )? NO , right ? this is just a trivial REUSE problem --> searchProductsController CAN be reused is an established premise. , but may be not the the way these frameworks did .

              Surely you actually don't want the same instance of the controller in both cases. Else how could you have different values for the successView property?
              No, l DO want the same instance of the controller in both cases.

              my point of view is , because spring MVC ( l did not check the other two frameworks) let succesView enter (as a properties) to the controller , so cause the difficulty to have "different values for the successView property" . But what if l take out the succesView as a properties from the Controller , use another way to inform searchProductsController go to the correct page after a success action ? like spring-webflow did ? , spring-webflow can reuse the searchProductsController N times in different flows , but just write once for the searchProductsController bean in xxx-servler.xml , and of course it is a single instance.

              I don't see the reason why you are so keen in having a single instance of the controller, configured differently depending on the usage situation?
              because l found way in spring-webflow , but not spring original MVC.

              There are two criterias in my head,

              Criteria 1
              It is not the responsibility for the client to decide where to go.
              -- means that client cannot inject successView into controller.

              Criteria 2
              Neither is the responsibility of the controller.
              -- means that successView enter the Controller as properties cause the difficulty l described above.

              Am l wrong in my point of view ?

              moon

              Comment

              Working...
              X