What is an equivalent Struts2 code for below snippet? [duplicate] - struts2

I am using Struts2 framework in my application, I have one button on my JSP page. That is
<s:submit type="button" name="btnSave" />
Now, I want this button to behave as normal HTML button type should not submit the form and execute the Scripting
function on onclick event. That function submit the form using Ajax.
But what happens is Struts2 convert it to
<input type="submit" id="add_btnSave" name="btnSave" value="Save"/>
And my form is submitted.
1) If I use the HTML button tag it will mess the GUI. As theme of my form is Ajax.
This is a head tag with script
<head>
<s:head theme="ajax"/>
<script type="text/javascript">
$("btnSave").click(function(){
alert("aaa");
$.ajax({
url:
type:"POST",
dataType: "json",
error: function(XMLHttpRequest, textStatus, errorThrown){
alert('Error ' + textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
},
success: function(){
alert('SUCCESS');
}
});
});
</script>
</head>
My body tag is as followers :
<body>
<table border="1" width="80%" align="center">
<tr>
<td width="100%">
<s:tabbedPanel id="EmpDetail" useSelectedTabCookie="true">
<s:div id="one" label="Emp Reg." theme="ajax" tabindex="0" labelposition="top">
<center>
<s:form name="frmEmpReg" namespace="/" method="post">
EMPLOYEE REGISTRATIOM TAB<br>
<s:actionmessage />
<input type="hidden" name="empbean.id" value="<s:property value="empbean.id"/>"/>
<s:textfield label="Employee First Name" name="empbean.firstName"></s:textfield>
<s:textfield label="Employee Middle Name" name="empbean.middleName"></s:textfield>
<s:textfield label="Employee Last Name" name="empbean.lastName"></s:textfield>
<s:textfield label="Address" name="empbean.address"></s:textfield>
<s:textfield label="State" name="empbean.state"></s:textfield>
<s:textfield label="Employee Designation" name="empbean.designation"></s:textfield>
<s:submit name="btnSave" type="submit" value="Save" align="center"/>
</s:form>
</center>
</s:div>
<s:div>
..
..
Other Tabs
</s:div>
</s:tabbedPanel>
</td>
</tr>
</table>
</body>
any one have any idea of handling it with Struts2 then please help.
Your help will be really appreciated.

If you are using s:submit tag the rendered HTML output is either input or button tag but the type is submit except for the image type. You can use the code to execute javascript onclick event that doesn't submit the form.
<s:submit type="button" onclick="return false;"/>

Have you tried preventDefault()?
$("btnSave").click(function(e){
e.preventDefault();
//..rest of the code
});

Related

How to prevent submit buttons not to show the url in the address bar?

Folks,
I just noticed something interesting developing my first ASP.Net Mvc application and Its when you mouse over on a submit button in Mozilla the full Url is shown in the status bar! Is it a normal behavior?! how can i prevent that?
Try the following code:
<html>
<body>
<form id="myForm">
<input type="text">
<!-- <input type="submit" value="Send" > -->
<input type="button" value="Send" onclick="onClick(this)">
</form>
<script type="text/javascript">
function onClick (e)
{
document.forms["myForm"].submit();
}
</script>
</body>
</html>

Popup form post prevent site from refresh

I have a kendo grid with a button in it. When the button is pressed it opens up a popup with a file-select input and a button so that the user can save a image. Im using a normal form with a submit button to post my form to my controller. Everything works fine the right data arrives to the controller but then the page refreshes. I just want the popup to close and the grid and the whole page behind the popup to just stay as it is. Here is my controller:
[HttpPost]
public ActionResult SaveData(HttpPostedFileBase file, string id)
{
iResourceModel.SaveData(file, file.FileName);
return null;
}
and my html form:
<div id="details-container">
<form action="/Home/SaveData" method="post" enctype="multipart/form-data">
<span>Select file:</span><input type="file" name="file" id="file">
<input type="text" value=' + dataItem.ResourceId +' name="id" id="id" hidden/>
<button type="submit" id="" >Save file</button>
</form>
</div>
Page reloading after submitting a form is normal behavior. If you want to stop it, you can use jQuery and submit the form in the background.
You can find the jQuery form plugin here : http://plugins.jquery.com/form/
For your case :
<div id="details-container">
<form action="/Home/SaveData" method="post" enctype="multipart/form-data" id="myForm">
<span>Select file:</span><input type="file" name="file" id="file">
<input type="text" value=' + dataItem.ResourceId +' name="id" id="id" hidden/>
<button type="submit" id="" >Save file</button>
</form>
</div>
<script>
$(document).ready(function() {
$('#myForm').ajaxForm(function() {
alert("File uploaded!");
});
});
</script>

Input button in MVC asp.net

How to use Input button for #Html.ActionLink?
In my cshtml I have this code:
<div>
#Html.ActionLink("Back to menu", "HomeController")
</div>
Now I whant to use instead hyperlink button (Back to menu), input submit button, so this button:
<input class="btn" type="submit" value="Back to menu">
Thanks.
You can't - you can use css so the link will look like button, or you can utilize Url.Action and javascript instead. I.E.:
<input class="btn" type="submit" value="Back to menu" onclick="window.location='#Url.Action("Index", "Home")';return false" />
Ok, I solved it, Ondrej Svejdar thanks for advice, I use javascript and works fine:
<input class="someCss" type="button" value="Back to Edit Menu" onclick="Redirect()"/>
And under button I use this javascript:
<script type="text/javascript">
function Redirect() {
var url = "#Url.Action("Registration")";
location.href = url;
}
</script>
So result is:
<input type="button" value="Button" />
value="button" />

how to read selected radio button value in Grails?

I'm having a GSP page like below. The requirement is like a list of reports will be shown - the user has the option to select one report and can export the report to excel.
How to read the selected radio button and pass the selected value as "params" ?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="main" />
</head>
<body>
<div class="nav">
<span class="menuButton"><g:link class="create"
action="excelExport" params="['id':{ radId.value}]">Export To Excel</g:link>
</span>
</div>
<div class="body">
<div class="message">Select the report and click 'Excel Export'</div>
</div>
<g:form method="post">
<g:render template="displayUploadedReportsTemplate"
model="['uploadedReports':uploadedReports]" />
</g:form>
</body>
</html>
where displayUploadedReportsTemplate is:
<tbody>
<g:each in="${uploadedReports}" var="bbkRat">
<tr>
<td valign="top"><g:radio name="radId"
value="${fieldValue(bean:bbkRat,field:'id')}" /></td>
<td valign="top"><label> ${fieldValue(bean:bbkRat,field:'cmpName')}
</label></td>
<td valign="top"><label> ${fieldValue(bean:bbkRat,field:'reportCreationDate')}
</label></td>
<%--<td valign="top">
<label> ${fieldValue(bean:bbkRat,field:'cmpName')}
</label>
</td>
--%>
<tr>
</g:each>
</tbody>
How should the params value be below??
<g:link class="create"
action="excelExport" params="['id':{ radId.value}]">
i would recommand to use a radio button group. instead of using g:readio tag you can replace it with plain html input tag within you each tag, e.g.
<input type="radio" name="myGroup" value="1" checked="checked" />
<input type="radio" name="myGroup" value="2" />
<input type="radio" name="myGroup" value="3" />
you have already defined a form around your displayUploadedReportsTemplate. so you need to add a submit button to this form and a action where the params should be tramsitted, e.g.
<g:form method="post" action="test">
within test action you can print your params.myGroup and you will recieve to selected report.
<g:link is processed on server-side, so you can't use it on client side, you have to use javascript instead.
It would be like:
<a href="#" class="excelExport" onclick="doExport(); return false">
<script type="text/javascript">
function doExport() {
var id= $('input:radio[name=radId]:checked').val();
window.location = '${g.createLink(action:'excelReport')}?id=' + id;
}
</script>
ps I assume that you are using jQuery

JQuery Mobile - POST a form and get response

Greetings,
I have the following jquery mobile page:
<div data-role="content">
<center>
<img src="./images/logo320.png" />
</center>
<br><br>
<form method="POST" action="./ajax/login.php">
<label for="login_unameLabel">Username:</label><br>
<input type="text" name="login_uname" id="login_uname" /><br>
<label for="login_pwordLabel">Password:</label><br>
<input type="password" name="login_pword" id="login_pword" /><br>
<button id="login_submit" type="submit" data-theme="a">Submit</button>
</form>
</div>
./ajax/login.php returns either "OK" or "NOK". How can I capture this result in my page?
I keep getting the following error in Firebug:
k.data( [Break On This Error]
false)a.mobile.activePage=k;h(k);y&&D&...dd(k).removeClass("
out in reverse "+
It's as if jquery mobile is performing some operation on the result? I don't want this to happen. Do I have to return a valid jquery mobile HTML page from my PHP?
Any insight greatly appreciated.
Many thanks in advance,
Solution: use <input type="button" id="login_submit" data-theme="a" value="Submit" />
Now I can capture click events via:
<script>
$(document).ready(function() {
$("#login_submit").click(function() {
alert('clicked');
});
});
</script>
using data-ajax="false" in form.
then you can disable jquery mobile auto ajax call

Resources