我是spring mvc的新手,我使用SpringTemplateProject创建了一个项目并在其中使用了hibernate。
我创建了一个表单并在发布时将值插入到数据库中,但问题是我无法在 POST 上显示这些值。
这是代码。
//控制器
package com.projects.data;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.projects.data.*;
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@Autowired
ServiceImpl service;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String customer(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
return "home";
}
@RequestMapping(value = "/customer", method = RequestMethod.POST)
public String addCustomer(@ModelAttribute("customer") Customer customer,Model model)
{
service.addCustomer(customer);
return "customer/customer";
}
}
主页.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<spring:url var="customer" value="/customer"/>
<form:form action="${customer}" method="post" modelAttribute="customer" commandName="custform">
<form:label path="custid">Id:</form:label>
<form:input path="custid"/> <br>
<form:label path="name">Name:</form:label>
<form:input path="name"/> <br>
<form:label path="age">Age:</form:label>
<form:input path="age"/> <br>
<input type="submit" value="Save"/>
</form:form>
</html>
客户.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Submitted Information</title>
</head>
<body>
<h2>Submitted Information</h2>
<table>
<tr>
<td>Customer id</td>
<td>${custform.custid}</td>
</tr>
<tr>
<td>Name</td>
<td>${custform.name}</td>
</tr>
<tr>
<td>Age</td>
<td>${custform.age}</td>
</tr>
</table>
</body>
</html>
数据正在插入数据库中,但没有显示。请帮我解决这个问题。
我尝试使用@modelAttribute
model.addAttribute("custid",customer.getCustId());
model.addAttribute("name", customer.getName());
model.addAttribute("age", customer.getAge());
但这似乎也不起作用。
模型类
package com.projects.data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="customer")
public class Customer {
@Id
@Column(name="CUST_ID")
int custId;
@Column(name="NAME")
String name;
@Column(name="AGE")
int age;
public Customer(int custId,String name,int age)
{
this.custId=custId;
this.name=name;
this.age=age;
}
//getter and setter methods
public Customer() {
// TODO Auto-generated constructor stub
}
public int getCustId() {
return custId;
}
public void setCustId(int custId) {
this.custId = custId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}