java - 表单对象的 id 被设置为 0

标签 java forms spring

我不知道为什么这个 Controller 方法不起作用,但也许有人以前见过类似的东西。我的 Web 表单使用伪装的 PUT 方法提供所有 Property 对象的变量(其中之一是 id)。它的注释如下:

package uk.co.nicshouse.jester.domain;

import java.io.Serializable;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;


@Entity
public class Property implements Serializable
{
    @Id
    @GeneratedValue
    private long id;
    private String name;
    private String description;

    @Embedded
    private Address address;

    @Override
    public boolean equals(Object obj)
    {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Property other = (Property) obj;
        if (this.id != other.id) {
            return false;
        }
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if ((this.description == null) ? (other.description != null) : !this.description.equals(other.description)) {
            return false;
        }
        if (this.address != other.address && (this.address == null || !this.address.equals(other.address))) {
            return false;
        }
        return true;
    }



    @Override
    public int hashCode()
    {
        int hash = 3;
        hash = 29 * hash + (int)this.id;
        hash = 29 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 29 * hash + (this.description != null ? this.description.hashCode() : 0);
        hash = 29 * hash + (this.address != null ? this.address.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString()
    {
        return "Property{" + "id=" + id + ", name=" + name + ", description=" + description + ", address=" + address + '}';
    }



    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getDescription()
    {
        return description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    public Address getAddress()
    {
        return address;
    }

    public void setAddress(Address address)
    {
        this.address = address;
    }

}

Controller 看起来像这样:

package uk.co.nicshouse.jester.mvc;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.portlet.ModelAndView;
import uk.co.nicshouse.jester.domain.Property;
import uk.co.nicshouse.jester.service.PropertyService;


@Controller
@RequestMapping("/property")
public class PropertyController
{
    @Autowired
    private PropertyService propertyService;


    /*
     * Methods that return views
     */

    @RequestMapping(method=RequestMethod.GET, produces="text/html")
    public String listProperty(Model model, HttpServletRequest request)
    {
        model.addAttribute( propertyService.getAll() );
        model.addAttribute("hostname", request.getServerName());
        return "property/list";
    }

    @RequestMapping(value="/{id}", method=RequestMethod.GET, produces="text/html")
    public String viewProperty(@PathVariable long id, Model model)
    {
        model.addAttribute( propertyService.getById(id) );
        return "property/view";
    }

    @RequestMapping(value="/{id}/edit", method=RequestMethod.GET, produces="text/html")
    public String editProperty(@PathVariable long id, Model model)
    {
        model.addAttribute( propertyService.getById(id) );
        return "property/edit";
    }

    @RequestMapping(value="/new", method=RequestMethod.GET, produces="text/html")
    public String newProperty(Model model)
    {
        model.addAttribute( new Property() );
        return "property/edit";
    }

    @RequestMapping(value="/new", method=RequestMethod.POST)
    public String createProperty(@Valid Property property, BindingResult bindingResult)
    {
        getPropertyService().create(property);
        return "redirect:/property/" + property.getId();
    }

    @RequestMapping(value="/**", method=RequestMethod.PUT)
    public String updateProperty(@RequestParam("id") int id, Property property)
    {
        System.out.println("Supplied id: " + id);
        System.out.println("Supplied property: " + property);
        getPropertyService().update(property);

        return "redirect:/property/" + property.getId();
    }





    public PropertyService getPropertyService()
    {
        return propertyService;
    }

    public void setPropertyService(PropertyService propertyService)
    {
        this.propertyService = propertyService;
    }
}

jSTL 生成的表单如下所示:

<form id="property" action="/jester/property/1/edit" method="post" enctype="multipart/form-data"><input type="hidden" name="_method" value="PUT"/>
<input id="id" name="id" type="hidden" value="3"/>
<div>
    <label for="name">Name</label>
    <input id="name" name="name" type="text" value="Test Name" size="15"/><br/>

</div>
<div>
    <label for="description">Description</label>
    <textarea id="description" name="description" size="15">Test Description Here</textarea><br/>

</div>
<div>
    <label for="address.street">Street</label>
    <input id="address.street" name="address.street" type="text" value="Test Street" size="15"/><br/>

</div>
<div>
    <label for="address.borough">Borough</label>
    <input id="address.borough" name="address.borough" type="text" value="" size="15"/><br/>

</div>
<div>
    <label for="address.town">Town</label>
    <input id="address.town" name="address.town" type="text" value="Test Town" size="15"/><br/>

</div>
<div>
    <label for="address.county">County</label>
    <input id="address.county" name="address.county" type="text" value="Test County" size="15"/><br/>

</div>
<div>
    <label for="address.postcode">Postcode</label>
    <input id="address.postcode" name="address.postcode" type="text" value="TT11 1TT" size="15"/><br/>

</div>
<div>
    <label for="address.country">Country</label>
    <input id="address.country" name="address.country" type="text" value="UK" size="15"/><br/>

</div>
<div>
    <input type="submit" value="Save" />
    <input type="reset" />
</div>

奇怪的是,id==3(来自HTML表单的正确值),却是property.id==0!属性的其他变量设置正确。有什么想法为什么会发生这种情况吗?

最佳答案

弄清楚了,这是我的一个错误。 Property.id 的类型很长,但 Property.setId 接受 int。这似乎在某种程度上搞砸了 Spring!不过还是谢谢你的努力!网络功能虚拟化

关于java - 表单对象的 id 被设置为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13214105/

相关文章:

java - Spring beans - 验证是否正确初始化

java - 在 Wildfly 中实例化 ClientBuilder 期间的 ClassCastException

java - (无)参数构造函数的最佳方法 - ResponseBody - Spring

java - 如何检查 String 是否仅包含 Java 中的 Date 格式?

java - 如果指定了参数,则从 Maven 运行 java 类

html - "GO"移动设备文本区域上的按钮

reactjs - 多选轮廓 varant 属性不起作用

java - 通用通配符类型不应在返回参数中使用

java - 同时使用两个路由器配置的 Akka 路由

delphi - 可以在delphi应用程序中创建多个MDI窗体吗?