Showing posts with label Struts 2. Show all posts
Showing posts with label Struts 2. Show all posts

Saturday, March 1, 2008

Handling multiple checkboxes in Struts 2

Many a time in Web applications we implement a form with a table of records with checkboxes , allowing users to select some rows for performing operations on selected rows. In this post let us look at how it is implemented.

This example is based on Struts 2 framework, with StudentAction as the action class and student.jsp as JSP to display a list of students

The general requirement in these forms is to be able to get the list of selected rows in the "Action" class to perform the required operations and when the control is passed to render the jsp the previously selected rows should be still shown as selected. To achieve this we will add a special field to the StudentAction to hold the list of selected rows (normally identified with some primary key) , as shown in the below code snippet from StudentAction.java


public class StudentAction extends ActionSupport {
List students = new ArrayList();
List selected = new ArrayList();
public List getSelected() {
return selected;
}
public void setSelected(List selected) {

this.selected = selected;
}


.....



The corresponding JSP page to display the list of students with a checkbox will have to name that checkbox as "selected" as shown in below snippet

 















What this code does ? look at the line





if you find everything is straight forward except value="%{name in selected}" , it is a OGNL expression which returns "true" if the value returned by getName() (on the StudentAction instance) is contained in the list returned by getSelected() method, and because this list will be empty for the first time no checkbox will be selected (as the above expression returns false), on clickin Submit button after selecting some checkboxes Struts2 will automagically populates the selected List with the values of selected checkboxes (which happens to be name here), after the performing the business logic when the page is displayed again the above expresion will return true for the previously selected names resulting in those checkbox rendered selected.

Easy ?

Sunday, February 24, 2008

Implementing File download feature in Struts 2 & Struts

Although file upload is a common feature that many struts application developers implement , it is less common to implement file download , in this post we will see how to provide file download feature in your struts 2 application.

Before we see how to implement this feature, let us first understand the requirement

1) A web page contains a link , which upon clicking it responds with a browser's file save dialog with a default name
2) User can choose the save location to save the file (as with any file save dialog)

This example uses a simple ActionClass with name DownloadAction and a jsp to show the link for downloading an arbitrary file



Let us start with the JSP , this is the simplest thing with a link pointing to the action class


Click to download


The action class which provides the download of file just needs to implement the getInputStream() method returning the InputStream of the required file to dowload !! Simple ...isn't ?

DownloadAction.java

public class DownloadAction {
private String fileName="YOUR FILE PATH";
public InputStream getInputStream(){
try{
return new FileInputStream(new File(fileName));
}catch(Exception e){}
}
public String execute(){
return "success"
}
}


Now the final thing is to wire these up in struts.xml , NOTE... there is something that is different from other regular configurations!! ... unlike other cases the control need not be passed to the JSP , instead we want the content to be handled by the browser itself (to bring up required download dialog), hence the result type is specified as "stream", which by default looks for getInputStream method (it can be configured) and sends it back as response














Easy !! isn't ? Then how about the implementation in Struts 1.xx ?

We can achieve this with the following steps

1) In the Action Class obtain the servlet response object
2) get the OutputStream from the response object
3) Get the InputStream of the target file (which we want to send to the browser)
4) Write the contents of the file to the Outputstream of step #2.
5) Don't forward the request by returning null to terminate the action.

Now, let us take a look at the code snippet of the DownloadAction.


...
...
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
File f = new File("C:\\myTestFile.xml");
response.setContentType("application/octet");
byte[] buffer = new byte[2048];
FileInputStream inputStream=new FileInputStream(f);
int bytesRead = inputStream.read(buffer);
ServletOutputStream outputStream=response.getOutputStream();
while (bytesRead >= 0) {
if (bytesRead > 0) {
outputStream.write(buffer, 0, bytesRead);
}
bytesRead = inputStream.read(buffer);
}
outputStream.flush();
//no need to forward ..
return null;
}


Here for the sake of brevity I have hardcoded the file name you will get the file name Or file Object probably from your models or from the form ..(Or any other way)

Thats it!! Hope it helps