Struts2 submit button with id in iterated list - struts2

How do I use a struts submit button to submit the specific id of an object in an iterated list?
<s:form action="actionDeleteBooking">
<s:iterator var = "bookingList" value="bookings">
<s:submit label="delete booking" value = "cancel booking" theme = "simple" id="bookingid" />
</s:iterator>
</s:form>
I can do if for a radio button:
<s:form action="actionConfirmBooking">
<s:iterator var = "pathList" value="results">
<s:iterator var = "flightList" value="pathList">
<s:radio name="flightSelected" list="flightList" listKey = "flightid" id = "flightid" value = "flightid"/>
</s:iterator>
</s:iterator>
<s:submit label="Submit" />
</s:form>

if what you are trying to achieve is to have a submit button for each "row", submitting one value, then use multiple forms and an hidden field:
<s:iterator var = "bookingList" value="bookings">
<s:form action="actionDeleteBooking">
<s:hidden name="selectedItem" value="%{bookingid}" />
<s:submit label="delete booking" value="cancel booking" theme="simple" />
</s:form>
</s:iterator>
Otherwise use an <s:a />, or set a field with JavaScript, or describe better what you want.

You can use checkbox tag for the items you want to delete. You should be able to do this because you have already used similar form with s:radio.
<s:form action="actionDeleteBooking">
<s:iterator var = "bookingList" value="bookings">
<s:checkbox name="bookingids" label="bookingid" />
</s:iterator>
<s:submit label="delete bookings" value = "cancel booking" theme = "simple"/>
</s:form>

Related

How can I do: required="false" in grails g:select?

required="false" does not work in "g:select" tag
Example:
<g:select from="${}" name="select" required="false" />
and
<g:select from="${}" name="select" required="true" />
produces a html tag required (in html5)
How can I make the "g:select" produces the required or not, dynamically?
Just remove required, for eg:
<g:select id="select" from="${}" name="select"/>
You can use jquery to change the g:select to be required or not required. For example, lets say you have another
<g:select id="yesNo" from="[yes, no]">
In the gsp, use javascript:
$( "#yesNo" ).change(function() {
if($(this)[0].value == "yes") {
$( "#select" ).attr('required', 'required')
}
else {
$( "#select" ).removeProp( "required" )
}
});
Another approach is if you pass a variable required to gsp, you can use <g:if>:
In controller:
[required: "true"] //If dont want required, simply don't return required at all
In gsp:
<g:if test="${required}">
<g:select from="${}" name="select" required/>
</g:if>
<g:else>
<g:select from="${}" name="select"/>
</g:else>
<g:select from="${}" name="select" required="${false/true}" />
should work !

Unable to save dynamic added row

I am adding row dynamicall on click of commandButton.
in which I have two textfield and one dropdown and last column is save.
I am able to save old data which are already in DB,
But unable to save the newly added row. actually control is not reaching to bean when I click on save commandLink.
<div>
<div class="float_right">
<p:commandButton styleClass="add_uom"
value="#{msg['uom.add.new.uom']}" process="#this"
actionListener="#{uomBean.addRow}" immediate="true"
update=":uomFormId:languageDetail">
</p:commandButton>
</div>
<div class="clear"> </div>
<div>
<h:panelGroup id="languageDetail">
<table width="100%" cellspacing="1" cellpadding="1"
border="0" class="role_detail_section">
<thead>
<tr>
<ui:repeat var="language" value="#{uomBean.languageList}">
<th>#{language.languageCode}</th>
</ui:repeat>
<th>#{msg['uom.measuremnet.type.heading']}</th>
<th>#{msg['uom.save']}</th>
<th>#{msg['uom.activate.deactivate.heading']}</th>
</tr>
</thead>
<tbody>
<ui:repeat var="uom" value="#{uomBean.uomDTOList}">
<tr>
<ui:repeat var="uomCountry" value="#{uom.uomList}">
<td><h:inputText styleClass="values"
value="#{uomCountry.languageValue}" /></td>
</ui:repeat>
<td><h:selectOneMenu
value="#{uom.measureTypeId}"
required="true"
requiredMessage="#{msg['uom.measurement.error.msg']}">
<f:selectItem itemLabel="Select" itemValue="0" />
<f:selectItems value="#{uomBean.measureList}"
var="uomtDTO"
itemLabel="#{uomtDTO.measurmentTypeValue}"
itemValue="#{uomtDTO.measurmentTypeId}" />
</h:selectOneMenu></td>
<td>
<p:commandLink value="Saving.." action="#{uomBean.testUOM()}" ></p:commandLink>
</td>
</td>
</tr>
</ui:repeat>
</tbody>
</table>
</h:panelGroup>
<h:messages globalOnly="true" />
</div>
</div>
UOMDTO is
private Country country;
private Language language;
private String uomDisplayName;
private String uomDescription;
private List<UOMDetailDTO> uomList;
private Integer measureTypeId;
and UOMDetailDTO is
private String languageName;
private String languageCode;
private String languageValue;
Would be nice to see the source of uomBean.
I can only guess, but if uomBean is request scoped, the new row has been "lost" by the time you click save and thus cannot be saved. If uomBean is view or session scoped, then I would guess that you somehow reset/reload your uomDTOList, loose the new row and get the same problem as if it was request scoped.

Indexed properties are getting duplicated struts2 on form submission

I need to add/edit/delete the table of objects in struts2. When I submit the form, I am getting objects duplicated.
Let me know where I made a mistake.
This is my code:
<s:form action="addPromotionLineItems" id="promotionLineItemsAddFormId">
<display:table id="data"
name="lstPromotionLineItems" sort="external" uid="row" htmlId="rowid"
class="tborder" excludedParams="*" style="width:100%" export="false">
<display:column titleKey="table.title.material" style="width:100px">
<s:property value="%{#attr.row.materialCode}" />
<s:hidden
name="lstPromotionLineItems(%{#attr.row_rowNum-1}).promotionLineItemId"
value="%{#attr.row.promotionLineItemId}" />
<s:hidden
name="lstPromotionLineItems(%{#attr.row_rowNum-1}).materialCode"
value="%{#attr.row.materialCode}" />
<s:hidden
name="lstPromotionLineItems(%{#attr.row_rowNum-1}).material.description"
value="%{#attr.row.material.description}" />
<s:hidden
name="lstPromotionLineItems(%{#attr.row_rowNum-1}).programId"
value="%{#attr.row.programId}" />
</display:column>
<display:column property="material.description"
titleKey="table.title.materialdesc" />
</s:form>
function refreshTableRecords(url,formNameId,resultId)
$.ajax({
type:'POST',
url: url,
data:$('#'+formNameId).serialize(),
success : function(response) {
$('#'+resultId).html(response);
},
error:function(data)
{
alert(data);
}
});
}
I have this content in jQuery ui modal popup.
I think when mapping to collection data in struts the syntax is not:
name="lstPromotionLineItems(%{#attr.row_rowNum-1}).promotionLineItemId"
rather:
name="lstPromotionLineItems[%{#attr.row_rowNum-1}].promotionLineItemId"
There is a slight difference in the bracketing around the row_num attribute in the input name.

Radio button selected value is not getting reflected in command button ajax call

I have an issue with radio button. i have a 2 set of radio buttons and one command button in a field set.Based on the selection of radio button, respected value will be displayed through ajax call. But issue is always the first button is gettting considred in the command button action, thats why always first values is getting displayed,however the second radio button is selected.Here is my code
//test.xhtml
<h:panelGroup id="demandDashBoardPanelGroupID">
<fieldset>
<legend style="font-family:Arial;font-size: 12px;">Demand Dashboard</legend>
<table>
<tr>
<td>
<h:selectOneRadio immediate="true" id="groupbyid" value="#{demandManagedBean.selectedRadioButton}" styleClass="labelstyle">
<f:selectItem itemLabel="Group By Creation Date Time" itemValue="1" />
<f:selectItem itemLabel="Group By Location" itemValue="2" />
</h:selectOneRadio>
</td>
<td>
<h:commandButton id="fetchbuttonID" value="Fetch"
style="font-size:13px;width:60px;height:25px;" action="#{demandManagedBean.displayDemandRequestsTable}">
<f:setPropertyActionListener target="#{demandManagedBean.selectedRadioButtonParam}" value="#{demandManagedBean.selectedRadioButton}"/>
<f:ajax render=":parentformid:demandrequestpanelgrpid" immediate="true"/>
</h:commandButton>
</td>
</tr>
</table>
</fieldset>
</h:panelGroup>
<h:panelGroup id="demandrequestpanelgrpid" rendered="#{demandManagedBean.renderedFieldSet}">
<fieldset id="demandrequestfieldsetid">
<legend style="font-family:Arial;font-size: 12px;">Demand Requests</legend>
<h:outputText rendered="#{demandManagedBean.renderCreationdateDmdtable}" value="first table"/>
<h:outputText rendered="#{demandManagedBean.renderDmdLocationTable}" value="second table"/>
</fieldset>
</h:panelGroup>
//backing bean action
public void displayDemandRequestsTable(){
if(selectedRadioButtonParam!= null && selectedRadioButtonParam.endsWith("1")){
renderCreationdateDmdtable = true;
renderDmdLocationTable = false;
}else if(selectedRadioButtonParam!= null && selectedRadioButtonParam.endsWith("2")){
renderCreationdateDmdtable = false;
renderDmdLocationTable = true;
}
renderedFieldSet = true;
}
You don't need the f:setPropertyActionListener here to get the selected value into your backing bean. Just add an execute attribute to your f:ajax tag and remove the immediate=true:
<h:commandButton ...>
<f:ajax render=":parentformid:demandrequestpanelgrpid" execute="#form" />
</h:commandButton>
You could also just give the id of the component to be executed instead of the whole form:
<h:commandButton ...>
<f:ajax render=":parentformid:demandrequestpanelgrpid" execute="groupbyid" />
</h:commandButton>
Also make sure that the selectedRadioButton is properly initialized in your backing bean and that the bean is at least in #ViewScoped.
UPDATE:
If you remove the f:setPropertyActionListener then the selectedRadioButtonParam will not be set. Then you need to check selectedRadioButton inside your action method instead.

Struts2 pagination

My Struts2 application needs the pagination functionality applied to some records, but what I need to customize is the "Last 1 - 2 - 3 Next" appearance and also have the ability to use a combo box for the selection of how many records should be visible(10 - 20 - 30 - 40 -50).
I have tried two way to accomplish this goal:
1) use display tag library, but I'm not able to customize the appearance, because is auto-generated by the library, and I don't how implement the combo box for select how many records should be visible
2) create my own code for accomplish this functionality but is a job too long and not enough time due to the expiry.
My question is: exists some Struts2 library for realize this functionality? Or, is possible to customize the display tag library for the page scroll bar and the records combo box?
I can give insight from struts2 code base there is no functionality as described by you provided by struts2 in itself..
Regarding the Display tag i have not used them much but since they are the part of oprn source i am sure we can customize that one more thing regarding the customization of display tag ask question under display tag you will get better answer at at quick pace
I wrote a custom tld just for the purpose as below and then just use it like this:
Usage:
paginate.tag
1}">
<c:set var="showingPage"
value="${param.pageNumber == null ? 1 : param.pageNumber}" />
<c:url value="${postUrl}" var="previousUrl">
<c:param name="pageNumber" value="${showingPage - 1}" />
<c:param name="perPage" value="${perPage}" />
</c:url>
<c:url value="${postUrl}" var="nextUrl">
<c:param name="pageNumber" value="${showingPage +1}" />
<c:param name="perPage" value="${perPage}" />
</c:url>
<div class="pagination" />
<c:if test="${showingPage > 1}">
<< Previous
</c:if>
<fmt:formatNumber var="endCounter" pattern="0">
<%= totalCount % perPage > 0 ? Math.ceil(totalCount/perPage) + 1 : totalCount/perPage %>
</fmt:formatNumber>
<c:forEach begin="1" end="${endCounter}" var="index">
<c:url value="${postUrl}" var="url">
<c:param name="pageNumber" value="${index}" />
<c:param name="perPage" value="${perPage}" />
</c:url>
${index}
</c:forEach>
<c:if test="${endCounter != showingPage}">
Next >>
</c:if>
</div>
</c:if>
hi you can try struts2 jquery grid here is the code
<%# page contentType="text/html; charset=UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>
<%# taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<sj:div id="resultsDiv">
<body>
<div id="mainframe">
<s:form>
<div id="sucessMsg">
<s:property value="msg" />
</div>
<s:url id="editurl" action="editUser" />
<s:url id="deleteurl" action="deleteUser" />
<script type="text/javascript">
function editLinkFormatter(cellval, options, rowObject, icon, link_class, link_action) {
//alert(rowObject.username);
return "<a href='#' onClick='javascript:openEditDialog(""+cellval+"",""+rowObject.username+"")'><font class='hyperlink'><u>" + rowObject.username + "</u></font></a>";
}
function deleteLinkFormatter(cellval, options, rowObject, icon, link_class, link_action) {
return "<a href='deleteUser?userid="+cellval+"' onClick='javascript: return delete_user()'><font class='hyperlink'><u>Delete</u></font></a>";
//return "Delete";
}
colModal: [
{name: 'userid', formatter: function (cellvalue, options, rowObject) {
return editLinkFormatter(cellvalue, options, rowObject,
'ui-icon-pencil', 'edit-link-class', 'Edit');
}},
{name: 'userid', formatter: function (cellvalue, options, rowObject) {
return deleteLinkFormatter(cellvalue, options, rowObject, icon, link_class, link_action);
}}
];
function openEditDialog(userid,username) {
$("#resultsDiv").load("<s:property value="editurl"/>?userid="+userid+"&username="+username);
}
function delete_user() {
var agree=confirm("Are you sure you want to Delete?");
if (agree){
return true;
}
else{
return false;
}
// $("#edit_Users").dialog('open');
}
function unlockerFormatter(cellval, options, rowObject, icon, link_class, link_action) {
if(rowObject.loginStatus=='Locked'){
return "<a href='unlockuser?userid=" + rowObject.userid + "')'><font class='hyperlink'><u>Locked</u></font></a>";
}
else{
return "UnLocked";
}
/* return "<a href='deleteUser?userid="+cellval+"' onClick='javascript: return deleteUser("+cellval+")'><u>Delete</u></a>"; */
//return "Delete";
}
</script>
<sj:dialog id="edit_Users" title="Edit User" autoOpen="false"
modal="true" width="800" />
<sj:div id="draggable" draggable="true">
<s:url id="remoteurl" action="viewUserstemp" />
<sjg:grid id="gridtable" caption="Users" dataType="json"
href="%{remoteurl}" pager="true" gridModel="gridModel"
rowList="10,15,20,50,100" rowNum="10" rownumbers="true" viewrecords="true"
width="800" navigator="true" navigatorView="false" navigatorDelete="false" navigatorAdd="false" navigatorEdit="false" navigatorSearch="false">
<sjg:gridColumn name="userid" index="userid" title="User Id"
sortable="false" />
<sjg:gridColumn name="userid" index="username" title="User Name"
sortable="true" formatter="editLinkFormatter" />
<sjg:gridColumn name="emailid" index="emailid" title="Email Id"
sortable="true" />
<sjg:gridColumn name="userCreatedDate" index="userCreatedDate"
title="Created Date" sortable="true" />
<sjg:gridColumn name="userModifiedDate" index="userModifiedDate"
title="Modified Date" sortable="true" />
<sjg:gridColumn name="accstatus" index="accstatus"
title="Account Status" sortable="true" />
<sjg:gridColumn name="userid" index="username" title="Delete User"
sortable="true" formatter="deleteLinkFormatter" />
<sjg:gridColumn name="loginStatus" index="loginStatus" title="Unlock User"
sortable="true" formatter="unlockerFormatter" />
</sjg:grid>
<br></br>
<s:submit action="loadUserValues" cssClass="ui-button ui-widget ui-state-default ui-corner-all ui-state-hover" name="button"
id="button" value="New User" />
<br />
</sj:div>
</s:form>
<%--
<td height="25" align="left" valign="middle">
<s:url id="url" action="unlockuser">
<s:param name="userid">
<s:property value="userid" />
</s:param>
</s:url>
<s:set name="type" value="%{loginStatus}" />
<s:if test="%{#type=='Locked'}">
<s:a href="%{url}" title="Click here to Unlock" ><s:property value="loginStatus"/></s:a>
</s:if>
<s:else>
Unlocked
</s:else> --%>
</td>
</div>
</body>
</sj:div>
</html>

Resources