简体   繁体   English

Hibernate Spring MVC公式

[英]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. 您好,我正在尝试开发一个可以删除用户的页面,但是当我单击“提交”时,我遇到一个错误,例如Etat HTTP 400-客户端语法要求不正确。

Jsp file JSP文件

</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. 您期望有一个User和一个名称为id的参数,但是您发送了一个名称为idUser的参数。

Eliminate ModelAttribute and force de name of RequestParam: 消除ModelAttribute并强制命名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. 1.首先,您需要像这样向您的表单添加modelattribute:注意我如何使用spring表单。 You can use them by adding 您可以通过添加使用它们

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

before DOCTYPE html> 在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. 然后,您需要向属性“ id”添加隐藏的路径,以便当控制器收到请求时,您将知道需要删除或编辑哪个用户。

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). 注意:您将必须创建具有id属性的class = User :(以及您需要的所有其他用户)。 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. 您的deleteUser方法中的PS用户用户实际上与使用@modelAttribute批注创建的用户相同。

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. 提交表单时,所有内容都将保存到带有注释@modelAttribute的对象User中。 Hidden id field is crucial here. 隐藏的ID字段在这里至关重要。 When you have id, which is primary key you can just create method in repository (something like this) 当您拥有ID(这是主键)时,您可以在存储库中创建方法(类似这样)

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

Hope you find this post helpful. 希望这篇文章对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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