Let's go back to the FileListController we created earlier. So far it looks like this:
package edu.asu.diging.tutorial.spring.web; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class FileListController { @RequestMapping(value = "files") public String listFiles(Model model) { return "files"; } }
In line 8 we tell Spring that this method should be called when the user goes to the "files" path of our application. If you named your app fileManager, then this would be http://your.host/fileManager/files (e.g. http://localhost:8080/fileManager/files). In line 10, we define, which jsp page should be rendered (files.jsp). But other than that our method doesn't do anything. Eventually, however, we want the method to retrieve all files to show them on the page. For now, let's just create some dummy data, so that we can finish coding the jsp page.
The first domain class
To create dummy data, we need a class that can hold our dummy data. We could put all the info about the files we want to display on the page, into a map structure, but that's not very maintainable or readable. So, we will create a domain class holding those information. Let's create a package called "core.impl" and create a new class in there called "Document". The document should have the following fields with getter and setter methods:
id (String)
title (String)
uploader (String)
version (int)
date (Date)
current (boolean)
pathToFile (String)
Also, you should write some Javadoc explaining what this class is doing. In addition to your class, create an interface "IDocument" for your class. Your final class should look like this:
package edu.asu.diging.tutorial.spring.core.impl; import java.util.Date; import edu.asu.diging.tutorial.spring.core.IDocument; /** * This class represents one document in the system. It points to a file in the * file system and contains some metadata about the file such as who uploaded it, * a title, and the date a file was uploaded. * * @author jdamerow * */ public class Document implements IDocument { private String id; private String title; private String uploader; private int version; private Date date; private boolean current; private String pathToFile; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getUploader() { return uploader; } public void setUploader(String uploader) { this.uploader = uploader; } public int getVersion() { return version; } public void setVersion(int version) { this.version = version; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public boolean isCurrent() { return current; } public void setCurrent(boolean current) { this.current = current; } public String getPathToFile() { return pathToFile; } public void setPathToFile(String pathToFile) { this.pathToFile = pathToFile; } }
Create dummy objects
Now, that we have a class to hold the information we want to display, let's create some dummy objects. In the FileListController, extend the method "listFiles" so that it looks like this:
@RequestMapping(value = "files") public String listFiles(Model model) { IDocument doc1 = new Document(); doc1.setCurrent(true); doc1.setDate(new Date()); doc1.setId("ID1"); doc1.setTitle("Document 1"); doc1.setUploader("Ernie"); doc1.setVersion(1); IDocument doc2 = new Document(); doc2.setCurrent(true); Calendar cal = Calendar.getInstance(); cal.set(2016, 1, 10, 13, 5); doc2.setDate(cal.getTime()); doc2.setId("ID2"); doc2.setTitle("Expenses.xls"); doc2.setUploader("Arthur Dent"); doc2.setVersion(2); List<IDocument> documents = new ArrayList<IDocument>(); documents.add(doc1); documents.add(doc2); model.addAttribute("documents", documents); return "files"; }
Note: this is dummy code that we will replace with real code later. For now that let's us adjust our jsp page to display files dynamically.
Open the file files.jsp and adjust the table in it to this:
<!-- Table --> <table class="table"> <tr> <th>Filename</th> <th>Uploader</th> <th>Date</th> <th>Version</th> <th>Actions</th> </tr> <c:forEach var="doc" items="${documents}"> <tr> <td><a title="Download file" href="">${doc.title}</a></td> <td>${doc.uploader}</td> <td>${doc.date}</td> <td>v${doc.version}</td> <td><a title="Upload new version"><span class="glyphicon glyphicon-cloud-upload"></span></a></td> </tr> </c:forEach> </table>
Reload your browser. You should now see the dummy document objects we've created in the controller in your table (it might take a minute for Eclipse to redeploy your changes, so just reload the page a couple of times until you see your changes).