我需要使用下面的表单从两个数据库表(factory和factoryType)下载数据,但它无法正常工作。我设法只从一张桌子中获取数据......有人可以看看吗?
Factory table:factoryId,factoryname
FactoryType table: factoryType,factoryTypeId
FactoryConf table: factoryID,factoryTypeId
我们正在使用Hibernate进行数据库操作。
表格
<form:form id="FactoryUploadForm" method="post" modelAttribute="FactoryUploadForm" action="/submitUploadFactoryForm" enctype="multipart/form-data">
<table class="ui-widget" width="100%>
<tr>
<td valign="top" colspan=3 height="40px">
<div class="column span-15 highlight">
</div>
</td>
<td>
<h><a href="/download.do">Click here to download file</a></h>
</td>
</tr>
<tr>
<td>
<div class="column span-2_5"><label for="filedata">Select CSV File:</label><span style="color: red">*</span></div>
</td>
<td align="left">
<div class="column span-4"><input class="span-5 ui-input" type="file" id="fileData" name="fileData" size="45" /></div>
</td>
</tr>
<tr>
<td>
<div class="column span-2_5"><label for="uploadComment">Comment/Description:</label></div>
</td>
<td align="left">
<div class="column span-4"><input class="span-5 ui-input" type="text" maxlength="100" id="uploadComment" name="uploadComment" size="50" /></div>
</td>
</tr>
<tr>
<td valign="top"> </td>
<td colspan=2>
<div class="column " style="margin-top: 15px;">
<input type="submit" value="Upload File" "/>
</div>
<div class="column" style="margin-top: 15px;">
<input type="button" value="Reset" onclick="javascript:resetUploadFactoryForm();"/>
</div>
</td>
</tr>
</table>
</form:form>
控制器:
@Controller
public class NewFactoryUploadDownloadController {
private static final Log logger = LogFactory.getLog(NewFactoryUploadDownloadController.class);
@Resource
IInteropService interopService;
@Resource
FactoryUploadRepository repository;
@RequestMapping(value="/download.do")
public String downloadFactory(FactoryUploadForm form, Model model,HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter pw = null;
try{
logger.debug("+++++++++++++++++++++++++++++++++++++++");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment; filename="+ "Backup" +".csv");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-csheck=0, pre-check=0");
response.setHeader("Pragma", "public");
pw = response.getWriter();
// Get, Write and flush the data.
pw.println("Factory Id,FactoryLegalName,FactoryShortName,FactoryType");
List<Factory> factoryFromDB = Collections.emptyList();
factoryFromDB = Service.getfactories();
logger.info("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
for(Factory factory : factoryFromDB)
{
pw.println(String.format("%s,%s,%s",
factory.getId(),
formatString(factory.getName()),
factory.getShortName()));
}
pw.flush();
}catch (Exception ex){
logger.error("Error while downloading companies", ex);
model.addAttribute("failureMsg", "Error while downloading companies");
}
return null;
}
private String formatString(String str){
if (str.contains(",")){
return "\""+str + "\"";
}
return "\""+str + "\"";
}
}
型号:
@Entity
@Table(name = "FactoryConf", uniqueConstraints = {
@UniqueConstraint(columnNames = { "factoryId" } )
})
public class FactoryConf {
@Id
long factoryId;
@OneToOne
@JoinColumn(name = "factoryId", insertable = false, updatable = false)
Factory factory;
@ManyToOne(optional = false)
@JoinColumn(name = "factoryTypeId")
FactoryType factoryType;
public FactoryConf() {
super();
}
public FactoryConf(long factoryId, FactoryType factoryType) {
super();
this.factoryType = factoryType;
this.factoryId = factoryId;
}
public Factory getFactory() {
return factory;
}
public void setFactory(Factory factory) {
this.factory = factory;
}
public FactoryType getFactoryType() {
return factoryType;
}
public void setFactoryType(FactoryType factoryType) {
this.factoryType = factoryType;
}
public long getFactoryId() {
return factoryId;
}
public void setFactoryId(long factoryId) {
this.factoryId = factoryId;
}
public FactoryType getFactoryTypeByFactoryID(long factoryId){
return factoryType;
}
}