I'd like to know if it is possible to get html content send to browser in interceptor ?
My aim is to get one div content and convert it in pdf.
Thanks
I am not sure about your question,and moreover if they are with respect to some specific actions i do not favor the approach to handle such use-case in interceptors,since in general interceptors are more towards cross-cutting concern and they are applied to whole stack you have configured rather than for some particular action.
Additionally the data will not be thread safe if its specific to action, being said that you can still have access to the parameters send to your action class from UI
Map parameters = ActionContext.getContext().getParameters();
The request parameters are available on the ActionContext instance, which is made available via ThreadLocal.
You can create your own interceptor and can get access to the parameters as follow
actionInvocation.getInvocationContext().getParameters();
above line will give you all request parameters from jsp page on which you have called any
action.
Hope that will help you.
Related
If someone goes to our website with a query string key of ref (for example mysite.com/AboutUs?ref=Test), is it possible to maintain that query string on all future links/form posts?
So for example, I may have a link on our website to go to mysite.com/Products. I would want this to be translated into mysite.com/Products?ref=Test.
Ideally I want to know if this is possible to do by inspecting the previous URL and adding it to the current URL when the request for the page is made, maybe by intercepting the route and appending the key (if it doesn't exist already).
The project is an MVC 4 application.
You could actually pass it along by adding it to every single URL, but that would require manually adding to each use of Html.ActionLink, etc. (or I suppose you could create a custom HTML Helper to do that for you, but then every developer who works on your project would need to remember to always use the custom helper), as well as all redirects and such in your controller actions. Long and short, this would be very time consuming and very fragile.
Your best bet is to simply intercept the initial request with the querystring parameter and then set a session var. Then, you can simply check for the presence of the session var instead of the querystring parameter.
To handle all this logic, your best bet is to create a global action filter. There's pretty extensive documentation on Filters at MSDN. Once you create your filter, you just have to register it in FilterConfig.cs (in App_Start) to make it global.
Set the URL parameter in a cookie, and later in your code do whatever you want to do based on presence of that value in either the cookie or URL.
if(parameter set in cookie OR URL)
// do stuff
if(parameter set in URL)
// set the cookie so that future actions are also tagged with that parameter
Alternatively if you want such tagging to happen only for the session, set a session variable.
To do it in the way you suggested - You could rewrite all links on your page based on this tag, but that would be a roundabout, and costly, way of doing this. Most languages allow a hook that allows your code to process the HTML output before it is sent out to browser; just run a appropriate regex search and replace to get what you want. e.g. PHP has ob buffering and associated functions. You could use the equivalent for .Net.
I am in the process of writing a web app that includes a reporting form. This form contains a number of radio buttons that allow the user to specify the return data.
There are about 6 different return data 'formats', and each of those has two variations - html data or JSON data for rendering to a chart.
I have begun coding it up and already my form post action method feels wrong.
I basically have a check on the requested data format and return it as needed. Each return type requires its own partial view / json object so there is little room for reusing code.
It feels like each one should have its own action method. Having the form post to different locations based on a radio button choice also feels wrong though.
Switching on report type and then redirecting to the appropriate action in the controller also feels like its not quite right.
Am I approaching this in the wrong way? As it currently stands my controller action contains a lot of code and a lot of logic...
Hope my query makes sense.
Thanks
I don't think there is anything wrong with your approach. To maximize reuse you could:
include reusable templates inside your views
make sure the business/data layer code is the same everywhere (where possible)
I suppose the views you need to return actually are different for each combination of options so whatever approach you take, you are stuck with that.
I wouldn't opt for the client-side approach. You then have code on both the server and the client that has to be updated whenever you change anything. I would keep the code that receives a set of options and determines what to do with them in one place.
I know what you mean about it feeling like each format should be a separate action, but maybe a hybrid approach would make it feel better.
If you set the value of each radiobutton to the name of the action it relates to, you then, in your main POST action, have a parameter that you can use to call the appropriate action in one line of code. You don't have to fudge anything in Javascript, it's easily extensible, and you get your separate actions.
If I understand your problem right you have a lot of switch code in action.
I think you can use Factory pattern. You can create factory that will accept switch parameter as parameter and will return ActionResult instance.
I'm trying to find the ideal exception handling strategy for my MVC project. I have done the following and am looking for some feedback.
Problem:
I have disparate result types (Pages, Partial Pages, JSON, Files, etc). Controller.OnException() doesn't have an easy way to identify what kind of result the client is expecting. Without anything special I am serving an HTML page when when they want JSON and so forth, which leads to display issues.
Solution:
I have an abstract BaseController that has utility functions like HandleJsonException(), HandlePartialPageException(), HandlePageException(), etc. These functions will:
a) Hand off to Enterprise Library for logging and notifications.
b) Set a result view in a format the client expects.
c) Set an appropriate Http Status Code for the error.
I separate my actions into different controllers based on result type. For example, Instead of AbcController I have AbcPageController and AbcJsonController. The OnException for this controller calls one of the base class utility handlers.
JavaScript (for JSON and Partial Page views) looks at the status code to direct behavior in some cases.
My Concern is that the display logic is dictating the design of the controllers and therefore influencing the routing (not the URL's but the routes obviously). Also, this buggers up prior inheritance strategies with regards to shared OnAuthenticate on base controllers.
Anyways... Looking for a review. And possibly links to other people's solutions to this problem.
Cheers
Controller.OnException() doesn't have an easy way to identify what kind of result the client is expecting
You could use the Accept request header which standards respectful clients send to indicate what content types they support and expect in return. For example if you are using jquery.getJSON() method it will send the following header: Accept: application/json, text/javascript, */*. As you can see application/json is the preferred format here and you could use this information in your controller.
I ended up abandoning this approach.
Handled errors will return an appropriate result directly from the controller action.
Unhandled errors dealt with in Controller.OnException will:
a) set the HttpStatusCode to 500.
b) render a full HTML page with our applications error page
c) set the handled property to true so custom error pages don't kick in.
If the caller is expecting something other than an HTML page (Ajax JSON, Partial Page, XML, etc) will examine the 500 code and ignore the HTML content.
I have an action that populates the result from the DB. Right now, I see a way of doing it and that is to make the Action ServletRequestAware, set the populated list as a request attribute and show it in jsp.
Since a lot of improvements have been introduced into struts2, is there any other way of doing that? The first thing that comes to my mind is to use displayTag and change the return type of Action to a List, but that does not seem to work.
Thanks for any replies in advance.
You question is unclear, you should read some book about Struts2 to get the general idea.
No need to make the Action ServletRequestAware. The mapping from http parameters to actions fields is automatically done via the Param interceptor (already set in the default configuration). And one of the points of Struts2 is decoupling the action from the http protocol, you should not (typically) do anything related to http in your action.
Tipically, in your action execute() method (or whatever) you'll get the data to display from the DB and set it as one property of your action, so that is accesable from some getter.
Then, in your view page (JSP or whatever) you'll display it. You can use displayTag, but first you'll prefer to display it "manually", to understand what's involved. See for example here http://www.roseindia.net/struts/struts2/struts2controltags/iterator-tag.shtml
For manually displaying a table, also see this example http://www.vaannila.com/struts-2/struts-2-example/struts-2-crud-example-1.html , search for the <table class="userTable> tag.
I have been trying to find some step by step guidance on how to take a user's inputs (e.g.) name address, etc) via the view, to write that to xml, and then to submit that set of data to a third party interface (e.g UK Companies house xml gateway)
I have some aspects in hand (i.e. GET side of view to collect the data), but can't seem to get my head around using the controller and POST to submit the actual thing!
If anyone can steer me to a "For dumboids" tutorial, I'd highly appreciate it
Thanks
Paul
Side stepping the issue of whether you should be posting from your controller, you can use code like the following to post xml to a url. In the example below "xml" is your xml string and "url" is the url you want to post to. (Note: WebClient is built-into the .NET framework)
// create a client object
using(System.Net.WebClient client = new System.Net.WebClient()) {
// performs an HTTP POST
client.UploadString(url, xml);
}
I would question whether the controller should be posting directly to the XML gateway - this sounds like the job of a domain/business object sitting behind the controller, ideally referenced by an interface.
This was you can test your controller without requiring it to hit the XML gateway, and simulate the success/failure conditions.
If you can get the data already, I'd consider:
IGatewayPoster poster = ...
poster.Submit(dataSentFromView);
In the GatewayPoster class do the actual work of communicating with the XML gateway.