0

我使用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 ModelAndView customer() {
      return new ModelAndView("home", "command", new Customer());
   }

@RequestMapping(value = "/customer", method = RequestMethod.POST)
public String addCustomer(@ModelAttribute("customer") Customer customer,ModelMap model)
{
    model.addAttribute("custid",customer.getCustId());
    model.addAttribute("name", customer.getName());
    model.addAttribute("age", customer.getAge());

    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">
<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>${custid}</td>
</tr>
 <tr>
<td>Name</td>
<td>${name}</td>
</tr>
<tr>
<td>Age</td>
<td>${age}</td>
</tr>
</table> 
 </body>
</html>

数据被插入到数据库中,但没有显示出来。请帮我解决这个问题。谢谢。

4

0 回答 0