
|
Struts 2 / DisplayTag example
Everyone,
I've had a hard time getting DisplayTag and Struts 2 to play nicely together. I've finally achieved success, and I wanted to post this example to the community, primarily because I've found information on these two hard to find.
This example displays a list of test scenarios:
<display:table name="${scenarioList}" class="its" uid="row" pagesize="20" sort="list" requestURI="" >
<display:column title="" >
<s:hidden name="scenarioList[%{#attr.row_rowNum - 1}].scenarioId" value="%{#attr.row.scenarioId}"/>
<s:checkbox name="select[%{#attr.row_rowNum - 1}]" value="select[%{#attr.row_rowNum - 1}]" />
</display:column>
<display:column property="number" sortable="true" href="testScenario2.jsp" paramId="id" paramProperty="scenarioId" />
<display:column property="description" sortable="true" />
<display:column property="standard" sortable="true" />
</display:table>
I've defined scenarioList (java.util.List) on my Struts action. I did not need to use the <s:set> tag as seen in other posts. I'm using "uid" instead of "id" because the displaytag docs says I need to. I'm paging and sorting successfully -- the requestURI setting seems to have a lot to do with that.
The first column of my table displays a checkbox (the idea here is to allow user selection of multiple rows). The OGNL syntax took me a long time to figure out, the "[]" mean an index, %{} contains an OGNL expression, #attr means look at the page context, row_rowNum is pure DisplayTag for the row number (based on uid), and -1 is needed to get DisplayTag to start at 0 rather than 1.
Hope this info helps someone -- K
|

|
Re: Struts 2 / DisplayTag example
[QUOTE]
Hi Kenk,
Im also trying to use display tag in struts 2, can you please provide your sample code where you have done this. Also i tried using display tag within struts 2 tabbed pane, but im using image in one of column but image is not displayed....
Also im calling struts 1 'action.do' from that display tag requestURI='action.do', so if i do like this, my action is loaded in a new browser, instead within the struts 2 tabbed pane.
Please help me out in this.....
Looking forward for your reply..
Regards,
Prabhu.
[/QUOTE]
|

|
Re: Struts 2 / DisplayTag example
Hi Kenk,
Thank for your example.
I have a question , it would be great if you can help me.
In the hidden field the value you are capturing is scenarioId which i assume is a string value.
I want to capture the Object a DTO object which the user select.
Is there anyway we can capture the object.
I want all the TaskDTO which the user select , which is a part of the List which display table iterators.
Regards
Siddiq.
kenk wrote:
Everyone,
I've had a hard time getting DisplayTag and Struts 2 to play nicely together. I've finally achieved success, and I wanted to post this example to the community, primarily because I've found information on these two hard to find.
This example displays a list of test scenarios:
<display:table name="${scenarioList}" class="its" uid="row" pagesize="20" sort="list" requestURI="" >
<display:column title="" >
<s:hidden name="scenarioList[%{#attr.row_rowNum - 1}].scenarioId" value="%{#attr.row.scenarioId}"/>
<s:checkbox name="select[%{#attr.row_rowNum - 1}]" value="select[%{#attr.row_rowNum - 1}]" />
</display:column>
<display:column property="number" sortable="true" href="testScenario2.jsp" paramId="id" paramProperty="scenarioId" />
<display:column property="description" sortable="true" />
<display:column property="standard" sortable="true" />
</display:table>
I've defined scenarioList (java.util.List) on my Struts action. I did not need to use the <s:set> tag as seen in other posts. I'm using "uid" instead of "id" because the displaytag docs says I need to. I'm paging and sorting successfully -- the requestURI setting seems to have a lot to do with that.
The first column of my table displays a checkbox (the idea here is to allow user selection of multiple rows). The OGNL syntax took me a long time to figure out, the "[]" mean an index, %{} contains an OGNL expression, #attr means look at the page context, row_rowNum is pure DisplayTag for the row number (based on uid), and -1 is needed to get DisplayTag to start at 0 rather than 1.
Hope this info helps someone -- K
|

|
Re: Struts 2 / DisplayTag example
Hi siddiq,
Actually, scenarioId in my example is a Long. Struts, Stripes, Spring MVC, etc. have no problem translating back and forth between String and Long. It's the unique ID on the scenario on the DB. I use the checkbox to determine which rows the user selected (just like you want to do), then use the ID on the back end to lookup the corresponding object. I happen to be using Hibernate to lookup the object in a relational DB, but I could just as easily use the ID to lookup an item in a Map in the user's session, or something else.
I imagine you could serialize your entire TaskDTO to a String and place it in a hidden field, but I think a better way would be to rely on some kind of ID. That way you don't have to drag your serialized objects across http just to find out what was selected.
Hope that helps,
Ken
|

|
Re: Struts 2 / DisplayTag example
Hi Ken,
Thanks for your quick reply. I really appricate that.
yeah there is a filed taskId which is unique and from which i can get the required TaskDTO.
This is the ideal way to get the selected object.
But i was wondering is there any way to get the object without going to DB something like using <s:checkboxlist /> tag.
Anyways i am getting my DTO as the same way you did.
Thanks for you help again.
Regards
Siddiq.
kenk wrote:
Hi siddiq,
Actually, scenarioId in my example is a Long. Struts, Stripes, Spring MVC, etc. have no problem translating back and forth between String and Long. It's the unique ID on the scenario on the DB. I use the checkbox to determine which rows the user selected (just like you want to do), then use the ID on the back end to lookup the corresponding object. I happen to be using Hibernate to lookup the object in a relational DB, but I could just as easily use the ID to lookup an item in a Map in the user's session, or something else.
I imagine you could serialize your entire TaskDTO to a String and place it in a hidden field, but I think a better way would be to rely on some kind of ID. That way you don't have to drag your serialized objects across http just to find out what was selected.
Hope that helps,
Ken
|

|
Re: Struts 2 / DisplayTag example
How did you get the checkbox on the same line as the display:column properties.
I am getting the checkbox on a line by itself for each row.
kenk wrote:
Everyone,
I've had a hard time getting DisplayTag and Struts 2 to play nicely together. I've finally achieved success, and I wanted to post this example to the community, primarily because I've found information on these two hard to find.
This example displays a list of test scenarios:
<display:table name="${scenarioList}" class="its" uid="row" pagesize="20" sort="list" requestURI="" >
<display:column title="" >
<s:hidden name="scenarioList[%{#attr.row_rowNum - 1}].scenarioId" value="%{#attr.row.scenarioId}"/>
<s:checkbox name="select[%{#attr.row_rowNum - 1}]" value="select[%{#attr.row_rowNum - 1}]" />
</display:column>
<display:column property="number" sortable="true" href="testScenario2.jsp" paramId="id" paramProperty="scenarioId" />
<display:column property="description" sortable="true" />
<display:column property="standard" sortable="true" />
</display:table>
I've defined scenarioList (java.util.List) on my Struts action. I did not need to use the <s:set> tag as seen in other posts. I'm using "uid" instead of "id" because the displaytag docs says I need to. I'm paging and sorting successfully -- the requestURI setting seems to have a lot to do with that.
The first column of my table displays a checkbox (the idea here is to allow user selection of multiple rows). The OGNL syntax took me a long time to figure out, the "[]" mean an index, %{} contains an OGNL expression, #attr means look at the page context, row_rowNum is pure DisplayTag for the row number (based on uid), and -1 is needed to get DisplayTag to start at 0 rather than 1.
Hope this info helps someone -- K
|

|
Re: Struts 2 / DisplayTag example
In your Struts 2 action, how are you retrieving which rows were selected from the jsp? Could you please post an example of the applicable action code.
How did you get the checkbox on the same line as the display:column properties.
I am getting the checkbox on a line by itself for each row.
kenk wrote:
Everyone,
I've had a hard time getting DisplayTag and Struts 2 to play nicely together. I've finally achieved success, and I wanted to post this example to the community, primarily because I've found information on these two hard to find.
This example displays a list of test scenarios:
<display:table name="${scenarioList}" class="its" uid="row" pagesize="20" sort="list" requestURI="" >
<display:column title="" >
<s:hidden name="scenarioList[%{#attr.row_rowNum - 1}].scenarioId" value="%{#attr.row.scenarioId}"/>
<s:checkbox name="select[%{#attr.row_rowNum - 1}]" value="select[%{#attr.row_rowNum - 1}]" />
</display:column>
<display:column property="number" sortable="true" href="testScenario2.jsp" paramId="id" paramProperty="scenarioId" />
<display:column property="description" sortable="true" />
<display:column property="standard" sortable="true" />
</display:table>
I've defined scenarioList (java.util.List) on my Struts action. I did not need to use the <s:set> tag as seen in other posts. I'm using "uid" instead of "id" because the displaytag docs says I need to. I'm paging and sorting successfully -- the requestURI setting seems to have a lot to do with that.
The first column of my table displays a checkbox (the idea here is to allow user selection of multiple rows). The OGNL syntax took me a long time to figure out, the "[]" mean an index, %{} contains an OGNL expression, #attr means look at the page context, row_rowNum is pure DisplayTag for the row number (based on uid), and -1 is needed to get DisplayTag to start at 0 rather than 1.
Hope this info helps someone -- K
|

|
Re: Struts 2 / DisplayTag example
Hi,
I am trying to use a radio button instead of checkbox. I am unable to figure out how to determine which row was selected. How do you determine it with your check box?
kenk wrote:
Everyone,
I've had a hard time getting DisplayTag and Struts 2 to play nicely together. I've finally achieved success, and I wanted to post this example to the community, primarily because I've found information on these two hard to find.
This example displays a list of test scenarios:
<display:table name="${scenarioList}" class="its" uid="row" pagesize="20" sort="list" requestURI="" >
<display:column title="" >
<s:hidden name="scenarioList[%{#attr.row_rowNum - 1}].scenarioId" value="%{#attr.row.scenarioId}"/>
<s:checkbox name="select[%{#attr.row_rowNum - 1}]" value="select[%{#attr.row_rowNum - 1}]" />
</display:column>
<display:column property="number" sortable="true" href="testScenario2.jsp" paramId="id" paramProperty="scenarioId" />
<display:column property="description" sortable="true" />
<display:column property="standard" sortable="true" />
</display:table>
I've defined scenarioList (java.util.List) on my Struts action. I did not need to use the <s:set> tag as seen in other posts. I'm using "uid" instead of "id" because the displaytag docs says I need to. I'm paging and sorting successfully -- the requestURI setting seems to have a lot to do with that.
The first column of my table displays a checkbox (the idea here is to allow user selection of multiple rows). The OGNL syntax took me a long time to figure out, the "[]" mean an index, %{} contains an OGNL expression, #attr means look at the page context, row_rowNum is pure DisplayTag for the row number (based on uid), and -1 is needed to get DisplayTag to start at 0 rather than 1.
Hope this info helps someone -- K
|

|
Re: Struts 2 / DisplayTag example
Sudheendra, Atilim,
I had to go back through my old code from that project, but here's the relevant parts of my action class. Ignore the Spring 2.5 references if you're not familiar with that technology:
package com.xxx.web.struts;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.xxx.domain.TestScenario;
import com.xxx.domain.TestScenarioCategory;
import com.xxx.service.LaunchService;
import com.xxx.service.TestDataService;
/**
* <p>Handle all actions associated with the testScenarioList.jsp page.</p>
*/
@Scope("prototype")
@Controller ( "testScenarioListAction" )
@SuppressWarnings("serial")
public class TestScenarioListActionBean {
private List<TestScenario> scenarioList;
private List<Boolean> select;
private TestDataService testDataService;
private LaunchService launchService;
@Resource
public void setTestDataService(TestDataService testDataService) { this.testDataService = testDataService; }
@Resource
public void setLaunchService(LaunchService launchService) { this.launchService = launchService; }
public List<TestScenario> getScenarioList() { return scenarioList; }
public void setScenarioList(List<TestScenario> scenarioList) { this.scenarioList = scenarioList; }
public List<Boolean> getSelect() { return select; }
public void setSelect(List<Boolean> select) { this.select = select; }
/**
* Executed immediately prior to the page displaying.
*/
public String doShow() {
super.doShow();
scenarioList = testDataService.getDisplayTestScenarioList( criteria );
return SUCCESS;
}
/**
* Executed when the user wants to submit multiple scenarios
*/
public String doLaunch() {
logger.debug ( "doLaunch method." );
// Loop through the selection checkboxes. Submit the selected scenarios:
int counter = 0;
for ( int i = 0; i < select.size(); i++ ) {
// If this checkbox was selected:
if ( select.get(i) != null && select.get(i) ) {
// Get the matching test scenario:
TestScenario ts = getScenarioList().get(i);
// ...and launch it:
logger.debug ( "launching scenario " + ts.getScenarioId() );
launchService.launchTestScenario( ts.getScenarioId() );
counter ++;
}
}
addActionMessage( counter + " Test Scenarios launched" );
return "GO_TO_TEST_EXECUTION";
}
}
The page has a "Launch" button on it that submits the form, the idea is that the user checks the items they want to run and launches them. 'select' and 'scenarioList' are simply properties, struts 2 populates them automatically depending on interceptors you setup.
I never tried using radio buttons here, for one thing this particular page required multi-selection.
Hope this helps,
K
|

|
Re: Struts 2 / DisplayTag example
Hi and thanks for a nice example! I have a follow-up question, what if the values I want to display in my checkboxes should instead be taken from some state in the db. For instance, lets say I have a list of orders which can either be packed or unpacked and then the user should be able to check/uncheck, press submit, and the orders will be iterated in my action and set corrspondingly reflecting the new state (packed/unpacked). So the question boils down to how to make the view display this?
Regards,
Patrik
kenk wrote:
Sudheendra, Atilim,
I had to go back through my old code from that project, but here's the relevant parts of my action class. Ignore the Spring 2.5 references if you're not familiar with that technology:
package com.xxx.web.struts;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.xxx.domain.TestScenario;
import com.xxx.domain.TestScenarioCategory;
import com.xxx.service.LaunchService;
import com.xxx.service.TestDataService;
/**
* <p>Handle all actions associated with the testScenarioList.jsp page.</p>
*/
@Scope("prototype")
@Controller ( "testScenarioListAction" )
@SuppressWarnings("serial")
public class TestScenarioListActionBean {
private List<TestScenario> scenarioList;
private List<Boolean> select;
private TestDataService testDataService;
private LaunchService launchService;
@Resource
public void setTestDataService(TestDataService testDataService) { this.testDataService = testDataService; }
@Resource
public void setLaunchService(LaunchService launchService) { this.launchService = launchService; }
public List<TestScenario> getScenarioList() { return scenarioList; }
public void setScenarioList(List<TestScenario> scenarioList) { this.scenarioList = scenarioList; }
public List<Boolean> getSelect() { return select; }
public void setSelect(List<Boolean> select) { this.select = select; }
/**
* Executed immediately prior to the page displaying.
*/
public String doShow() {
super.doShow();
scenarioList = testDataService.getDisplayTestScenarioList( criteria );
return SUCCESS;
}
/**
* Executed when the user wants to submit multiple scenarios
*/
public String doLaunch() {
logger.debug ( "doLaunch method." );
// Loop through the selection checkboxes. Submit the selected scenarios:
int counter = 0;
for ( int i = 0; i < select.size(); i++ ) {
// If this checkbox was selected:
if ( select.get(i) != null && select.get(i) ) {
// Get the matching test scenario:
TestScenario ts = getScenarioList().get(i);
// ...and launch it:
logger.debug ( "launching scenario " + ts.getScenarioId() );
launchService.launchTestScenario( ts.getScenarioId() );
counter ++;
}
}
addActionMessage( counter + " Test Scenarios launched" );
return "GO_TO_TEST_EXECUTION";
}
}
The page has a "Launch" button on it that submits the form, the idea is that the user checks the items they want to run and launches them. 'select' and 'scenarioList' are simply properties, struts 2 populates them automatically depending on interceptors you setup.
I never tried using radio buttons here, for one thing this particular page required multi-selection.
Hope this helps,
K
|

|
Re: Struts 2 / DisplayTag example
Hi Patrik,
That should be easy. Replace my domain object "TestScenario" with yours: "Order". Within it, define a boolean property called 'packed'. In the action where I have "private List<TestScenario> scenarioList;", replace it with "private List<Order> orderList;", with appropriate getters and setters. Throw away my "private List<Boolean> select;" property, you won't need it.
Then, in DisplayTag, the column for packed would be:
<display:column title="packed?" >
<s:checkbox name="orderList[%{#attr.row_rowNum - 1}].packed" />
</display:column>
Similar syntax for all of the struts 2 controls, i.e. a text input for the same field would be:
<s:textfield name="orderList[%{#attr.row_rowNum - 1}].packed />
I think that's it, but I may have made a mistake. Post back and let me know.
K
|

|
Re: Struts 2 / DisplayTag example
Hi Kenk och thanks for the quick reply. Your solution works for the checked versions, but if you uncheck a checkbox and press submit, there will not be a setPacked(false) called on the Order item... Only the checked checkboxes will be sent to model setter with value true... I guess one solution is to keep a packedList hidden input type for each checkbox, and in my Action compare this list with the original order list and take appropriate actions based on these...?
BTW, someone else in this thread asked about checkboxes ending up on a row of their own. A solution for this is to add the theme="simple" attribute on the s:checkbox element.
Cheers,
Patrik
kenk wrote:
Hi Patrik,
That should be easy. Replace my domain object "TestScenario" with yours: "Order". Within it, define a boolean property called 'packed'. In the action where I have "private List<TestScenario> scenarioList;", replace it with "private List<Order> orderList;", with appropriate getters and setters. Throw away my "private List<Boolean> select;" property, you won't need it.
Then, in DisplayTag, the column for packed would be:
<display:column title="packed?" >
<s:checkbox name="orderList[%{#attr.row_rowNum - 1}].packed" />
</display:column>
Similar syntax for all of the struts 2 controls, i.e. a text input for the same field would be:
<s:textfield name="orderList[%{#attr.row_rowNum - 1}].packed />
I think that's it, but I may have made a mistake. Post back and let me know.
K
|

|
Re: Struts 2 / DisplayTag example
Hi Patrick,
You've encountered the classic checkbox form submission problem. To summarize: in HTML forms, checkboxes are not submitted with the rest of the form if they are unchecked.
Struts 2 attempts to address this via the checkbox interceptor (see Struts 2 Interceptors), see if that helps (or switch to Stripes!).
thx,
K
|

|
Re: Struts 2 / DisplayTag example
hi, can you please explain( I've defined scenarioList (java.util.List) on my Struts action.) how you have defined to use List in jsp page using displat tag
thanks
Rudresh
kenk wrote:
Everyone,
I've had a hard time getting DisplayTag and Struts 2 to play nicely together. I've finally achieved success, and I wanted to post this example to the community, primarily because I've found information on these two hard to find.
This example displays a list of test scenarios:
<display:table name="${scenarioList}" class="its" uid="row" pagesize="20" sort="list" requestURI="" >
<display:column title="" >
<s:hidden name="scenarioList[%{#attr.row_rowNum - 1}].scenarioId" value="%{#attr.row.scenarioId}"/>
<s:checkbox name="select[%{#attr.row_rowNum - 1}]" value="select[%{#attr.row_rowNum - 1}]" />
</display:column>
<display:column property="number" sortable="true" href="testScenario2.jsp" paramId="id" paramProperty="scenarioId" />
<display:column property="description" sortable="true" />
<display:column property="standard" sortable="true" />
</display:table>
I've defined scenarioList (java.util.List) on my Struts action. I did not need to use the <s:set> tag as seen in other posts. I'm using "uid" instead of "id" because the displaytag docs says I need to. I'm paging and sorting successfully -- the requestURI setting seems to have a lot to do with that.
The first column of my table displays a checkbox (the idea here is to allow user selection of multiple rows). The OGNL syntax took me a long time to figure out, the "[]" mean an index, %{} contains an OGNL expression, #attr means look at the page context, row_rowNum is pure DisplayTag for the row number (based on uid), and -1 is needed to get DisplayTag to start at 0 rather than 1.
Hope this info helps someone -- K
|

|
Re: Struts 2 / DisplayTag example
can you give one complete example with action class
|

|
Re: Struts 2 / DisplayTag example
Hi Patrik,
Please send entire struct 2.x example code to rammohanparvatham@gmail.com.
Advance Thanks,
Ram Mohan
kenk wrote:
Hi Patrik,
That should be easy. Replace my domain object "TestScenario" with yours: "Order". Within it, define a boolean property called 'packed'. In the action where I have "private List<TestScenario> scenarioList;", replace it with "private List<Order> orderList;", with appropriate getters and setters. Throw away my "private List<Boolean> select;" property, you won't need it.
Then, in DisplayTag, the column for packed would be:
<display:column title="packed?" >
<s:checkbox name="orderList[%{#attr.row_rowNum - 1}].packed" />
</display:column>
Similar syntax for all of the struts 2 controls, i.e. a text input for the same field would be:
<s:textfield name="orderList[%{#attr.row_rowNum - 1}].packed />
I think that's it, but I may have made a mistake. Post back and let me know.
K
|