7

I come from some experience with MVC frameworks and recently I started to get interested in Spring. I think it's a good framework, for what I've seen until now. Anyway, in my past experiences I was used to a different programming style, especially for the structure of controllers. The way I was used to to employ controllers was different. Of course I used to map somehow a request to them (think of structs with various actions), but then what I really liked was that you could pass actions to other controllers, then they would remain the "active" controller and those would be responsible of handling the next request, may it come from an HTTP request or from something else. I did enjoy this because it was very good to keep the state of a user session in an automated way, making code clean and separating different situations in different controllers. Now I've read Spring Web MVC and the docs only talk about controllers that answer requests, but they don't keep state and there's no word about chaining controllers (apart from forwarding) and state retention.

How do you handle these topics in spring, is there a different way, or should I implement my own stateful controllers and state/action classes?

I hope my question is clear enough and I apologize for its broadness.

3
  • In what situations would you want to have chaining controllers like this? Commented Jan 5, 2011 at 16:27
  • I usually tie every simple activity to one controller, so that I can isolate the logic of that action (a number of queries, views and data manipulations). Then when the user enters another activity I usually forward everything to the controller that does it. Thus it is easier to mantain and you can reuse code. Commented Jan 5, 2011 at 17:46
  • Let's make an example: suppose I have a controller that takes care of showing a view with telephone numbers and let's you choose, add, edit or remove them. In any situation where I need to pick a number in my app, I would forward to this controller, then have it return the picked number. That's what I meant Commented Jan 6, 2011 at 10:49

2 Answers 2

8

In Java, the difference between stateful / stateless web frameworks is usually described as the difference between action-based and component-based frameworks.

Both approaches are valid, but usually when you choose a Framework you are stuck with that framework's way of doing it:

Popular Action Frameworks:

Popular Component Frameworks:

(There are of course many more, in both categories)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but I'm not choosing a framework, I want to use Spring, so my question was more about programming practices in Spring
@gotch4 and I answered your question. The answer is "Spring MVC doesn't work that way"
Some (including me) find division to Action and Component frameworks not quite correct. Components may have component-level actions and actions may involve several components to work. For instance - what category would you assign to HybridJava (hybridserverpages.com)?
5

In Spring MVC you could use the stateless controllers forwarding the request / invoking session scoped beans.

But if this is a good idea or not strongly depends on your use case. -- I would do this only if I there are only a few of this "strange" controllers. -- If you have a lot of them I think it is worth to look for an other solution. (for example JSF with Spring)

2 Comments

Example of session scoped beans?
Session scoped beans are beans (a.k.a. java beans) thare are stored in the HttpSession. Read this: static.springsource.org/spring/docs/3.0.x/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.