我正在尝试创建一个简单的页面,我可以在填写表单时添加/编辑员工到数据库(请注意mitarbeiter =德语员工 - 对不起)。问题是当我填写表格时没有在Mitarbeiter对象中设置。我究竟做错了什么?
以下是模型:
public class Mitarbeiter implements Serializable {
private static final long serialVersionUID = -1280037900360314186L;
private int personalNummer;
private String nachname;
private String vorname;
/**
* Default-Konstruktor
*/
public Mitarbeiter()
{
super();
}
/**
* Konstruktor
* @param personalNummer
* @param nachname
* @param vorname
*/
public Mitarbeiter(int personalNummer, String nachname, String vorname) {
super();
this.personalNummer = personalNummer;
this.nachname = nachname;
this.vorname = vorname;
}
public int getPersonalNummer() {
return personalNummer;
}
public String getNachname() {
return nachname;
}
public String getVorname() {
return vorname;
}
这是我的控制器:
@RequestMapping(value = { "/addMitarbeiter" }, method = RequestMethod.GET)
public ModelAndView addEditMitarbeiter(@ModelAttribute("mitarbeiter") Mitarbeiter mitarbeiter) {
logger.info("Generating addEditMitarbeiter view");
ModelAndView model = new ModelAndView();
model.setViewName("addEditMitarbeiter");
model.addObject("mitarbeiter", new Mitarbeiter());
return model;
}
@RequestMapping(value = { "/saveMitarbeiter/" }, method = RequestMethod.POST)
public ModelAndView saveMitarbeiter(@ModelAttribute("mitarbeiter") Mitarbeiter mitarbeiter) {
logger.info("Saving Mitarbeiter...");
try {
if (mitarbeiterDAO.getMitarbeiterByPersonalnummer(mitarbeiter.getPersonalNummer()) != null);
mitarbeiterDAO.updateMitarbeiter(mitarbeiter);
}
catch (EmptyResultDataAccessException e) {
logger.info("Neuen Mitarbeiter anlegen...");
mitarbeiterDAO.saveMitarbeiter(mitarbeiter);
}
return new ModelAndView("redirect:/searchEmployee");
}
@RequestMapping(value = { "/editMitarbeiter/{personalNummer}" })
public ModelAndView editEmployee(@PathVariable("personalNummer") int personalNummer) {
logger.info("Generating mitarbeiterEdit view");
ModelAndView model = new ModelAndView();
model.setViewName("addEditMitarbeiter");
Mitarbeiter mitarbeiter = mitarbeiterDAO.getMitarbeiterByPersonalnummer(personalNummer);
model.addObject("mitarbeiter", mitarbeiter);
return model;
}
这是一个表格的片段,应填写并保存员工或更改到数据库:
<form:form name="mitarbeiterForm" commandName="mitarbeiter" method='POST' action="${pageContext.request.contextPath}/saveMitarbeiter" >
<table>
<tr>
<td><form:label path="personalNummer">Personalnummer:</form:label></td>
<td><form:input type="text" path="personalNummer" /></td>
</tr>
<tr>
<td><form:label path="vorname">Vorname:</form:label></td>
<td><form:input type="text" path="vorname" /></td>
<td><form:label path="nachname">Nachname:</form:label></td>
<td><form:input type="text" path="nachname" /></td>
</tr>
如果有必要,这里还有pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>at.business</groupId>
<name>business_core</name>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.8</java-version>
<org.springframework-version>4.3.7.RELEASE</org.springframework-version>
<hibernate.version>4.3.11.Final</hibernate.version>
<spring.security.version>3.2.3.RELEASE</spring.security.version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
<mysql.connector.version>5.1.21</mysql.connector.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>
<!-- Spring Security JSP Taglib -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${spring.security.version}</version>
</dependency>
<!-- MySQL connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<!-- CGLIB is required to process @Configuration classes -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.2.2</version>
<scope>runtime</scope>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
<!-- <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat8-maven-plugin</artifactId>
<version>2.2</version> <configuration> <url>http://162.253.124.18</url> <server>UnixTomcatServer</server>
<path>/home/porttosu</path> </configuration> </plugin> -->
</plugins>
</build>
<artifactId>core</artifactId>
<packaging>war</packaging>
</project>
答案 0 :(得分:0)
删除
@ModelAttribute("mitarbeiter") Mitarbeiter mitarbeiter
来自
public ModelAndView addEditMitarbeiter(@ModelAttribute("mitarbeiter") Mitarbeiter mitarbeiter) {
应该只是
public ModelAndView addEditMitarbeiter(Model model) {
并做
model.addAttribute("mitarbeiter", new Mitarbeiter());
(模型的对象,而不是modelandview)
答案 1 :(得分:0)
一切都很好看。您在表单中使用 commandName ,这是旧方式读取请求参数值,使用 modelAttribute 而不是命令名称。
{{1}}