Struts2 - Repopulating a page with updated data after form submit - struts2

I have a jsp page called diary.jsp where the action class associated with it is called DiaryAction and in this class I am using the populate method to prepopulate fields. This is working as expected when I first access the page using PopulateDiary.action in the URL i.e.
<li>Profile</li>
However as part of this page I have a form where a user can add a new diary entry. On the submit of this form I call the following action:
<action name="addDiaryEntry" method="addDiaryEntry"
class="spirit.of.community.action.classes.DiaryAction">
<result name="success">/diary.jsp</result>
</action>
This is calling the correct code to add the new diary entry. However, on the success of this action method I want to display the same page i.e diary.jsp with the populated data again (to include the updated entry). I have placed this line inside my diary.jsp page in the hope that this would accomplish my goal:
<s:action name="populateDiary"></s:action>
Now when the diary.jsp loads I can see the populate method being called. But the page isn't populated with any of my data. If its of any importance, the URL when the form is submitted is - "/addDiaryEntry.action". Can anyone advise how I could call my addEvent method to add a new entry, and then reload the page with the updated data? Any advice is appreciated! Thanks in advance!

You have few ways to accomplish this and here they are
When you call addDiaryEntry action to add a diary instance you can create a list of all entries and can make use of iterator to show the data as per your way.
Other way is to <s:action name="populateDiary" executeResult="true">.
execute result tell whether the result of this action (probably a view) should be executed/rendered.
For more details refer to the Execute action

Related

Passing a selected row from a GSP's table to a Controller in Grails 3.0

I have one controller that sends a list of users data (UserID, FirstName, LastName, email) to a GSP.
The GSP displays the data into a table of 5 columns where the 5th column is a submit button so one particular row in the table can be selected. That selected row of data is then passed back to the same controller for further processing.
I am new to Grails 3 and have not found a similar use case.
It seems to me that Grails does not have an equivalent of the JSF dataTable. I need pointers on how to do this without using JavaScript or JQuery.
Thanks in advance.
You can use grails http://grails.github.io/grails-doc/2.2.1/ref/Tags/remoteFunction.html which can be assigned to Dom event to call the remote method. For example If you have a regular button you can add the following code:
onclick="<g:remoteFunction action='modify' id='${user.id}'/>"
Another option to use would be http://grails.github.io/grails-doc/2.3.7/ref/Tags/submitToRemote.html which creates a button that submits the containing form as a remote Ajax call. Here url parameter can be used as per documentation:
url - The url to submit to, either a map contraining keys for the action, controller and id or a string value
Hope this helps.

Rails 4 Single form-two actions -preview and assign

I have a form which has two buttons
One will assign the form, i mean create an entry in corresponding table after validation and redirect to another page
Second will give a preview of the values submitted on the form.For the preview screen we need some calculations so that an action to be called from controller and respective page will be loaded.
This page should seen on the same form if i clicked preview before assigning the form
How i would address this is to change your new action so that it takes the same params as the create action, and makes the object but doesn't save them. This means that if you call the new action with the params it will reload the form with the built but not saved objects' data displayed in it.
The two buttons will need to change the form's action attribute so that it submits to the create or the new action as appropriate.
This is a general answer, as your question is quite general!

asp.mvc how do I submit mulitple forms?

I have an asp.mvc app the presents to the user different forms when they click on the next button - like a wizard. I do it this way so I can use JQuery to validate each form as the user progresses through them. i.e.
...
// use jquery validator to funk up the form validation
// user clicks the Next button ...
switch(currPageIndex) {
case 0:
if($('#form1'.valid()) {
$('#form1').hide();
$('#form2').show();
}
break;
}
...
However I can't use a single submit button to post the all the forms formcollection data back to my controller - if I do I only get back the one form that the submit button was in and not all of them.
Is there some magic icantation I can type in to get all the forms data sent back to the controller?
Presumably I can cruft up the data myself and send it back to my controller as a jason string, but I'm not sure if this is the best way.
Many thanks.
Wizard-like forms basically rely on a model which is kept on the server-side (in session or a database table) and kept updated.
For example, each post from the client you get back the model from session or database and then call UpdateModel() using the FormCollection which updates the model and the you can check if it is valid.
You can only submit one form at a time, however there are multiple ways around this in your scenario. When you change to the second form you could populate some hidden fields using javascript that would contain the information from the previous form. Then it would all be in the second form and you wouldn't have a problem getting the information.
You could also do it via ajax/json, but then you would probably want to do it with both of the forms data anyway.
In the end I just json'd up the form data and ajax'd it over to the controller, it works very well, nice and and clean to.
many thanks for the replies.
You can do that using $('#form1').submit() function, I don't think there is another way.

What is the best practices to track action id in controller for saved form?

Wasnt sure how to word the question, but this is the scenario:
the view is a data entry form eg http://127.0.0.1/User/AddEdit/
so edit the user I have an ID: http://127.0.0.1/User/AddEdit/7838fd9c-425c-4c98-b798-771bba10d9c1
This ID gets the data to populate the form values in a ViewModel, which populates the form
I am using jquery/ajax to save the form, which returns a Json result, indicating ok/error etc
In the View, I get the ID and use this in a hidden field which is set via jquery when the page loads and when the form is saved via ajax.
This seems a bit clunky, how do others do this?
in my opinion best solution is to create a partial view with all the fields and use it on add and edit view which are separate actions in controller. after you create user you can redirect to action edit. if you must / like use ajax you can reload div with form (change from user/add to user/edit/1). i might be wrong but i never see a code or example with one action in controller for add and edit.

View Master Page and PostBack

I have a dropdown list on my master page that needs to postback after being changed. After the postback, whatever page initiated the postback needs to be re-displayed.
My question is where do I handle this? Obviously I do not want to have to modify every Action in my project... My guess is to maybe postback to some other fixed action and have that action redirect back to the page that is the referrer. Am I correct? Any thoughts?
Thanks.
Jason
In Site.Master, I ended up wrapping the dropdown within its own form that posted back to a dedicated controller/action.
<% Using Html.BeginForm("ChangeRole", "Home")%>
<div id="roleSelector">Change Role: <%=Html.DropDownList("Role", DirectCast(ViewData.Item("Roles"), SelectList), New With {.onchange = "this.form.submit();"})%></div>
<% End Using%>
In the controller I used the following code to change the mode and then redirected back to the referring URL.
<AcceptVerbs(HttpVerbs.Post)> _
Public Function ChangeRole() As ActionResult
Me.CurrentUser.SetRole(DirectCast([Enum].Parse(GetType(Models.ApplicationRoles), Me.Request.Item("Role")), Models.ApplicationRoles))
Return Redirect(Request.UrlReferrer.ToString())
End Function
I am unsure if this is the recommended way but I have been unable to think of another solution.
When you post back from the dropdown list change what are you doing? Can you maybe handle this in a jQuery call thus eliminating the need to re-display the page at all?
Calls to Action Methods can be asynchronous as griegs says, as such, you can post whatever information you need from the radio buttons to an action method without needing to reload the page.
If you need to update a part of the page, you can replace it with the contents of a rendered action method. If you use the jQuery ajax methods, you can post specific information to your methods.
For example, something like this:
$(document).ready(function()
{
$("#myRadioButton").change(function()
{
//Post to your action method, with the value of the radio button, function to call on success
$.post('yourActionMethodUrl', $(this).val(), function()
{
//Update some part of the page
});
});
});
This is based on memory, so you may need to check the syntax.

Resources