struts2 submit form and show result in a new window - struts2

struts.xml
<action name="run" class="editTrackerAction" method="run">
<result name="input">/jsp/editTracker.jsp</result>
</action>
editTracker.jsp
<s:form method="post" name="saveTracker" id="saveTracker">
<input type="submit" name="executeEntityButtonName"
id="executeEntityOnTableSubmit" value="Run Entity" onclick="executeEntityOnTable();">
*.js has function
function executeEntityOnTable() {
document.saveTracker.action="run";
document.saveTracker.onsubmit=window.open('jsp/thankyou.jsp', 'executeEntityOnTable', 'width=450,height=300,status=yes,resizable=yes,scrollbars=yes');
document.saveTracker.submit();
}
In Page editTracker.jsp, I need to click submit button to show result variable defined in action in Page jsp/thankyou.jsp in a new window, editTracker.jsp page still stay there after submit form as struts.xml configured, but I can't get result in jsp/thankyou.jsp because it is pop up before saveTracker form submit.

You would likely be better off submitting the form via ajax then opening the new window.
If you submit a form with a normal request, the page will update; that's just the way it works. If you mean the form submission reloads the same page again, that's fine, but you still need to interact with the server before the new window opens.

try to use your Form something like below and use the following javascript function as well.
<form name="myForm" action="myaction.do" method="post"
onsubmit="return createTarget(this.target)" target="formtarget">
javascript function....
function createTarget(t){
var left = (screen.width/2)-(700/2);
var top = (screen.height/2)-(550/2);
window.open("", t,"status = 1,height = 550,width = 700,resizable = 1,left="+left+",top="+top);
return true;
}
I've done some formatting for the new window as well.
Regards.

Related

Ask user to save changes before leaving a web page - Angular JS

I am creating an ASP.net MVC application with C#.
In the main .cshtml page, I am showing a list of user names on the left hand side of the page.
When the user clicks on any Username from the left, it display the "Personal Details" form for that User in a "partial view" on the Right Hand Side of the page.
Current Functionality
There is a "Save" button on the "Personal Details" Partial view form.
User can make changes in the Personal Details and hit Save to save the changes made in the partial view.
Desired Functionality
User makes changes in the Personal Details of any record.
Now, the user forgot to hit Save and clicked on the Other Username from the left hand side.
It should "AUTOMATICALLY SAVE" the changes made in the partial view.
Is it possible to execute the Save button trigger event of the partial view from main view ?
When you want to save the form automatically you can call angular Submit on Other Username click , This snippet shows example of how.
The onUserNameClick() method invokes same angular submit.
function Ctrl($scope) {
$scope.list = [];
$scope.text = 'hello';
$scope.submit = function() {
if ($scope.text) {
$scope.list.push($scope.text);
$scope.text = '';
}
};
}
function onUserNameClick() {
angular.element('#submit').trigger('click');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<div ng-app>
<div ng-controller="Ctrl">
<form ng-submit="submit()">Enter form input values to test
<input type="text" ng-model="text" name="text" />
<input type="submit" id="submit" value="Regular Form Submit" />
<pre>Form values={{list}}</pre>
</form>
</div>
</div>
<button onClick="onUserNameClick()">Other username click</button>

<g:checkBox> click calling a controller function

When the user clicks on the checkbox, I want to call a controller function and pass the current status of checkbox, whether it's checked or not.
I know how to do this using jQuery but I want to do this from the checkBox itself.
<g:checkBox id="customer_checkbox" name="customer_checkbox" value="${checked}" />
Controller function to be called:
class updateController {
def updateIndex () {
// do something
}
}
U need to use remoteFunction from grails taglib. This tag generate for u ajax function:
<select from="[1,2,3,4,5]" onchange="${remoteFunction(action: 'updateIndex', controller:'update',options: '[asynchronous: true]'}" />
For more information go to docs
The checkbox would be a part of the form. Use javascript to invoke the action.
<g:form name="formName" controller="updateController" action="updateIndex">
<!-- Other form elements -->
<g:checkBox id="customer_checkbox" name="customer_checkbox" value="${checked}" onChange="document.getElementById('formName').submit();"/>
</g:form>

How to do validation in struts2 when calling action from jquery ajax?

i am calling struts2 action from jquery ajax on success it returns string and on error it should be dispatched to same page and should display errors. i have used following code check it..
$(document).ready(function(){
$('#getActionRs').click(function(){
alert("call action");
$.ajax({
type:"POST",
url: "returnToAjax",
data: "firstinput=" +$('#firstinput').val()+"&secondinput=" +$('#secondinput').val(),
success: function(msg){
alert("success:"+msg);
}
});
});
});
i have used above code to call my struts action. onclick of button this thing get called
i have specified my action element in config file as follows
<action name="returnToAjax" class="example.returnToAjax">
<result name="success" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
<result name="input" type="dispatcher">/Login.jsp</result>
</action>
when it returns success it return string correctly, i have done some validation of this action by validation xml but when it returns error it just shows me Login.jsp file code it does not dispatches it to Login.jsp
you are doing things fundamentally wrong.Ajax is something which means doing a backed process without refreshing page and letting user stay on the same page.
Your approach is something what not in the right scope of Ajax principal.So even when you have validation failure in you action class control is coming back to same handler in your Jquery code and since you have specified the Login.jsp as the view template the steam result is picking the whole jsp and returning back its content.
If you want to go with same approach, just return action name from the Action if validation failed and den redirect user to input page using JavaScript form submit function.
You can follow this procedure :
Form submit
Validation error (checked through validation.xml)
pass error.jsp as input result where error.jsp contains a word, suppose say ERRORPAGE
you'll get error.jsp via the ajax response.
check out the response content if it starts from ERRORPAGE, do a javascript redirect to login.jsp
Here's a code snippet of the example which does struts2 jquery grid validation through ajax.
afterSubmit:function(response,postdata){
return isError(response.responseText);
}
<script type="text/javascript">
function isError(text){
if(text.indexOf('ERRORPAGE')>=0){
return [false,text]; //do redirection here something like
//window.location="/login.action";
}
return [true,''];
}
</script>

JSF - Richfaces, process submitted form data and then confirm to continue or cancel

I want to show a confirmation dialog to continiue or cancel a save operation when a form is submitted. I have a form with a save button which is calling an action methode to persist data in the form.
When save button is clicked, a file will be readed on serverside before the form data is persisted. Data from the file will be joined into form data and then te form data will be persisted. I need some values from the form to define which file will be readed. There is no problem so far. When a FileNotFoundException throwed or the neccessary data from the file is not found, then i want to show a confirmation dialog to continiue or cancel save operation with caused message.
Does anybody have some examples or any ideas how to handle this? Do i need to use a4j? Thanks.
I am using Rifchfaces 3.3.3 and Seamframework 2.2.
At first i have to correct my question title. It is not going on "processing submitted form data" but a form data that will be submitted after some validation.
Now the solution.
For example I have following in my form:
some filelds
an a4j:commandButton to reRender the fields and perform doSomeStuff() action
an hidden h: or a4j:commandButton to submit the form.
1- User clicks on 'fake' submit button which is an a4j:commandButtton,
2- Ajax call updates fields in reRender attribute
3- After that,method doSomeStuff() is performed with rerendered field values
4- In the end Javascript will run to submit form or not.
Form:
<h:form id="myForm">
<h:inputText id="name" value="#{personHome.person.name}"/>
<h:inputText id="surname" value="#{personHome.person.surname}"/>
<a:commandButton value="Save" reRender="name, surname"
action="#{personHome.doSomeStuff()}"
oncomplete="return checkMessage('#{personHome.success}')"
id="a4jSave" />
<h:commandButton id="save" value="Save"
action="#{personHome.persist}"
style="visibility:hidden" />
</h:form>
JavaScript:
<script language="javascript">
function checkMessage(success) {
if(success =='false')
{
return confirm('Do you want to submit this form?') ? submitForm() : false;
} else {
submitForm ();
}
}
function submitForm() {
document.getElementById('myForm:save').click();
return false;
}
</script>
Yes you need to use a4j.
Try something like that (non tested, but follow the algorithm) :
<a4j:commandButton onclick="if(messageHasToBeDisplayed){Richfaces.showModalPanel('modalId');}else{doSomeStuff();}" />
...
<a4j:jsFunction name="doSomeStuff" action="#{controller.doSomeStuff}" reRender="..."/>
This shows you how to display a modal panel if necessary.
Without more code I can't help you more, but I think this should help you...

AJAX behaving differently for Submit button vs. this.form.submit?

I'm trying to auto-save a selection in a dropdown (ASP.NET, MVC, VB), but it's not behaving as expected. Here's the dummy action in the controller:
<AcceptVerbs(HttpVerbs.Post)> _
Function TestAction(ByVal id As Integer) As ActionResult
Return Content(id)
End Function
and the HTML:
<script type="text/javascript" src='<%= Url.Content("~/Scripts/MicrosoftAjax.debug.js") %>'></script>
<script type="text/javascript" src='<%= Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js") %>'></script>
<% Using Ajax.BeginForm("TestAction", New AjaxOptions With {.UpdateTargetId = "test"})%>
<%=Html.Hidden("id", 123)%>
<%=Html.DropDownList("actions", Nothing, New With {.onchange = "this.form.submit();"})%>
<input type="submit" value="Submit" />
<span id="test"></span>
<% End Using%>
The Submit button works as expected - the span is populated with "123". The dropdown on the other hand opens a new page with nothing but "123" on it. Why "this.form.submit()" not doing the same thing as the Submit button? Is there a different call I should make to emulate the Submit button?
this.form.submit does not run the form.onsubmit event. Pressing the submit button, on the other hand, does. That, combined with the HTML that Ajax.BeginForm generates, explains why the two behave differently. As for how to make your event do the same thing as pressing the submit button, look at the HTML in the linked article:
Sys.Mvc.AsyncForm.handleSubmit(
this,
new Sys.UI.DomEvent(event),
{
insertionMode: Sys.Mvc.InsertionMode.replace,
updateTargetId: 'test'
});
I know this is old, but there is a new (and better) way to do this.
Instead of doing using javascript, use jQuery. Just had this issue and it worked great.
this.form.submit() <---- Javascript
$("form").submit() <---- jQuery

Resources