我迷失了为什么这种控制器方法不起作用的想法,但也许有人之前见过这样的事情。我的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;
}
}
控制器如下所示:
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
!属性的其他变量设置正确。任何想法为什么会发生这种情况?
答案 0 :(得分:0)
想出这个,这是我的一个错误。 Property.id的类型很长,但Property.setId接受了一个int。这似乎在某种程度上搞砸了Spring!谢谢你的努力! NFV