Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring MVC用户数据更新问题_Java_Spring_Spring Mvc_Updating - Fatal编程技术网

Java Spring MVC用户数据更新问题

Java Spring MVC用户数据更新问题,java,spring,spring-mvc,updating,Java,Spring,Spring Mvc,Updating,我对SpringMVC应用程序中的用户数据更新有问题。 因此,我有一个user,我有一个表单,里面填充了来自JSP的数据。现在,来自表单的数据用null覆盖用户数据的所有字段,但在jsp中输入的数据除外。 在另一种情况下–用户的数据覆盖表单的数据。 请帮我把它做好。我试过很多变体,但都不管用 @RequestMapping(value = "/edit", method = RequestMethod.GET) public ModelAndView updateView(@ModelAttri

我对SpringMVC应用程序中的用户数据更新有问题。 因此,我有一个user,我有一个表单,里面填充了来自JSP的数据。现在,来自表单的数据null覆盖用户数据的所有字段,但在jsp中输入的数据除外。 在另一种情况下–用户的数据覆盖表单的数据。 请帮我把它做好。我试过很多变体,但都不管用

@RequestMapping(value = "/edit", method = RequestMethod.GET)
public ModelAndView updateView(@ModelAttribute(value = "updateForm")
                                 HttpSession session) {
    User user = (User) session.getAttribute("user");
    UserForm updateForm = new UserForm();
    updateForm.setUser(user);
    return new ModelAndView("profileupdate", "updateForm", updateForm);
}

@RequestMapping(method = RequestMethod.POST)
    public String updateUserProcess(@ModelAttribute(value = "updateForm")
                                    UserForm updateForm,
                                    BindingResult result, Model model,
                                    HttpSession session) {
        User user = (User) session.getAttribute("user");
        model.addAttribute("updateForm", updateForm);

        if (result.hasErrors()) {
            return "profileupdate";
        }

        if (!updatingUser(updateForm, model, user))
            model.addAttribute("errorMsg", "Login or Email is already in use!");
            return "profileupdate";
        }

        return "updated";
    }

    private boolean updatingUser(UserForm updateForm, Model model, User user) {

        fillForm(updateForm, user);
        user = updateForm.getUser();

        //Another case 
        //user = updateForm.getUser();           
        //fillForm(updateForm, user);

        return userService.updateUser(user);
    }
      private void fillForm(UserForm updateForm, User user) {        
      updateForm.setUserId(user.getUserId());          
      updateForm.setLogin(user.getLogin());          
      updateForm.setPassword(user.getPassword());         
      updateForm.setEmail(user.getEmail());  
    }
}
**用户窗体类**

public class UserForm {
    private Integer userId;
    private String login;
    private String name;
    private String password;
    private String email;

    public UserForm() {
    }

    public User getUser() {
        User user = new User();
        user.setUserId(userId);
        user.setLogin(login);
       user.setPassword(password);
        user.setName(name);
        user.setEmail(email);        
        return user;
    }
    public void setUser(User user) {
        this.userId = user.getUserId();
        this.login = user.getLogin();
        this.password = user.getPassword();
        this.name = user.getName();
        this.email = user.getEmail();
…………………………. 
getters and setters
    }
这是我的刀和服务

@Override
public boolean updateUser(User user) {
    return userDao.updateUser(user);
}  

@Override
@Transactional
public boolean updateUser(User user) {
    if (isUserExists(user)) {
        return false;
    }
    currentSession().update(user);
    return true;
}
Updade.jsp

<sf:form name="login"
     method="POST"
     action="${app}/edit"
     modelAttribute="updateForm"
     enctype="application/x-www-form-urlencoded">

          <label for="login">Login:</label><br>
          <input name="login" id="login" type="text" value=""/> <br>
          <sf:errors path="login" cssClass="error"/><br>

          <br><label for="password">Password:</label>
          <br><input name="password" id="password" type="password" value=""/> 
          <br>
          <sf:errors path="password" cssClass="error"/><br>
          <br> <input type="submit" name="submit" value="Update"/>
  </sf:form>

登录:



密码:




是否检查了user.getUserID、user.getLogin()、user.getPassword()和user.getEmail()的值 在下面的代码段中?它是null还是您在模型对象用户中收到的数据

      updateForm.setUserId(user.getUserId());          
      updateForm.setLogin(user.getLogin());          
      updateForm.setPassword(user.getPassword());         
      updateForm.setEmail(user.getEmail());  

请发布userService.updateUser(user)的代码,以便我们了解更多信息。

您是否检查了user.getUserID、user.getLogin()、user.getPassword()、user.getEmail()的值 在下面的代码段中?它是null还是您在模型对象用户中收到的数据

      updateForm.setUserId(user.getUserId());          
      updateForm.setLogin(user.getLogin());          
      updateForm.setPassword(user.getPassword());         
      updateForm.setEmail(user.getEmail());  

请发布userService.updateUser(用户)的代码,以便我们了解更多信息。

spring或hibernate很难猜测哪些值为null,因为用户希望它们为null,而哪些值为null,因为不必触摸它们。作为程序员,您必须提供一个完整的对象

有两种常见的方法可以做到这一点:

  • 您假设空字段应该保持不变,并相应地修改
    fillform

    if (updateForm.getUserId().isEmpty()) { updateForm.setUserId(user.getUserId()); }
    ...
    
  • 在post前面的get中,您可以在表单前面加上当前
    User
    值(更常见的情况是,除非您需要之前没有get部分的post)
编辑

为了预先填充表单(jsp部分似乎很好),控制器应该在GET方法的模型中放置一个填充的
UserForm

@RequestMapping(method = RequestMethod.GET)
public String updateView(@ModelAttribute(value = "updateForm")
                                 UserForm updateForm,
                                 HttpSession session) {
    User user = (User) session.getAttribute("user");
    updateForm.setUser(user);
    return "profileupdate";
}
由于
@modeldattribute
注释,
updateForm
隐含在模型中,或

@RequestMapping(method = RequestMethod.GET)
public ModelAndView updateView(HttpSession session) {
    updateForm = new UserForm();
    User user = (User) session.getAttribute("user");
    updateForm.setUser(user);
    return new ModelAndView("profileupdate", "updateForm", updateForm);
}

我还删除了
value=“/edit”
,因为它不在
updateUserProcess
上,我假设
“/edit”
已在控制器上建立好。

spring或hibernate很难猜测哪些值为空,因为用户希望它们为空,而哪些值为空,因为不必触摸它们。作为程序员,您必须提供一个完整的对象

有两种常见的方法可以做到这一点:

  • 您假设空字段应该保持不变,并相应地修改
    fillform

    if (updateForm.getUserId().isEmpty()) { updateForm.setUserId(user.getUserId()); }
    ...
    
  • 在post前面的get中,您可以在表单前面加上当前
    User
    值(更常见的情况是,除非您需要之前没有get部分的post)
编辑

为了预先填充表单(jsp部分似乎很好),控制器应该在GET方法的模型中放置一个填充的
UserForm

@RequestMapping(method = RequestMethod.GET)
public String updateView(@ModelAttribute(value = "updateForm")
                                 UserForm updateForm,
                                 HttpSession session) {
    User user = (User) session.getAttribute("user");
    updateForm.setUser(user);
    return "profileupdate";
}
由于
@modeldattribute
注释,
updateForm
隐含在模型中,或

@RequestMapping(method = RequestMethod.GET)
public ModelAndView updateView(HttpSession session) {
    updateForm = new UserForm();
    User user = (User) session.getAttribute("user");
    updateForm.setUser(user);
    return new ModelAndView("profileupdate", "updateForm", updateForm);
}

我还删除了
value=“/edit”
,因为它不在
updateUserProcess
上,我假设
“/edit”
已在控制器上建立好。

好吧,主要问题是JSP上的路径。我没有通过控制器处理请求,而是只设置了一个指向页面的链接。因此,建议-小心并注意映射

链接版本错误

<form name="redaction" 
      action="${pageContext.request.contextPath}/updatepage.jsp"
      method="GET"
      enctype="application/x-www-form-urlencoded">
      <input type="submit" name="submit" value="Redaction"/>
 </form>
  <form name="redaction" 
        action="${pageContext.request.contextPath}/edit"
        method="GET"
        enctype="application/x-www-form-urlencoded">
        <input type="submit" name="submit" value="Redaction"/>
 </form>

好吧,主要问题是JSP上的路径。我没有通过控制器处理请求,而是只设置了一个指向页面的链接。因此,建议-小心并注意映射

链接版本错误

<form name="redaction" 
      action="${pageContext.request.contextPath}/updatepage.jsp"
      method="GET"
      enctype="application/x-www-form-urlencoded">
      <input type="submit" name="submit" value="Redaction"/>
 </form>
  <form name="redaction" 
        action="${pageContext.request.contextPath}/edit"
        method="GET"
        enctype="application/x-www-form-urlencoded">
        <input type="submit" name="submit" value="Redaction"/>
 </form>

当控制器发生故障时,
UserForm
中的数据是否为null或
User
中的数据?User中的数据与UserForm中的数据相同。我只想更改一个字段。当控制器发生故障时,
UserForm
中的数据是否为null或
User
中的数据?User中的数据与UserForm中的数据相同。我只想更改一个字段。我没有做任何检查,因为我没有在任何示例中看到它。我问过的每个人都告诉我,Hibernate会注意更改哪些字段。请检查这些值是否实际得到反映。我没有做任何检查,因为我没有在任何示例中看到它。我问过的每个人都告诉我,Hibernate将负责更改哪些字段。请检查这些值是否实际得到反映。哦,我写得不太正确。我已经声明我想使用第二种方法,在表单前面加上当前用户的前缀。但是我应该在哪里做呢?在GET方法中?您能演示一下如何显示表单吗?添加了一个JSP,可能还有控制器中的一个GET方法。问题是-我的应用程序根本看不到updateView(GET)方法。我能看穿调试器。哦,我写得不太正确。我已经声明我想使用第二种方法,在表单前面加上当前用户的前缀。但是我应该在哪里做呢?在GET方法中?您能演示一下如何显示表单吗?添加了一个JSP,可能还有控制器中的一个GET方法。问题是-我的应用程序根本看不到updateView(GET)方法。这我能看穿。