表单问题:使用Spring mvc进行一对多映射的选项

时间:2017-11-20 10:39:38

标签: java spring jsp spring-mvc

你好我是Spring的新手并且找不到错误=(我正在使用Spring MVC并希望为一对多的映射选择User bean for Client。当我尝试保存新客户端时,我遇到了错误。< / p>

  

HTTP状态400 - 错误请求

     

输入状态报告

     

描述服务器不能或不会处理请求   被认为是客户错误的东西(例如,畸形   请求语法,无效请求消息框架或欺骗性请求   路由)。

用户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 + "]";
    }

客户端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
@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>

如果我评论下一个代码的和平工作,那么

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

   

我尝试在互联网上搜索这个问题,但代码示例与我的相同。请帮我找错;)

我在Tomcat日志中有这个

  

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/2017年11月: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

1 个答案:

答案 0 :(得分:0)

我找到了答案。我需要将.id放在路径

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