0

我是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;
    }

}
4

2 回答 2

0

custform.${custform.age},${custform.name}和jsp 中删除${custform.custid}并保持你的 model.addAttribute 与控制器中的一样。您应该能够age从使用${age}jsp 的客户访问。

控制器:

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

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>

或者

只需model.addAttribute("custform",customer);在控制器中执行并保留其他所有内容。

控制器

@RequestMapping(value = "/customer", method = RequestMethod.POST)
   public String addCustomer(@ModelAttribute("customer") Customer customer,Model model)
{
    service.addCustomer(customer);
    model.addAttribute("custform",customer);
    return "customer/customer";
}

JSP 保持与所讨论的相同。

于 2013-05-13T10:44:02.213 回答
0

尝试

@RequestMapping(value = "/customer", method = RequestMethod.POST)
public ModelAndView addCustomer(@ModelAttribute("customer") Customer customer)
{
    service.addCustomer(customer);

    Model model = new ModelMap();
    model.addAttribute("custid",customer.getCustId());
    model.addAttribute("name", customer.getName());
    model.addAttribute("age", customer.getAge());


    return new ModelAndView("customer/customer", model);
}    

顺便说一句:更好的方法是在发布后将(http 代码 303)重定向到客户展示页面。然后用户可以在每次刷新时重新加载页面 (F5) 而无需创建新客户。

于 2013-05-13T10:53:47.173 回答