Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java spring mvc表单值未在POST上打印_Java_Spring_Spring Mvc - Fatal编程技术网

java spring mvc表单值未在POST上打印

java spring mvc表单值未在POST上打印,java,spring,spring-mvc,Java,Spring,Spring Mvc,我是spring mvc的新手 我使用SpringTemplateProject创建了一个项目,并在其中使用了hibernate 我创建了一个表单,并在post上将这些值插入到数据库中,但问题是我无法在post上显示这些值 这是代码 //控制器 package com.projects.data; import java.util.Locale; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sprin

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

我创建了一个表单,并在post上将这些值插入到数据库中,但问题是我无法在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>
但这似乎也不起作用

模范班

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

}

从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";
}
<%@ 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>
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";
}
<%@ 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>
JSP 保持与问题相同。

试试看

@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 dat值没有显示。将
Id:
更改为
Id:
如果使用我提到的第二种方法,请使用
${custform.custid}
访问
custId
。java中的所有内容都区分大小写;)嘿,我把所有的custid都改成了custid,但在我调试之后,我仍然看到它的名字是'custid',它仍然是零..我如何更新它?你使用的是哪种方法?我想你需要清除缓存。如果您使用的是Eclipse,请清理并构建该项目,它应该可以工作。嘿,我现在清理并构建了所有值,从custid更新到custid。但是它仍然接受值0。嘿,谢谢它工作了,但是现在我没有得到custid值。我得到的custid为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);
}