Struts2 setting object from jsp - struts2

I have queried DB and set that object in Action class and set that back to jsp to display results. Now I want the same object into another or same action class. How do I do that.

I haev also faced same issue and implemeted in the following manner. find if it is useful.
You have use conversion technique to speficy what type of Object are you going to store in the list. Then i have set it in hidden feild in jsp. then i am able to retrive it in action class.
http://struts.apache.org/2.0.14/docs/type-conversion.html

Related

How can we pass a list from jsp to Actionclass in struts2?

How can we pass a list from JSP to the action in Struts 2?
The list is a list of strings set from the same action when the JSP page gets loaded (there is a hidden field in the JSP which is being set).
All that I need is when again the form is submitted and the control goes to the action, I need that list again.
Make an array of strings and submit it using json or make a # seperated string of all strings and associate it with some hidden variable and submit it to action class. then in action class you needs to parse it back to original form.
you can use the hidden tag name attribute to refer to the list name that is being used in the class. Now when the form is submitted it will automatically map to the list in your action.
You need to have list as your instance variable in the action class
If you are setting a single hidden field with the contents of a list then you'll need to parse it back into a list again. Whether or not this is a good idea depends on the contents of the list, and how good you are at parsing.
Another option is to use Struts 2's default list-building mechanism and use multiple hidden fields and OGNL's array notation, e.g., name="foo[0]", name="foo[1]", and so on.
I'd start, however, by examining the need to reconstruct the list from the JSP like this.
If you're just serializing/deserializing the same list, why bother? Either keep it in session, or reconstruct it on the Java side. If it's backed by a DB then your caching mechanism should reduce any overhead.

MVC Razor How to get the model in the Controller on HttpPost when the model is dynamic

I'm working a feature in the application where model will be dynamic in the sense that any settings data could be displayed and the view will get the model based on what tab they clicked on. I use Hidden field to store what the settings name was because they are same as model name. for ex., if tab1-> Settings1 then Model is Settings1[already exists in the Model].So I used # model dynamic in View and used #Html.EditotForModel() to draw the required UI based off the model. My problem is when I do HttpPost on Edit currently I'm using FormCollection to read the data on that page when I declare the model name in the param it will get it for me but I don't know which model is coming back other than by the Hidden variable and I need it because the Model validation is broken because of this issue. Any help or feedback is appreciated? I can give more details if required? Has anybody crossed this issue before??
Dynamics can be a good thing and a bad thing. Using them on models that have a common interface in a controlled manor is best.
There are different options that you can look at:
1)
Have you tried making the action method accept a dynamic type? That might be the easiest way.
You might have to set up a casting helper to cast the object to the correct type based on the hidden field.
2)
I have a similar idea in some code, but I created a viewmetamodel class that contained all my types as nullable properties. My action method accepts this viewmetamodel type and validates the properties that are not null.
In line with this, if your data is not too large, then you could load all the settings tabs and use Jquery apply the tab with on click.
3)
You could also create #sections or use EditorFor(c=>c.settings) for each tab. That way each tab will load a type safe object. You would need to create controllers for each.
I would say pick the easiest method for you. I hope that this at least gives you some ideas.

Getting the Container in a ASP.NET MVC EditorTemplate

I just created a custom template for all elements with an FunctionPickerAttribute (custom attribute that I wrote myself). Now, what the FunctionPickerAttribute does is simply to store the name of a method that returns a IEnumerable<KeyValuePair<String, String>>.
The template I created finds that attribute, finds the method (using reflection) and is then supposed to call that method upon the object. However, the problem is that FunctionPickerAttribute is assigned onto a property of type string, so that when I enter the FunctionPicker-template I have no idea of how to get a reference to my object.
I can find the type of the Container (using ViewData.ModelMetadata.ContainerType), but I need to get a reference to the Container in some way. Is this possible? And if it is, how do I go about making it?
Not the way your doing it.
The only way to get the container is pass the entire model to your template.
If you post more of your code I could help better. I do this type of thing often.

What is a preferred way of displaying result as a table?

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.

Can you change a custom attribute from a controller in ASP.NET MVC?

I am looking to have an attribute "show" on certain properties in my class and give the user the option to show other properties in the view.
Can I change a custom attribute from the front end?
Are you talking about the Filter attributes? If you are, you cannot change properties as they are statically defined, unless the code within the attribute happens to reference a static class or within the context at execution time, and then you pretty much can access within the attribute anything from that static or the context...
If I'm on the wrong track, please let me know.
HTH.
I am not sure attributes are the best approach for dynamically attaching metadata. Normally attributes are attached at code generation time.

Resources