vendredi 17 juin 2016

Hibernate - Neither BindingResult nor plain target object for bean name 'venueType' available as request attribute

I'd like to ask a question about how Spring and Hibernate behave. I'm trying to build an object creation page, but after several tries, I'm coming to you asking for your help.

I have an object, Venue, which possesses a reference to another object, VenueType:

Venue.java

@Entity
@Table(name = "VENUE")
public class Venue extends CommonEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@NotEmpty
@Column(name = "NAME", nullable = false, length = 60)
private String name;

@ManyToOne(optional = false)
@JoinColumn(name = "VENUE_TYPE_ID")
private VenueType venueType;

@NotEmpty
@Column(name = "ADDRESS", nullable = false, length = 100)
private String address;

@NotEmpty
@Column(name = "TOWN", nullable = false, length = 60)
private String town; (...)  

VenueType.java

@Entity
@Table(name = "VENUE_TYPE")
public class VenueType extends CommonEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@NotEmpty
@Column(name = "NAME", nullable = false, length = 30)
private String name; (...)  

In my controller, I tried to do like other simple classes I have implemented:

VenueController.java

@Controller
@RequestMapping(value = "/venue")
public class VenueController {

@Autowired
private VenueService service;
@Autowired
private VenueTypeService venueTypeService;

@RequestMapping(method = RequestMethod.GET)
public String getAll(ModelMap model) {
    model.addAttribute("venues", service.findAll());
    return Venue.getViewFolder() + "/" + "listAll";
}

@RequestMapping(value = "/new", method = RequestMethod.GET)
public String loadCreateVenuePage(ModelMap model) {
    model.addAttribute("venueTypes", venueTypeService.findAll());
    model.addAttribute("venue", new Venue());
    return Venue.getViewFolder() + "/" + "create";
}

@RequestMapping(value = "/new", method = RequestMethod.POST)
public String createVenue(@ModelAttribute("venue") Venue venue) {
    service.save(venueType);
    return "redirect:/venueType";
}  

In my .jsp file, I have a very simple code, didn't even apply CSS to it:

venue/create.jsp

<body>
<h1>Creating Venue</h1>

<form:form action="./new" method="POST" commandName="venue">
    <form:label path="name">Name</form:label>
    <form:input path="name"/>
    <br/>

    <form:label path="address">Address</form:label>
    <form:input path="address"/>
    <br/>

    <form:label path="town">Town</form:label>
    <form:input path="town"/>
    <br/>

    <form:label path="postcode">Post Code</form:label>
    <form:input path="postcode"/>
    <br/>

    <form:label path="country">Country</form:label>
    <form:input path="country"/>
    <br/>

    <form:label path="venueType">Venue Type</form:label>
    <form:select path="venueType">
        <form:option value="" label="Select an option"/>
        <form:options items="${venueTypeList}" itemValue="name" itemLabel="name"/>
    </form:select>

    <br/>
    <input type="submit" value="Submit"/>
</form:form>

</body>  

When I try to access the '/new' address, then I get the exception "java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'venueType' available as request attribute". I have come across several SO questions, most were referring to the lack of a commandName / modelAttribute or the addition of a new object to the ModelMap, what I'm doing. If you can help me pinpointing the cause of the problem, I'll be very appreciated. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire