Struts2 Form in parts - struts2

I was wondering if I can do this in struts 2:
<h4>Select Category</44>
<s:form id="Step01Page" action="Step01Page">
<s:select name="nombreCategoria" list="categorias"
listKey="nombre" listValue="nombre" label="Categorias"
headerKey="Crear" headerValue="Crear Nueva"/>
</s:form>
<h4>Name of Category</h4>
<s:form id="Step01Page" action="Step01Page">
<s:textfield label="Nombre de la Categoria"/>
</s:form>
<sx:submit formId="Step01Page" value="Siguiente" cssStyle="float:right;"/>
I'm doing the DOJO plugin ofr Struts 2.

I don't think that breaking up the form in that way would work. Since Struts by default formats the form in a table, you can insert lines into the table in the middle of your form like so:
<s:form id="Step01Page" action="Step01Page">
<s:select name="nombreCategoria" list="categorias"
listKey="nombre" listValue="nombre" label="Categorias"
headerKey="Crear" headerValue="Crear Nueva"/>
</s:form>
<tr><td colspan="2"><h4>Name of Category</h4></td></tr>
<s:form id="Step01Page" action="Step01Page">
<s:textfield label="Nombre de la Categoria"/>
</s:form>

Related

struts2 with 3 dependent dropdowns without using ajax

I'm using Struts 2 and MySQL.
In my JSP I have 3 select controls:
<s:select list="country" name="country" onchange="getstate();" />
<s:select list="state" name="state" onchange="getcity();" />
<s:select list="city" name="city" />
In my action class:
public string displayDetails() {
getCountry();
getState(getCountry());
getCity(getState());
}
If I want to get state I have to access getCountry() method again. How can I avoid that call every time?
If I'm not adding all the action in one method I'm getting "list field is not filled" error.

How to automatically populate value from action class to the JSP TextBox

I am able to print value from Java Action Class to JSP Page without textbox, But when I want to put that value in a TextBox in JSP, its not coming.
My Code is :
<S:form>
<s:textfield name="toDate" key="td" > <s:property value="fromDate"/> </s:textfield>
<s:textfield name="fromDate" key="fd"> <s:property value="toDate" /> </s:textfield>
<s:submit value="Confirm"></s:submit>
</s:form>
toDate and fromDate is variables in Java Class.
Just dive in the Struts2 UI Tag documentation:
key : Set the key (name, value, label) for this particular component
If you want them different (eg. toDate the value to set, td the value to read, and so on), specify all of them:
<s:form>
<s:textfield name="toDate" value="td" label="To date" />
<s:textfield name="fromDate" value="fd" label="From date" />
<s:submit value="Confirm" />
</s:form>
(Obviously according to your getters and setters).
You can set value in TextBox using value attribute of <s:textfield> tag to value come from action class.
For this you have your getter and setter method for that variable in action class and read that value by below code:
For Example :
<s:textfield name="some_name" value="%{variable}"/>
your code look like this:
<s:form>
<s:textfield name="toDate" value="%{fromDate}" />
<s:textfield name="fromDate" value="%{toDate}" />
<s:submit value="Confirm" />
</s:form>

Multiline textbox in Formview with DynamicDataTemplatesCS

Using model binding with the above like so:
<asp:FormView runat="server" ID="ConversationForm" DefaultMode="Edit"
OnCallingDataMethods="ConversationForm_CallingDataMethods"
ItemType="MyApp.Model.Conversation"
DataKeyNames="ConversationID"
SelectMethod="GetConversation"
UpdateMethod="UpdateConversation"
OnItemUpdated="Conversation_ItemUpdated">
<EditItemTemplate>
<fieldset>
<legend>Conversation Notes:</legend>
<ol>
<asp:DynamicEntity runat="server" Mode="Edit" />
</ol>
</fieldset>
<asp:Button ID="btnUpdate" runat="server" Text="Save" CommandName="Update" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CausesValidation="false" OnClick="btnCancel_Click" />
</EditItemTemplate>
</asp:FormView>
The Conversation entity basically has one property, "Text", which should contain freeform text captured by the user.
The DynamicEntity control generates a simple textbox for this property as it has a datatype of string.
How do I tell it to create a multiline textbox instead?
Can I add some sort of data annotation to the Conversation class that will tell the Dynamic Templates to create a multiline textbox?
It does not seem to be possible so I had to revert to using a static asp:Textbox control with the TextMode="MultiLine" property set.

struts2 TreeMap: keys with spaces not appearing in action

JSP Code:
<s:iterator value="#currentRequisitionGroup.plFldWrap.allPFields" var="pMap" status="hStatus">
<s:iterator value="#pMap.value.paramMdlList" var="paramModel" status="fStat">
<li>
<label>
<s:property value="#paramModel.parameterName" />
</label>
<s:set var="cEdit" value="%{#paramModel.isEditable}"> </s:set>
<s:if test="%{#cEdit == true}">
<s:textfield id="paramId_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[% {#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].prmValue"/>
</s:if>
<s:else>
<s:textfield id="paramId_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[% {#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].prmValue" readonly="true"/>
</s:else>
</li>
<s:hidden id="prmId_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[%{#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].parameterId"></s:hidden>
<s:hidden id="paramName_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[%{#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].parameterName"></s:hidden>
<s:hidden id="pGId_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[%{#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].parameterGroupId"></s:hidden>
<s:hidden id="seqNo_%{#paramModel.parameterId}" name="rqPGPrmMdl.rqGrp[%{#cGStat.index}].plFldWrap.allPFields['%{(#pMap.key)}'].paramMdlList[%{#fStat.index}].sequenceNumber"></s:hidden>
</s:iterator>
</s:iterator>
The problem is that some values do not appear in action.
Investigation till now indicates that, if the corresponding html input has space in the key of allPFields, that value doesn't appear in action.
<input id="prmId_30" type="hidden" value="30"
name="rqPGPrmMdl.rqGrp[1].plFldWrap.allPFields['Emp System'].paramMdlList[0].parameterId">
However, if the corresponding html input has no space in the key of allPFields, that value appears in action.
<input id="prmId_46" type="hidden" value="30" name="rqPGPrmMdl.rqGrp[1].plFldWrap.allPFields['Emp'].paramMdlList[0].parameterId">
Here is what the logs say:
xwork2.interceptor.ParametersInterceptor - Parameter [rqPGPrmMdl.rqGrp[1].plFldWrap.allPFields['Emp System'].paramMdlList[0].parameterId] **didn't match acceptedPattern pattern!**
This is happening because white spaces are not accepted in parameter names. You can change acceptParamNames parameter of the ParametersInterceptor, BUT as the documentation states
acceptParamNames - a comma delimited list of regular expressions to describe a whitelist of accepted parameter names. Don't change the default unless you know what you are doing in terms of security implications
So I suggest you to get rid of white spaces in parameters names.

How to get rid of the auto generated j_idt ID in composite component

I'm using below composite component:
<composite:interface>
<composite:attribute name="inputId" />
</composite:interface>
<composite:implementation>
<h:panelGrid id="myTableId">
<h:inputText id="#{cc.attrs.inputId}" value="..." />
...
</h:panelGrid>
</composite:implementation>
And I'm using it in my form as below:
<h:form id="myForm">
<myCompositeComp:test inputId="myInputTextBoxId" />
<h:form>
I've verified the view source for the page and this is how it is generated:
<table id="myForm:j_idt90:myTableId">
...
<input type="text" id="myForm:j_idt90:myInputTextBoxId" />
</table>
How can I get rid of j_idt90 here? Is it the id of my composite component? I read from one of BalusC post that this issue will get fixed if I declare id as static. But I'm not able to identify the place to declare it in my code. Also can I assume <h:panelGrid> is a kind of UINamingContainer?
Yes it is the id of your composite component, <h:panelGrid> is not a UINaminContainer but the composite component is (it has to be, otherwise you would have duplicate IDs if you use it several times inside the same form for example).
Why do you need to get rid of the ID? You can set it yourself if that solves your problem:
<h:form id="myForm">
<myCompositeComp:test id="myComp" attr1="" attr2="" />
<h:form>
the genereated html should look like this:
<table id="myForm:myComp:myTableId">
....
<input type="text" id="myForm:myComp:myInputTextBoxId"
</table>

Resources