working with multiple forms in same action class in Struts2 - struts2

How do we populate\retrieve information from\into multiple forms in same action class in Struts2 ? With ModelDriven approach, action is tied to one form. With ScopedModelDriven interceptor jsp becomes dirty as one has to write model.property to access form properties. Is there a better way of doing it?

Related

What´s the difference between a custom action filter and a custom action selector in ASP.NET MVC?

I would like to know the differences between a custom action filter and a custom action selector in ASP.NET MVC.
Say we want to restrict who can have access to an action method on a controller based on some rules. I could either create an action filter extending the ActionFilterAttribute class or extending the ActionMethodSelectionAttribute class, so that I could have something like:
[MyRestriction]
public ActionResult AnyAction(){}
Could anyone explain the differences between them so that I can make the right decision?
If you look at the documentation for ActionMethodSelectionAttribute, you will see at the very bottom of the page that there are a number of other classes that derive from this attribute.
These include:
Microsoft.Web.Mvc.AjaxOnlyAttribute
System.Web.Mvc.AcceptVerbsAttribute
System.Web.Mvc.HttpDeleteAttribute
System.Web.Mvc.HttpGetAttribute
System.Web.Mvc.HttpHeadAttribute
System.Web.Mvc.HttpOptionsAttribute
System.Web.Mvc.HttpPatchAttribute
System.Web.Mvc.HttpPostAttribute
System.Web.Mvc.HttpPutAttribute
System.Web.Mvc.NonActionAttribute
In other words, these are the attributes which control which Action Method is selected during routing when there are several different choices to choose from (ie there are 2 different Index methods, one decorated with [HttpGet] and one with [HttpPost]).
ActionFilterAttribute, on the other hand, is called only when an action is actually executing.
Think about it this way, The selection can run even if the action doesn't execute, the ActionFilter only runs if it does. The selection filter is only used to determine whether the action is a match condition, the action filter is used to do some action before, after, etc.. the action or response is executed.

What are filters in asp.net mvc

What are the filters in asp.net mvc, can any one explain clearly.
How to create a custom filters in asp.net mvc 4
[Authorize]
Public ActionResults Index()
{
return View()
};
In ASP.NET MVC, controllers define action methods that usually have a one-to-one relationship with possible user interactions, such as clicking a link or submitting a form. For example, when the user clicks a link, a request is routed to the designated controller, and the corresponding action method is called.
Sometimes you want to perform logic either before an action method is called or after an action method runs. To support this, ASP.NET MVC provides action filters. Action filters are custom attributes that provide a declarative means to add pre-action and post-action behavior to controller action methods.
Check Filters-and-Attributes-in-ASPNET-MVC
The filter attribute has the Order property which can be used to manage the orders. The order needs to be the order the business process to be followed. For example if HandleError attribute is given higher order than the Authorize attribute then even an unauthorized users will be getting application errors. It would be better saying "Please Login".

Sending a parameter to the controller in ASP MVC 2

I am writing an area for administering several subsites, almost all of the functionality will be the same across each site (add/edit/remove pages etc) and my repository on instantiation takes the SiteIdentity so all the data access methods are agnostic in relation to this. The problem I have at the moment is trying to make my action methods also agnostic.
The URL pattern I want to use is along the lines of:
"ExternalSite/{identity}/{controller}/{action}/{id}"
A naive approach is to have each action take the identity parameter, but this means having to pass this in to my repository on each action as well as include it in the ViewData for a couple of UI elements. I'd much rather have this something that happens once in the controller, such as in its constructor.
What is the best way to do this? Currently the best I can come up with is trying to find and cast identity from the RouteData dictionary but part of me feels like there should be a more elegant solution.
It sounds like you want to use OnActionExecuting or a Custom ModelBinder to do that logic each time you have a specific parameter name (also known as a RouteData dictionary key).
Creating a custom modelbinder in ASP.NET MVC
Creating an OnActionExecuting method in ASP.NET MVC, Doing Serverside tracking in ASP.NET MVC
You have access to your route values in Request.RequestContext.RouteData, so you can make base controller and public property SiteIdentity, in such case you can access it from all actions in all inherited controllers.

What is the proper way to handle forms in ASP.NET MVC?

Forms + ASP.NET MVC = Confusing for me:
What are the preferred ways to setup your controller action for form posts?
Do I need to specify an attribute that has [AcceptVerbs(HttpVerbs.Post)]?
Should the controller action take a "FormCollection" or should I use ModelBinders?
If I should use ModelBinders, how?
How do I setup the form in the view?
Should I use the Html helpers like Html.BeginForm/Html.EndForm or simply specify the form tag myself?
How do you specify the controller and action to be used in a form (with either the Html helpers or with a manual form tag)?
Can somebody please show me both a simple view with a form ~and~ its corresponding controller action?
I'm trying to write a form with a single textbox that I can submit to the Home/Create action and pass the string from the textbox to it.
ScottGu's handling form edit and post scenarios is exactly what you're looking for. There's also form posting scenarios and even though it was written for preview 5, it's still mostly valid.
The template's AccountController.LogOn and .Register methods along with the corresponding views should give you the simple introduction you need.

Validating posted form data in the ASP.NET MVC framework

I've been playing around with the ASP.NET MVC Framework and the one thing that's really confusing me is how I'm meant to do server side validation of posted form data. I presume I don't post back to the same URL, but if I don't, how do I redisplay the form with the entered data and error messages? Also, where should the validation logic go? In the model or the controller? This seems to be one of the few areas where web forms are much stronger (I miss the validation controls).
Here's an overview of the flow in MVC:
/new - render your "New" view containing a form for the user to fill out
User fills out form and it is posted to /create
The post is routed to the Create action on your controller
In your action method, update the model with the data that was posted.
Your Model should validate itself.
Your Controller should read if the model is valid.
If the Model is valid, save it to your db. Redirect to /show to render the show View for your object.
If the Model is invalid, save the form values and error messages in the TempData, and redirect to the New action again. Fill your form fields with the data from TempData and show the error message(s).
The validation frameworks will help you along in this process. Also, I think the ASP.NET MVC team is planning a validation framework for the next preview.
You might want to take a look at ScottGu's latest post for ASP.Net prev 5. It walks through a validation sample that is very interesting:
http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx
As far as I can tell everyone is still trying to figure out the "standard" way of doing it. That said definitely check out Phil Haack and Scott Guthrie's latest posts on MVC and you'll find some interesting info on how they did. When I was just playing around with it for myself I created a ModelBinder for the LinqToSql data class that I had generated. You can check out this post to find out how to put together a basic ModelBinder:
ASP.Net MVC Model Binder
The in your action if you had created a "Product" ModelBinder you would just declare the action like so:
public ActionResult New(Product prod)
And the model binder will take care of assigning posted data to the objects properties as long as you've built it right anyway.
After that within your GetValue() method you can implement whatever validation you want, whether using exception's, regex's, or whatever you can make a call like:
(ModelStateDictionary_name).AddModelError("form_element_id", "entered_value", "error_message");
Then you can just throw a <%= Html.ValidationSummary() %> in your view to display all your errors.
For client-side validation I just used jQuery. After you get a basic sample set up though you can start doing some interesting things combining all that with Partial Views and Ajax calls.
Have you taken a look at this?
http://www.codeplex.com/MvcValidatorToolkit
Quoted from the page
The Validator Toolkit provides a set
of validators for the new ASP.NET MVC
framework to validate HTML forms on
the client and server-side using
validation sets.
I'm afraid that someone more MVC-savvy than me would have to speak to where in the architecture you should put things.
I'm just learning the MVC framework too so I'm not sure how off this is, but from what I understand you would have a form on a View such as Edit.aspx. This form would then post to the controller to another action method such as Update() passing in the contents of the form that you set in Edit.aspx as parameters.
Update(int id, string name, string foo)
You could do the validation within that method. If all is ok,
return View("Item", yourObject)
There is Castle.Components.Validator module in Castle project. It's very agile and powerfull. It generates validation rules based on model attributes (or any other source) and even able to generate JS validation using jQuery, Prototype Validation, fValidate and other.
Of course it's wise to abstract validator away behind IValidationEngine interface.

Resources