简体   繁体   中英

Hibernate spring mvc formulaire

Hello I'm tryin to developpe a page that can delete users but when I click on submit I have an error Etat HTTP 400 - La requête envoyée par le client était syntaxiquement incorrecte.

Jsp file

</div>
<form method="POST" action="Users">
User ID
<input type="text" name="idUser" /><br><br>
<input type="submit" name="Supprimer" value="Supprimer"/>
</form>

Controller

@RequestMapping(value = "/Users")
public String goUsers(Model model)
{

    model.addAttribute("AllUsers", UserS.getAllUsers());
    return "Users";
}
@RequestMapping(value = "/Users", method = RequestMethod.POST)
public String goUsers(@ModelAttribute User user,BindingResult result,@RequestParam int id, Map<String, Object> model) 
{
    UserS.deleteUser(id);
    return "Users"; 
}

thank you

Your controller wrong. You expect oen User and one param with name id but you send one param with name idUser.

Eliminate ModelAttribute and force de name of RequestParam:

@RequestMapping(value = "/Users", method = RequestMethod.POST)
public String goUsers(BindingResult result,@RequestParam(name="idUser") int id, Map<String, Object> model) 
{
    UserS.deleteUser(id);
    return "Users"; 
}

1.first you need to add modelattribute to your form like this : Notice how i am using spring forms. You can use them by adding

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

before DOCTYPE html>

  1. Then you need to add hidden path to attribute "id" so when you controller gets the request you will know which user you will need to delete or edit.

This is example form :

`<form:form method="POST" modelAttribute="User" action="Users"> 
<form:hidden path="id"/>
<div class="form-group">
        <label for="usernameId">Username</label>
        <form:input path="username" id="usernameId" class="form-control" />
        <form:errors path="username" style="color:red;"></form:errors>
</div>
<div class="form-group">
        <label for="fullNameId">Full Name</label>
        <form:input path="firstLastName" id="firstLastName" class="form-control"/>
        <form:errors path="firstLastName" style="color:red;"></form:errors>
</div>
<div class="form-group">
        <label for="passwordId">Password</label>
        <form:password path="password" id="passwordId" class="form-control"/>
        <form:errors path="password" style="color:red;"></form:errors>
</div>
<div class="form-group">
        <label for="emailId">Email</label>
        <form:input path="email" id="emailId" class="form-control"/>
        <form:errors path="email" style="color:red;"></form:errors>
</div>
<input type="submit" class="btn btn-default" value="Register"/>
</form:form>`
  1. finally you will add to your controller class.

    @ModelAttribute("User") public User getUser(){ return new User(); }

  2. Then you will need to adjust your controller like this :

    @RequestMapping(value="/Users", method=RequestMethod.POST) public String deleteUser(User user){ getRegisterService().deleteUser(user.getId()); return "home"; }

Note : You will have to create class = User : with id attribute(and all others you need). You will also need to create a method for deleting user in your service and repository layer.

PS User user in your deleteUser method is actually the same user you created with @modelAttribute annotation.

If you have any additional questions feel free to ask!

I have given you almost exact form i use for register/editing or deleting Users. When you submit form, everything will be saved into object User with annotation @modelAttribute. Hidden id field is crucial here. When you have id, which is primary key you can just create method in repository (something like this)

public void deleteUser(UserJPA userJPA){
    getEntityManager().remove(UserJPA);
}

Hope you find this post helpful.

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