[S2] Results Based on URL File Types
Hi everyone, I'm new to Struts 2 (and Struts in general, for that matter). I've got the Struts 2 framework up and running and I'm trying to figure out how to get it to support a use case for our new application. We'd like to have a set of web services that look like this: http://www.domain.com/struts2/queryName.xml?param1=foo http://www.domain.com/struts2/queryName.json?param1=foo http://www.domain.com/struts2/queryName.html?param1=foo http://www.domain.com/struts2/queryName.rss?param1=foo As you can see, the query and its parameters won't change, but we'd like to alter the result type based on the requested URL file type. How can I best achieve this in Struts 2? I know that I can do some Apache rewriting to map these to the correct Actions, but I'd much rather do it in the struts-config.xml file if I can, that way I can just maintain everything from one location. Also, does anyone have any suggestions for best practices on how to handle different result types? I'd like to reuse some of the same basic framework pieces to handle all Rss, JSON, etc requests so that when I want to add a new query to the web service, it's just a matter of adding another Action to support it, and all of the result types are automatically inherited. Thanks in advance for your help! Kevin Conroy
RE: [S2] Results Based on URL File Types
Kevin, From my knowledge you can't achieve exactly what you're looking for in struts2 because struts2 uses the first part of the last section of the url resource identifier as the key for the action lookup (i.e. for all 4 of your examples the action would be mapped to he same action for queryName). I quick and dirty way around this would be to add an extra directory level for each type, define that as a separate namespace in struts-config, define the struts filter to handle all relevant extensions in web.xml, and then you could have the different types. This would give you something like; http://www.domain.com/struts2/xml/queryName.xml?param1=foo http://www.domain.com/struts2/json/queryName.json?param1=foo http://www.domain.com/struts2/html/queryName.html?param1=foo http://www.domain.com/struts2/rss/queryName.rss?param1=foo Would this work for you? Al. -Original Message- From: Kevin Conroy [mailto:[EMAIL PROTECTED] Sent: 24 May 2007 14:42 To: user@struts.apache.org Subject: [S2] Results Based on URL File Types Hi everyone, I'm new to Struts 2 (and Struts in general, for that matter). I've got the Struts 2 framework up and running and I'm trying to figure out how to get it to support a use case for our new application. We'd like to have a set of web services that look like this: http://www.domain.com/struts2/queryName.xml?param1=foo http://www.domain.com/struts2/queryName.json?param1=foo http://www.domain.com/struts2/queryName.html?param1=foo http://www.domain.com/struts2/queryName.rss?param1=foo As you can see, the query and its parameters won't change, but we'd like to alter the result type based on the requested URL file type. How can I best achieve this in Struts 2? I know that I can do some Apache rewriting to map these to the correct Actions, but I'd much rather do it in the struts-config.xml file if I can, that way I can just maintain everything from one location. Also, does anyone have any suggestions for best practices on how to handle different result types? I'd like to reuse some of the same basic framework pieces to handle all Rss, JSON, etc requests so that when I want to add a new query to the web service, it's just a matter of adding another Action to support it, and all of the result types are automatically inherited. Thanks in advance for your help! Kevin Conroy - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [S2] Results Based on URL File Types
I'm still a newbie as well so take this with a pinch of salt. I would try doing it this way; http://www.domain.com/struts2/queryName?param1=foo,param2=xml http://www.domain.com/struts2/queryName?param1=foo,param2=json http://www.domain.com/struts2/queryName?param1=foo,param2=html http://www.domain.com/struts2/queryName?param1=foo,param2=rss Include getter and setter for param2 in your action and set your result strings as appropriate to get the results you want so, for example after executing your query, you could include in your action method; return getParam2() which would return xml,json,html or rss and then in your struts.xml you'd specify result name=xmlYourResultTypeForXML/result result name=jsonYourResultTypeForJSON/result etc etc Does this help. Regards Roger - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: [S2] Results Based on URL File Types
Just a small observation Roger, whilst you're solution is perfectly workable, you would need to have a big switch statement (or something similar) which could get messy. imho, the use of the extra directory and namespace (i.e. /xml/, /html/, etc.) that I suggested earlier is slightly simpler because it makes use of Struts existing dispatch determining mechanisms to direct the request to the correct handler, but I'd welcome any comments you have if you feel the extra param has other benefits to offer. Al. -Original Message- From: Roger Varley [mailto:[EMAIL PROTECTED] Sent: 24 May 2007 15:38 To: Struts Users Mailing List Subject: Re: [S2] Results Based on URL File Types I'm still a newbie as well so take this with a pinch of salt. I would try doing it this way; http://www.domain.com/struts2/queryName?param1=foo,param2=xml http://www.domain.com/struts2/queryName?param1=foo,param2=json http://www.domain.com/struts2/queryName?param1=foo,param2=html http://www.domain.com/struts2/queryName?param1=foo,param2=rss Include getter and setter for param2 in your action and set your result strings as appropriate to get the results you want so, for example after executing your query, you could include in your action method; return getParam2() which would return xml,json,html or rss and then in your struts.xml you'd specify result name=xmlYourResultTypeForXML/result result name=jsonYourResultTypeForJSON/result etc etc Does this help. Regards Roger - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: [S2] Results Based on URL File Types
Just read the your post again, I see you're using the result as a two step process as oppose to needing a dispatch switch statement, cool idea, but I think it will still be a bit more complex than using the separate directories. -Original Message- From: Al Sutton [mailto:[EMAIL PROTECTED] Sent: 24 May 2007 15:44 To: 'Struts Users Mailing List' Subject: RE: [S2] Results Based on URL File Types Just a small observation Roger, whilst you're solution is perfectly workable, you would need to have a big switch statement (or something similar) which could get messy. imho, the use of the extra directory and namespace (i.e. /xml/, /html/, etc.) that I suggested earlier is slightly simpler because it makes use of Struts existing dispatch determining mechanisms to direct the request to the correct handler, but I'd welcome any comments you have if you feel the extra param has other benefits to offer. Al. -Original Message- From: Roger Varley [mailto:[EMAIL PROTECTED] Sent: 24 May 2007 15:38 To: Struts Users Mailing List Subject: Re: [S2] Results Based on URL File Types I'm still a newbie as well so take this with a pinch of salt. I would try doing it this way; http://www.domain.com/struts2/queryName?param1=foo,param2=xml http://www.domain.com/struts2/queryName?param1=foo,param2=json http://www.domain.com/struts2/queryName?param1=foo,param2=html http://www.domain.com/struts2/queryName?param1=foo,param2=rss Include getter and setter for param2 in your action and set your result strings as appropriate to get the results you want so, for example after executing your query, you could include in your action method; return getParam2() which would return xml,json,html or rss and then in your struts.xml you'd specify result name=xmlYourResultTypeForXML/result result name=jsonYourResultTypeForJSON/result etc etc Does this help. Regards Roger - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [S2] Results Based on URL File Types
Hi Roger (and Al), Thanks for your replies. I had thought about doing something like what you specified Roger, by passing the return type as a parameter. However, in keeping with what seems to be fairly standard RESTful web service protocol, I was hoping to have the return type specified after the query name. Then from the users perspective the URL file type that the request matches the return type, rather than it just being some random query. It's true that I could also build the return type into the URL path, but that would give me several different actions for each query, and I'd like to have one Action object that knows how to get all of the POJOs that are associated with the query and then use the results to mux out how to serialize the POJOs into the correct format (e.g. REST xml, json, rss, etc.) So is there any way to tell Struts to ignore the extension (*.do or *.action) and just assume that all requests into the application (or namespace) are in fact actions it should execute? I'm suspecting not, as this might block access to other non-action resources, but if there is some way to set up a * pattern, that might work too. Thanks for all of your help. -- Kevin Conroy E-mail: [EMAIL PROTECTED] On 5/24/07, Roger Varley [EMAIL PROTECTED] wrote: I'm still a newbie as well so take this with a pinch of salt. I would try doing it this way; http://www.domain.com/struts2/queryName?param1=foo,param2=xml http://www.domain.com/struts2/queryName?param1=foo,param2=json http://www.domain.com/struts2/queryName?param1=foo,param2=html http://www.domain.com/struts2/queryName?param1=foo,param2=rss Include getter and setter for param2 in your action and set your result strings as appropriate to get the results you want so, for example after executing your query, you could include in your action method; return getParam2() which would return xml,json,html or rss and then in your struts.xml you'd specify result name=xmlYourResultTypeForXML/result result name=jsonYourResultTypeForJSON/result etc etc Does this help. Regards Roger - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: [S2] Results Based on URL File Types
If you don't have any other resources ending in .xml .html .etc inside your webapp you can modify the filter-mapping, if you do then you might have to consider the idea of a directory in which all objects are mapped to actions and a separate directory where they are not, e.g.; http://domain.com/struts2/stuff.html -- Static HTML http://domain.com/struts2/dynamic/stuff.html -- Goes to an action. Al. -Original Message- From: Kevin Conroy [mailto:[EMAIL PROTECTED] Sent: 24 May 2007 15:51 To: Struts Users Mailing List Subject: Re: [S2] Results Based on URL File Types Hi Roger (and Al), Thanks for your replies. I had thought about doing something like what you specified Roger, by passing the return type as a parameter. However, in keeping with what seems to be fairly standard RESTful web service protocol, I was hoping to have the return type specified after the query name. Then from the users perspective the URL file type that the request matches the return type, rather than it just being some random query. It's true that I could also build the return type into the URL path, but that would give me several different actions for each query, and I'd like to have one Action object that knows how to get all of the POJOs that are associated with the query and then use the results to mux out how to serialize the POJOs into the correct format (e.g. REST xml, json, rss, etc.) So is there any way to tell Struts to ignore the extension (*.do or *.action) and just assume that all requests into the application (or namespace) are in fact actions it should execute? I'm suspecting not, as this might block access to other non-action resources, but if there is some way to set up a * pattern, that might work too. Thanks for all of your help. -- Kevin Conroy E-mail: [EMAIL PROTECTED] On 5/24/07, Roger Varley [EMAIL PROTECTED] wrote: I'm still a newbie as well so take this with a pinch of salt. I would try doing it this way; http://www.domain.com/struts2/queryName?param1=foo,param2=xml http://www.domain.com/struts2/queryName?param1=foo,param2=json http://www.domain.com/struts2/queryName?param1=foo,param2=html http://www.domain.com/struts2/queryName?param1=foo,param2=rss Include getter and setter for param2 in your action and set your result strings as appropriate to get the results you want so, for example after executing your query, you could include in your action method; return getParam2() which would return xml,json,html or rss and then in your struts.xml you'd specify result name=xmlYourResultTypeForXML/result result name=jsonYourResultTypeForJSON/result etc etc Does this help. Regards Roger - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [S2] Results Based on URL File Types
Yes, you can tell Struts to not use any action extension at all. I'm doing that right now for a Restful application I developed. I haven't tried, but you might be able to use wildcards to match the extension in the action mapping and perhaps use a custom dispatcher result to determine which actual result should be executed: action name=queryName.* class=com.example.MyAction result type=extDispatch param name=extension{0}/param /result /action I'd love to see this type of feature either built into Struts or as a plugin, so if you discover a solution that involves Struts improvements, please file a JIRA ticket. Thanks, Don On 5/25/07, Kevin Conroy [EMAIL PROTECTED] wrote: Hi Roger (and Al), Thanks for your replies. I had thought about doing something like what you specified Roger, by passing the return type as a parameter. However, in keeping with what seems to be fairly standard RESTful web service protocol, I was hoping to have the return type specified after the query name. Then from the users perspective the URL file type that the request matches the return type, rather than it just being some random query. It's true that I could also build the return type into the URL path, but that would give me several different actions for each query, and I'd like to have one Action object that knows how to get all of the POJOs that are associated with the query and then use the results to mux out how to serialize the POJOs into the correct format (e.g. REST xml, json, rss, etc.) So is there any way to tell Struts to ignore the extension (*.do or *.action) and just assume that all requests into the application (or namespace) are in fact actions it should execute? I'm suspecting not, as this might block access to other non-action resources, but if there is some way to set up a * pattern, that might work too. Thanks for all of your help. -- Kevin Conroy E-mail: [EMAIL PROTECTED] On 5/24/07, Roger Varley [EMAIL PROTECTED] wrote: I'm still a newbie as well so take this with a pinch of salt. I would try doing it this way; http://www.domain.com/struts2/queryName?param1=foo,param2=xml http://www.domain.com/struts2/queryName?param1=foo,param2=json http://www.domain.com/struts2/queryName?param1=foo,param2=html http://www.domain.com/struts2/queryName?param1=foo,param2=rss Include getter and setter for param2 in your action and set your result strings as appropriate to get the results you want so, for example after executing your query, you could include in your action method; return getParam2() which would return xml,json,html or rss and then in your struts.xml you'd specify result name=xmlYourResultTypeForXML/result result name=jsonYourResultTypeForJSON/result etc etc Does this help. Regards Roger - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]