【问题标题】:java-spring-mvc Form values not getting printed on POSTjava-spring-mvc 表单值未在 POST 上打印
【发布时间】:2013-05-13 09:35:51
【问题描述】:

我是 spring mvc 的新手 我使用 SpringTemplateProject 创建了一个项目,并在其中使用了休眠。

我创建了一个表单并在发布时将值插入到数据库中,但问题是我无法在 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";
}
}

Home.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>

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>${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;
    }

}

【问题讨论】:

  • 数据库中插入的用户的值是否正确?

标签: java spring spring-mvc


【解决方案1】:

从 jsp 中的 ${custform.age}${custform.name}${custform.custid} 中删除 custform. 并保持 model.addAttribute 与控制器中的一样。您应该可以在 jsp 中使用 ${age} 从客户那里访问 age

控制器:

@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 保持原样。

【讨论】:

  • @hey 谢谢它的工作,但我无法看到我的 custid dat 值没有显示出来。
  • &lt;form:label path="custid"&gt;Id:&lt;/form:label&gt; &lt;form:input path="custid"/&gt;更改为&lt;form:label path="custId"&gt;Id:&lt;/form:label&gt; &lt;form:input path="custId"/&gt;,如果您使用我提到的第二种方法,请使用${custform.custId}访问custId。 Java 中的所有内容都区分大小写;)
  • 嘿,我将所有 custid 更改为 custId,但在我调试后,我仍然看到它的名称为“custid”,但它仍然为零..如何更新它??
  • 您使用的是哪种方法?我认为您将需要清除缓存。如果您使用的是 Eclipse,请清理并构建项目,它应该可以工作。
  • 嘿,我做了清理和构建,现在所有值都从 custid 更新为 custId..但它的值仍然为 0。
【解决方案2】:

试试

@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) 而无需创建新客户。

【讨论】:

  • 嘿,谢谢它的工作,但现在我没有得到 custid 值..我得到 custid 为 0
猜你喜欢
  • 1970-01-01
  • 2019-03-05
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多