Bonjour
Je travaille sur un projet web en J2EE, avec un serveur Tomcat.
J'utilise Spring MVC, je ne m'en sors pas avec les formulaires.
j' ai deux tables (customer et address) je veux inserer de données en utilisant un seul formulaire jsp.
merci pour votre aide
voici le controlleur
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 
 
 
import javax.validation.Valid;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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 org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
 
import com.bbstore.model.Address;
import com.bbstore.model.Customer;
import com.bbstore.repository.AddressRepository;
import com.bbstore.repository.CustomerRepository;
@Controller
@RequestMapping("/customer")
public class CustomerEditController {
 
    @Autowired CustomerRepository  customerRepository;
    @Autowired AddressRepository   addressRepository;
 
    private ModelAndView prepareModelAndView(Customer customer ) {
        ModelAndView mv = new ModelAndView("editcustomer");
        mv.addObject("id", customer.getId());
        mv.addObject("customer", customer); 
        return mv; 
    }
    @RequestMapping("/create")
    public ModelAndView customerCreate() {
         return prepareModelAndView(new Customer());     
    }
@RequestMapping(value="/customeronsubmit",method=RequestMethod.POST)
public ModelAndView CustomerAddSubmit( @Valid  @ModelAttribute Customer customer,
                                       String country,String city,String state,
                                       BindingResult bindingResult){
    Address address = new Address();
 
    if (bindingResult.hasErrors()){
        return new ModelAndView ("editcustomer", "customer", customer);
    }
 
    if(customer.getId()!=null) {
        address.setCountry(country);
        address.setCity(city);
        address.setState(state);
        customerRepository.merge(customer);
 
    } else {
        address.setCountry(country);
        address.setCity(city);
        address.setState(state);
        customerRepository.persist(customer);
 
    }
 
 
    return new ModelAndView("redirect:/customerlist/");
}
voici le formulaire jsp addcustomer
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>
 
 
<!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">
 
</head>
<body>
	<c:choose>
			<c:when test="${customer.id==null}">
				Création 
			</c:when>
			<c:otherwise>
				<title>Edit Customer:${customer.firstName}</title>
			</c:otherwise>
		</c:choose>
	<form:form modelAttribute="customer" method="post" action='<%=response.encodeUrl("/customer/customeronsubmit")%>'>
 
		<label>FirstName</label>   <form:input path="firstName" />       <form:errors path="firstName" /><br>
		<label>LastName</label>    <form:input path="lastName" />        <form:errors path="lastName" /><br>
		<label>Phone</label>        <form:input path="phone" />               <form:errors path="phone" /><br>
		<label>Email</label>       <form:input path="email" />              <form:errors path="phone" /><br>
		<label>Country</label>   <form:input path="address.country" />           <form:errors path="address.country" /><br>
		<label>City</label>    <form:input path="address.city" />                <form:errors path="address.city" /><br>
		<label>State</label>   <form:input path="address.state" />               <form:errors path="address.state" /><br>
		<label>Street</label>    <form:input path="address.street" />             <form:errors path="address.street" /><br>
		<label>Zipcode</label>    <form:input path="address.zipcode" />            <form:errors path="address.zipcode" /><br>
 
		<input type="submit"  value="<c:choose><c:when test="${customer.id==null}">Add Customer</c:when><c:otherwise>Edit Customer</c:otherwise></c:choose>"/>
	</form:form>
 
	<a href="/customerlist">home page</a><br/>
 
</body>
</html>
voici classe customer
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 
 
import java.io.Serializable;
 
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
 
@Entity
public class Customer implements Serializable{
	/**
         * 
         */
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	@Column()
	private String firstName;
	@Column()
	private String lastName;
	@Column()
	private String email;
	@Column()
	private String phone;
 
    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name ="creditcard_id")
    private   CreditCard creditCard ;
 
 
	@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinColumn(name ="address_id")
	private Address address;
 
 
	public Customer() {
		super();
		// TODO Auto-generated constructor stub
	}
 
    public Customer(String firstName, String lastName, String email,
            String phone, CreditCard creditCard, Address address) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.phone = phone;
        this.creditCard = creditCard;
        this.address = address;
    }
 
    public CreditCard getCreditCard() {
        return creditCard;
    }
 
    public void setCreditCard(CreditCard creditCard) {
        this.creditCard = creditCard;
    }
 
    public String getFirstName() {
		return firstName;
	}
 
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
 
	public String getLastName() {
		return lastName;
	}
 
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
 
	public String getEmail() {
		return email;
	}
 
	public void setEmail(String email) {
		this.email = email;
	}
 
	public String getPhone() {
		return phone;
	}
 
	public void setPhone(String phone) {
		this.phone = phone;
	}
 
	public Address getAddress() {
		return address;
	}
 
	public void setAddress(Address address) {
		this.address = address;
	}
 
 
 
    public Long getId() {
        return id;
    }
 
 
 
 
 
}
voici classe addresse
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 
package com.bbstore.model;
 
import java.io.Serializable;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
 
@Entity
public class Address  implements Serializable{
	/**
         * 
         */
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
 
	@Column()
	private String street;
	@Column()
	private String city;
	@Column()
	private String state;
	@Column()
	private String zipcode;
	@Column()
	private String country;
 
	public Address() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Address(String street, String city, String state, String zipcode,
			String country) {
		super();
		this.street = street;
		this.city = city;
		this.state = state;
		this.zipcode = zipcode;
		this.country = country;
	}
 
	public Long getId() {
        return id;
    }
    public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getZipcode() {
		return zipcode;
	}
	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
 
 
 
}