Language Contenttype Pageencoding Prefix Uri: "Java" "Text/Html Charset Iso-8859-1" "Iso-8859-1" "C"

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Document Upload Process Part#2

#1 JSP Code : Document.jsp

<%@ page language="java" contentType="text/html;


charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>welcome to Documents Page</h2>
<form action="upload" method="post" enctype="multipart/form-
data">
<pre>
SELECT DOCUMENT: <input type="file" name="fileOb"/>
<input type="submit" value="Upload"/>
</pre>
</form>
<table border="1">
<tr>
<th>ID</th>
<th>NAME</th>
<th>LINK</th>
</tr>
<c:forEach items="${list}" var="ob">
<tr>
<td> <c:out value="${ob[0]}"/> </td>
<td> <c:out value="${ob[1]}"/> </td>
<td>
DOWNLOAD
</td>
</tr>
</c:forEach>
</table>
<br/>
${message}
</body>
</html>

IDao and DaoImpl Code:

package com.app.dao;

import java.util.List;

import com.app.model.Document;

public interface IDocumentDao {

public Integer saveDocument(Document doc);


public List<Object[]> getDocsIdAndNames();
}

package com.app.dao.impl;

import java.util.List;

import
org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.app.dao.IDocumentDao;
import com.app.model.Document;
@Repository
public class DocumentDaoImpl implements IDocumentDao {
@Autowired
private HibernateTemplate ht;

@Override
public Integer saveDocument(Document doc) {
return (Integer) ht.save(doc);
}

@Override
public List<Object[]> getDocsIdAndNames() {
//SQL:select fid,fname from doctab
String hql="select fileId,fileName from
com.app.model.Document";
List<Object[]> data=(List<Object[]>) ht.find(hql);
return data;
}

#3 Controller:

package com.app.controller;

import java.util.List;

import
org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import
org.springframework.web.multipart.commons.CommonsMultipartFi
le;
import com.app.model.Document;
import com.app.service.IDocumentService;

@Controller
@RequestMapping("/docs")
public class DocumentController {
@Autowired
private IDocumentService service;

//1. show Document Page


@RequestMapping("/all")
public String showDocs(ModelMap map) {
//show all uploaded docs
List<Object[]> list=service.getDocsIdAndNames();
map.addAttribute("list",list);

return "Documents";
}

//2. on click upload

@RequestMapping(value="/upload",method=RequestMethod.POST)
public String uploadDoc(
@RequestParam("fileOb") CommonsMultipartFile
file,
ModelMap map)
{

if(file!=null) {
//convert CMF data to Model class obj
Document doc=new Document();
//doc.setFileId(fileId); auto generated
doc.setFileName(file.getOriginalFilename());
doc.setFileData(file.getBytes());
int id=service.saveDocument(doc);
map.addAttribute("message", "Saved with
Id:"+id);
}
//show all uploaded docs
List<Object[]> list=service.getDocsIdAndNames();
map.addAttribute("list",list);

return "Documents";
}

You might also like