我正在尝试使用spring用java创建一个web应用程序。
我们的想法是调用@get显示数据输入表单的模型,然后在@post中我想将它们插入到持久层(sqlserver)中。
问题是,我无法使传入@get的对象由表单填充,因此无法随后将数据插入数据库。我在@post中收到的对象的属性都是空的,我不明白为什么。
这是我的控制器:
package com.test.webapp.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.test.weapp.domain.Categorie;
import com.test.weapp.domain.File;
import com.test.weapp.domain.Regioni;
import com.test.webapp.repository.CategorieRepository;
import com.test.webapp.repository.FileRepository;
import com.test.webapp.repository.RegioniRepository;
@Controller
@RequestMapping("/")
public class IndexController
{
@Autowired
private CategorieRepository categorieRepository;
@Autowired
private RegioniRepository regioniRepository;
@Autowired
private FileRepository fileRepository;
@GetMapping(value="/index")
public String getWelcome(Model model)
{
File file = new File();
model.addAttribute("regioni", getRegioni());
model.addAttribute("categorie", getCategorie());
model.addAttribute("Fileee", file);
return "home";
}
@PostMapping(value="/index")
public String GestInsFile (@ModelAttribute("Fileee") File file)
{
fileRepository.InsFile(file);
return "home";
}
@ModelAttribute("regioni")
public List<Regioni> getRegioni()
{
List<Regioni> regioni = regioniRepository.SelRegioni();
return regioni;
}
@ModelAttribute("categorie")
public List<Categorie> getCategorie()
{
List<Categorie> categorie = categorieRepository.SelCategorie();
return categorie;
}
}
类文件:
package com.test.weapp.domain;
import java.io.Serializable;
import java.util.Date;
import org.springframework.web.multipart.MultipartFile;
public class File implements Serializable {
private static final long serialVersionUID = -316224596822077411L;
private int id;
private String titolo;
private String regione;
private String categoria;
private String descrizione;
private String note;
private MultipartFile file;
public File() {
}
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitolo() {
return titolo;
}
public void setTitolo(String titolo) {
this.titolo = titolo;
}
public String getRegione() {
return regione;
}
public void setRegione(String regione) {
this.regione = regione;
}
public String getCategoria() {
return categoria;
}
public void setCategoria(String categoria) {
this.categoria = categoria;
}
public String getDescrizione() {
return descrizione;
}
public void setDescrizione(String descrizione) {
this.descrizione = descrizione;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
类别
package com.test.weapp.domain;
import java.io.Serializable;
public class Categorie implements Serializable
{
private static final long serialVersionUID = 6904000811482985125L;
private int id;
private String categoria;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCategoria() {
return categoria;
}
public void setCategoria(String categoria) {
this.categoria = categoria;
}
public Categorie() {
}
}
regioni级
package com.test.weapp.domain;
import java.io.Serializable;
public class Regioni implements Serializable
{
private static final long serialVersionUID = 7083492150275199488L;
private int id;
private String regione;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRegione() {
return regione;
}
public void setRegione(String regione) {
this.regione = regione;
}
public Regioni() {
}
}
类filerepositoryimpl,它插入到表中
package com.test.webapp.repository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.test.weapp.domain.File;
@Repository
public class FileRepositoryImpl implements FileRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<File> SelFileByFilter(String id) {
return null;
}
@Override
public void InsFile(File file) {
String Sql = "insert into [dbo].[FILE] VALUES ('" + file.getTitolo() + "','" + file.getRegione() + "','"
+ file.getCategoria() + "','" + file.getDescrizione() + "','" + file.getNote()
+ "');";
jdbcTemplate.update(Sql);
}
@Override
public void DelFile(String id) {
}
}
这里是我的jsp,显示home.jsp编译表单
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!doctype html>
<html lang="it">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Altri CSS -->
<link href="<c:url value="/static/css/style.css" />" rel="stylesheet">
<title>Area Riservata</title>
</head>
<body>
<h1>Area Riservata</h1>
<p>Bim Italia Srl</p>
<main>
<form:form action ="index" method="POST" modelAttribute="Fileee"
enctype="multipart/form-data">
<table>
<CAPTION>Informazioni File</CAPTION>
<thead>
</thead>
<tfoot>
</tfoot>
<tbody>
<tr>
<td data-title='Provider Name'>File:</td>
<td><form:input path="file" type="file" class="form:input-large" /></td>
</tr>
<tr>
<td data-title='Provider Name'>Titolo:</td>
<td><form:input path="titolo" type="text" class="form-control" /> </td>
</tr>
<tr>
<td data-title='Provider Name'>Regione:</td>
<td><form:select path="regione" class="form-control">
<form:options items="${regioni}" itemValue="Id" itemLabel="Regione" />
</form:select></td>
</tr>
<tr>
<td data-title='Provider Name'>Categoria:</td>
<td><form:select path="categoria" class="form-control">
<form:options items="${categorie}" itemValue="Id" itemLabel="Categoria" />
</form:select></td>
</tr>
<tr>
<td data-title='Provider Name'>Descrizione:</td>
<td><form:textarea path="descrizione" rows="2" cols="33" autoResize="true" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;" />
</td>
</tr>
<tr>
<td data-title='Provider Name'>Note:</td>
<td><form:textarea path="note" rows="2" cols="33" autoResize="true" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;" />
</td>
</tr>
</tbody>
</table>
<input type="submit" id="btnAdd" />
</form:form>
</main>
<br />
<script src="/static/js/index.js"></script>
</body>
</html>
这里输入的数据都是空的!
请帮助我理解为什么我的表格不返回我填写的字段!
谢谢你的关注。
1条答案
按热度按时间2wnc66cl1#
在getwelcome controller@requestbody注解中添加文件