GeeCON Prague 2014*
The moose likes Spring and the fly likes How to do both? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to do both?" Watch "How to do both?" New topic
Author

How to do both?

Mimi Tam
Ranch Hand

Joined: Mar 05, 2010
Posts: 121
Hi,

In the MVC model, when the Controller is handling a POST like below, it returns a View (jsp) with the stuff posted.


Using the MVC model, how can I send an HTTP Response (in bytes) right back to the HTTP Requestor AND have the HTTP Response show up in a View like what it is doing now?

The "return "redirect:/business/"+business.getId();" seems to only allow me to show the data in a View. It is an action taken by the Server side. In addition to showing the View, I need to return the whole HTTP Response in a stream of bytes so the client can also display this Response Message (in a stream of bytes) on whatever mobile phone that s/he is using.

Am I making sense or misunderstanding some important points about the MVC model?

Any help will be greatly appreciated.







Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

I can recommend looking at Spring 3.0 and their implementation of RESTful WebServices. From what you describe, it seems like this might be the best and easiest approach to have a mobile device get the data instead of a "web page"

Good Luck

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mimi Tam
Ranch Hand

Joined: Mar 05, 2010
Posts: 121


I can recommend looking at Spring 3.0 and their implementation of RESTful WebServices


Hi Mark,

Did you mean the sample projects that come with the Spring 3.x dist? If possible, can you point me to a specific implementation (what, where/which sample project?) that will help me out, please?

I saw many samples that deal with sending data via the HttpServletRequest and HttpServletResponse pair but I have not (or simply do not recognize) found one that I can understand of doing both.

Many Thanks to your response. Can you direct me to specific sample project(s) that I can model after, please?

Much Obliged
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

I wasn't referring to any code.

This has little bit, but all it is is your Controller returns a JsonView object

return new JSonView

Just have the Jackson libarary jar in your classpath

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html#view-json-mapping

For Spring RESTful Web Services you just need to include the ContentNegotiatingViewResolver as a bean definition

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-multiple-representations

Good Luck

Mark
Mimi Tam
Ranch Hand

Joined: Mar 05, 2010
Posts: 121

Thank you again so very much for your response and info, Mark.

Unless I really misunderstood the usage of ContentNegotiatingsViewResolver and JsonView objects (if so, please straighten me out), these mechanisms are still only allowing one Response back (i.e. either this View, that View, writing to stream, or pass void to indicate that the Request has been taken care of ....and such).

I am however, trying to find out how to indicate in (return from) the Controller in order to display the data in a particular View as what I do most of the time AND at the same juncture to have the HTTP Response message sent back to the Requestor with XML data.

Hope I am making sense

Any info and ideas will be greatly appreciated.



Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

In Http, there is one response per one request. You can't ask for two different responses for a single request.

Mark
Mimi Tam
Ranch Hand

Joined: Mar 05, 2010
Posts: 121

Thanks very much for your response, Mark.

I was thinking whether I could use the MVC model to send back an HTTP response as usual but before returning from the Controller to direct data to a predefined View for display as well. So, this is an extra action to take besides responding to the request using the normal route in the MVC structure one way or the other.

I dug into the MVC code somewhat and found a manual but immensely ugly way to achieve this. Having to do this in the MVC model must not be such a novelty and I wonder how folks handle this requirement if they ever had to

Thanks for your follow up and I look forward to hearing any other ideas that anyone may have.

Many Thanks....Mimi

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Mimi Tam wrote:
Thanks very much for your response, Mark.

I was thinking whether I could use the MVC model to send back an HTTP response as usual but before returning from the Controller to direct data to a predefined View for display as well. So, this is an extra action to take besides responding to the request using the normal route in the MVC structure one way or the other.

I dug into the MVC code somewhat and found a manual but immensely ugly way to achieve this. Having to do this in the MVC model must not be such a novelty and I wonder how folks handle this requirement if they ever had to

Thanks for your follow up and I look forward to hearing any other ideas that anyone may have.

Many Thanks....Mimi



Do you mean like return a PDFView or JstlView object from your Controller method. You can definitely do that too, but not both at the same time.

Mark
 
GeeCON Prague 2014
 
subject: How to do both?