简体   繁体   中英

Problems with form:options to using Spring mvc to one to many mapping

Hello I'm new with Spring and cannot find the mistake =( I'm using Spring MVC and want to select User bean for Client with one to many mapping. When I trying to save new Client I get a mistake.

HTTP Status 400 – Bad Request

Type Status Report

Description The server cannot or will not process the request due to something that is perceived to be a client error (eg, malformed request syntax, invalid request message framing, or deceptive request routing).

User Bean

    @Entity
@Table(name="user")
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="surname")
    private String surname;

    @Column(name="name")
    private String name;

    @Column(name="second_name")
    private String secondName;

    @Column(name="phone_number")
    private String phoneNumber;

    @Column(name="type")
    @Enumerated(EnumType.STRING)
    private UserType type;

    @OneToMany(mappedBy="user",
            fetch = FetchType.LAZY
        // , cascade= {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH}
    )
    private List<Client> clients;

    public User() {
    }

    public User(String surname, String name, String secondName, String phoneNumber, UserType type) {
        this.surname = surname;
        this.name = name;
        this.secondName = secondName;
        this.phoneNumber = phoneNumber;
        this.type = type;
    }

    public int getId() {
        return id;
    }

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

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getName() {
        return name;
    }

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

    public String getSecondName() {
        return secondName;
    }

    public void setSecondName(String secondName) {
        this.secondName = secondName;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public UserType getType() {
        return type;
    }

    public void setType(UserType type) {
        this.type = type;
    }

    public List<Client> getClients() {
        return clients;
    }

    public void setClients(List<Client> clients) {
        this.clients = clients;
    }

    public void add(Client tempClient) {
        if(clients == null) {
            clients = new ArrayList<>();
        }
        clients.add(tempClient);
        tempClient.setUser(this);
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", surname=" + surname + ", name=" + name + ", secondName=" + secondName
                + ", phoneNumber=" + phoneNumber + ", type=" + type + "]";
    }

Client Bean

@Entity
@Table(name="client")
public class Client {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;

@Column(name="name")
private String name;

@Column(name="turnover")
private long turnover;

@ManyToMany(
        //fetch = FetchType.LAZY, 
        //cascade= {CascadeType.PERSIST, CascadeType.MERGE, 
            //  CascadeType.DETACH, CascadeType.REFRESH}
        )
@JoinTable(
        name="activity_client",
        joinColumns = @JoinColumn(name="client_id"),
        inverseJoinColumns=@JoinColumn(name="activity_id"))
private List<Activity> activities;

@ManyToOne(
        //cascade= {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH}
        )
@JoinColumn(name="user_id")
private User user;

@Column(name="status")
@Enumerated(EnumType.STRING)
private ClientStatus status;

@OneToMany(mappedBy="client"
    //  , cascade= {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH}
        )
private List<ContactPerson> contactPersons;

@OneToMany(mappedBy="client"
        //  , cascade= {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH}
            )
    private List<Action> actions;

public Client() {
}

public Client(String name, long turnover, ClientStatus status) {
    this.name = name;
    this.turnover = turnover;
    this.status = status;
}

public int getId() {
    return id;
}

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

public String getName() {
    return name;
}

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

public long getTurnover() {
    return turnover;
}

public void setTurnover(long turnover) {
    this.turnover = turnover;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public ClientStatus getStatus() {
    return status;
}

public void setStatus(ClientStatus status) {
    this.status = status;
}

public List<ContactPerson> getContactPersons() {
    return contactPersons;
}

public void setContactPersons(List<ContactPerson> contactPersons) {
    this.contactPersons = contactPersons;
}

public List<Action> getActions() {
    return actions;
}

public void setActions(List<Action> actions) {
    this.actions = actions;
}

public void add(ContactPerson tempContactPerson) {
    if(contactPersons == null) {
        contactPersons = new ArrayList<>();
    }
    contactPersons.add(tempContactPerson);
    tempContactPerson.setClient(this);
}

public void add(Action tempAction) {
    if(actions == null) {
        actions = new ArrayList<>();
    }
    actions.add(tempAction);
    tempAction.setClient(this);
}

public void add(Activity tempActivity) {
    if(activities==null) {
        activities = new ArrayList<>();
    }

    activities.add(tempActivity);
}

public List<Activity> getActivities() {
    return activities;
}

public void setActivities(List<Activity> activities) {
    this.activities = activities;
}

@Override
public String toString() {
    return "Client [id=" + id + ", name=" + name + ", turnover=" + turnover + ", status=" + status + "]";
}

}

Controller for Client

@Controller
@RequestMapping("/client")

public class ClientController {
@Autowired
private ClientService clientService;

@Autowired
private UserService userService;

@GetMapping("/list")
public String listClients(Model theModel) {

    List<Client> theClients = clientService.getClients();
    theModel.addAttribute("clients", theClients);

    return "client/list-clients";
}

@GetMapping("/showFormForAdd")
public String showFormForAdd(Model theModel) {
    Client theClient = new Client();
    theModel.addAttribute("client", theClient);
    theModel.addAttribute("clientStatus", ClientStatus.values());
    List<User> users = userService.getUsers();
    theModel.addAttribute("clientUser", users);
    return "client/client-form";
}

@PostMapping("/saveClient")
public String saveClient(@ModelAttribute("client") Client theClient) {
    clientService.saveClient(theClient);
    return "redirect:/client/list";
}

@GetMapping("/showFormForUpdate")
public String showFormForUpdate(@RequestParam("clientId") int theId, Model theModel) {

    Client theClient = clientService.getClient(theId);

    theModel.addAttribute("client", theClient);
    theModel.addAttribute("clientType", ClientStatus.values());

    return "client/client-form";

}

@GetMapping("/delete")
public String deleteClient(@RequestParam("ClientId") int theId) {

    clientService.deleteClient(theId);

    return "redirect:/client/list";
}

}

JSP for Client

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html>
<html>

<head>
    <title> Save Client</title>


    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/style.css"/>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/add-target-style.css"/>

</head>
<body>
    <div id="wrapper">
        <div id="header">
            <h2>CRM - Customer Relationship Manager</h2>
        </div>
    </div>
    <div id="container">
        <h3>Save User</h3>

        <form:form action="saveClient" modelAttribute="client" method="POST">

            <form:hidden path="id"/>
            <table>
                <tbody>
                    <tr>
                        <td><label> Name:</label></td>
                        <td><form:input path="name"/></td>
                    </tr>
                    <tr>
                        <td><label> Turnover:</label></td>
                        <td><form:input path="turnover"/></td>
                    </tr>
                    <tr>
                        <td><label> Manager:</label></td>
                        <td>
                            <form:select path="user">                       
                                <form:options items="${clientUser}"  itemValue="id" itemLabel="surname"   />
                            </form:select>
                        </td>                   </tr>
                    <tr>
                        <td><label> Status:</label></td>
                        <td>
                            <form:select path="status">                     
                                <form:options items="${clientStatus}"  itemLabel="status" />
                            </form:select>
                        </td>
                    </tr>
                    <tr>
                        <td><label> </label></td>
                        <td><input type="submit" value="Save" class="save"/></td>
                    </tr>
                </tbody>

            </table>
        </form:form>

        <div style="clear; both;"></div>
        <p>
            <a href="${pageContext.request.contextPath}/user/list">Back to List</a>
        </p>
    </div>
</body>
</html>

If I comment next peace of code its work OK

<tr>
<td><label> Manager:</label></td>
<td>
    <form:select path="user">                       
           <form:options items="${clientUser}"  itemValue="id" 
            itemLabel="surname"   />
    </form:select>
/td>                    

I try to search this question in internet but example of code looks like the same as my. Please help me to find the mistake ;)

This I have in Tomcat logs

127.0.0.1 - - [20/Nov/2017:12:24:56 +0200] "GET / HTTP/1.1" 404 1073 0:0:0:0:0:0:0:1 - - [20/Nov/2017:12:25:05 +0200] "GET /crmdemo/ HTTP/1.1" 302 - 0:0:0:0:0:0:0:1 - - [20/Nov/2017:12:25:12 +0200] "GET /crmdemo/target/list HTTP/1.1" 200 944 0:0:0:0:0:0:0:1 - - [20/Nov/2017:12:25:28 +0200] "GET /crmdemo/client/list HTTP/1.1" 200 652 0:0:0:0:0:0:0:1 - - [20/Nov/2017:12:25:30 +0200] "GET /crmdemo/client/showFormForAdd HTTP/1.1" 200 1726 0:0:0:0:0:0:0:1 - - [20/Nov/2017:12:25:30 +0200] "GET /crmdemo/resources/css/add-target-style.css HTTP/1.1" 304 - 0:0:0:0:0:0:0:1 - - [20/Nov/2017:12:25:38 +0200] "POST /crmdemo/client/saveClient HTTP/1.1" 400 1130

I find an answer. I need to put .id in path

<td>
    <form:select path="user.id">                       
           <form:options items="${clientUser}"  itemValue="id" 
            itemLabel="surname"   />
    </form:select>
</td>  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM