Struts 2 textfield label doesn't show in table - struts2

The following jsp file fragment using struts2 tags doesn't show the textfield label. How can I fix this? Is it possible to have the label displayed by using only struts2 tags?
<s:form action="voucher" theme="simple">
<table width=50% border="5">
<tr>
<td>
<s:textfield label="Enter the voucher hrn" name="voucher.SN" />
</td>
<td>
<s:submit value="Check state" />
</td>
</tr>
<tr>
<td>
<s:radio list="#{'used': 'Mark as used', 'available': 'Mark as available'}" name="selections" value="%{'used'}"></s:radio>
</td>
<td>
<s:submit value="Update state" />
</td>
</tr>
</table>
</s:form>

It is because you are using simple theme in your form. Remove theme="simple" from <s:form> tag then Struts2 will do table layout as well field errors and labels for you.
BTW if you really need to use your custom layout then use <s:label> tags for labels.

Related

When do you use th:remove="tag" versus th:block?

The following two blocks would evaluate the same, so when would you use th:remove="tag" over th:block?
<th:block th:text="${myBean.value}">[value]</th:block>
versus
<span th:remove="tag" th:text="${myBean.value}">[value]</span>
Since they can be used interchangeably I think it is opinion based... but for readability, my opinion is:
<th:block /> should be used for doing structural logic (th:if, th:each, etc..) and only when it's not possible on the parent tag. For example, in the case you have to contain more than one element in a loop -- e.g, a loop that produces 2 table rows for each object:
<table>
<th:block th:each="object: ${objects}">
<tr>
<td th:text="${object.data1}" />
<td th:text="${object.data2}" />
</tr>
<tr>
<td th:text="${object.data3}" />
<td th:text="${object.data4}" />
</tr>
</th:block>
<table>
th:remove should only be used for example data that should only be rendered when viewing the file in a browser/prototyping w/o rendering the thymeleaf:
<table>
<th:block th:each="object: ${objects}">
<tr>
<td th:text="${object.data1}" />
<td th:text="${object.data2}" />
</tr>
<tr>
<td th:text="${object.data3}" />
<td th:text="${object.data4}" />
</tr>
</th:block>
<tr th:remove="all">
<td>Mild Cinnamon</td>
<td>1.99</td>
</tr>
<tr th:remove="all">
<td>Other</td>
<td>Data</td>
</tr>
<table>
In the specific case where you want to output data/text without a tag, I prefer inline expressions. They're enabled by default in thymeleaf 3. So:
[[${myBean.value}]] instead of <th:block th:text="${myBean.value}">[value]</th:block>

How to disable label of <s:textfield> tag in Struts2

I have the following tag in struts2:
<s:textfield type="text" key="maquina" label="" labelSeparator="" style="width:100;"/>
and I want to disable the label by this tag, is this moment in my html code is generated as shown below:
<table>
<thead>
<tr>
<th>Hora</th>
<th>Ruta</th>
<th>Maquina</th>
</tr>
</thead>
<tbody>
<%int i=10;%>
<s:iterator value="datosPlan" var="datosPlanLoading" status="valorDatosPlan">
<%--//cambia de color la fila en la que se encuentra le cursor--%>
<tr id="<%=i%>"
<%
if(i%2 == 0)
{
%>
class="alt"
style="background: #E1EEf4;color: #00557F;"
onmouseover="destacarFila(this.id);"
onmouseout="colorOriginalFila(this.id,0);"
<%
}
else
{ %>
onmouseover="destacarFila(this.id);"
onmouseout="colorOriginalFila(this.id,1);"
<%
}
i++;
%>
>
<%--<input type="hidden" id="<%="editar"%>${listaHorariosLoading.codigoHorario}" value="${listaHorariosLoading.nombreHorario}">--%>
<td>
<p>
<s:property value="hora"/>
</p>
</td>
<td>
<p>
<s:property value="ruta"/>
</p>
</td>
<td>
<p>
<s:textfield type="text" name="maquina" style="width:100;"/>
</p>
</td>
</tr>
</s:iterator>
</tbody>
therefore I want to disable label because I want to just the input type.
thanks in advance.
If you use key="maquina" then it will generate label for you.
If you use label="" then it will generate label for you.
Don't use key and also do not use label attributes.
Try this
<s:textfield type="text" id="maquina" name="maquina" style="width:20;" />
Output:
<input id="maquina" type="text" style="width:20;" value="" name="maquina">
</input>
EDIT :
<td class="tdLabel"></td> will be generated if you use <s:form>
Try to use html form tag <form> instead of <s:form>.
For Example
<form action="someAction" method="get" >
<s:textfield type="text" id="maquina" name="maquina" style="width:20;" />
</form>
Note:
And if you want to use tag then you need to implement either template in freemarker or custom theme.
Tutorial 1
Tutorial 2
In struts.xml
<constant name="struts.ui.theme" value="simple"/>
will also remove default template.
because I want to just the input type
This is only possible if you use simple theme configured to be used by the tag.

Iterator, set, if-else tags of Struts2(Getting the value of s:radio and applying if-else )

I have a jsp page in which I have two radio buttons and a select tag. Now, if the first radio button is clicked than I want to make that select tag disable but I am fail to do this , I have tried using "disabled" property. Following is the code.
Jsp Page
<table align="center">
<s:iterator value="FirstObjectList" status="AuthorTypeStatus">
<tr>
<td>
<s:radio name="radio_SelectedValue" list="{ObjectName}" listKey="ObjectKey" listValue="ObjectName" value="DefaultObject"/>
</td>
</tr>
</s:iterator>
<s:if test="%{#radio_SelectedValue == 'ObjectName1'}">
<s:set name="isSelectDisabled" value="false"/>
</s:if>
<s:else>
<s:set name="isSelectDisabled" value="True"/>
</s:else>
<tr >
<td colspan="2">
Select Parent Discover Lab
</td>
</tr>
<tr>
<td>
<s:select name="select_SelectedValue" headerKey="DefaultObject" headerValue="ParentObject" list="ObjectList" listKey="ObjectListKey" listValue="ObjectListValue" disabled="%{isSelectDiabled}">
<s:iterator value="ObjectList">
</s:iterator>
</s:select>
<s:submit value="Continue">
</s:submit>
</td>
</tr>
All the getter and setter methods are fixed..is this possible without javascript?
I'm a little confused by your code block, but I think I understand what you're aiming for. Something like this should work
<tr>
<td>
<s:radio name="radio_SelectedValue" list="%{radioList}" listKey="ObjectKey" listValue="ObjectName" value="DefaultObject"/>
</td>
</tr>
<s:if test="%{radio_SelectedValue.equals('myChosenValueFromTheList')}">
<s:select name="select_SelectedValue" headerKey="-1" headerValue=" " list="ObjectList" listKey="ObjectList.Key" listValue="ObjectList.Value" />
</s:if><s:else>
<s:select name="select_SelectedValue" headerKey="-1" headerValue=" " list="ObjectList" listKey="ObjectList.Key" listValue="ObjectList.Value" disabled="disabled" />
</s:else>
I'm not sure what you're doing with the iterators with these tags, they handle the list propagation themselves. Simply tell the tag what list to use and struts will populate the tags appropriately. You can then do a conditional based on the value of the radio when the page renders and perform magic on the select tag as shown above.

why struts UI tags can not be put together in the same row?

I need 2 textareas in the same row and tried as below but it is printing in the next row.
All struts tags are have same problem.Is there any alternative method ? Please suggest.
<table>
<tr>
<td>
<s:textarea name="1"/>
</td>
<td>
<s:textarea name="2"/>
</td>
</tr>
</table>
This is due to Struts2's default rendering based on its default theme. I find this tutorial gives a good idea of Struts2 theming/templating: http://www.mkyong.com/struts2/working-with-struts-2-theme-template/
If you don't want to use the default and want to lay it out as you have it, try:
<table>
<tr>
<td>
<s:textarea name="1" theme="simple"/>
</td>
<td>
<s:textarea name="2" theme="simple"/>
</td>
</tr>
</table>

How do I use Asp.net MVC to validate a list property has a minimum number of items (count=N)?

I have a view model that has a property that looks like this
Property SelectedGroups() as List(of string)
In the view I have something like this
<table>
<tr>
<th>Description</th>
</tr>
<tr>
<td>
<input type="hidden" name="SelectedGroups" value="one" />
description one
</td>
</tr>
<tr>
<td>
<input type="hidden" name="SelectedGroups" value="two" />
description two
</td>
</tr>
<tr>
<td>
<input type="hidden" name="SelectedGroups" value="three" />
description three
</td>
</tr>
</table>
The table rows are added and removed with jquery. Is there a way to create a validation attribute on SelectedGroups property that will require a minimum number of items for the list? This can be done with javascript but I would like it to work with
<% Html.EnableClientValidation()%>
<%: Html.ValidationSummary(False)%>
You would have to write a custom validator. The built-in validators aren't that sophisticated.
ScottGu has written a good article on custom validators: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

Resources