Java 更新表会从jsp页面中未绑定的列中删除数据

Java 更新表会从jsp页面中未绑定的列中删除数据,java,hibernate,jsp,spring-mvc,Java,Hibernate,Jsp,Spring Mvc,我有一个有很多列的表。当我使用@modeldattribute更新某些列时,jsp页面中未指定的列中的数据将被删除 范例 @RequestMapping(value= "/studenteducation", method= RequestMethod.GET) public String setupUpdateEducationForm(Model model, @ModelAttribute("education") Student student){ student = stud

我有一个有很多列的表。当我使用@modeldattribute更新某些列时,jsp页面中未指定的列中的数据将被删除

范例

@RequestMapping(value= "/studenteducation", method= RequestMethod.GET)
public String setupUpdateEducationForm(Model model, @ModelAttribute("education") Student student){

    student = studentService.getStudent(getStudentName());
    model.addAttribute("education", student);  //adding saved student data into model
    return "studenteducation";
}

@RequestMapping(value= "/studenteducation", method= RequestMethod.POST)
public String studentEducationForm(Model model, @ModelAttribute("education") Student student, BindingResult result){

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

    student.setStudentId(getStudentName()); // inserting primary key
    studentService.updateStudent(student);  // updating student table

    return "redirect:/";        
}
问题是我没有绑定此更新中的所有列,因此只保留绑定列数据,而从数据库中删除其他列数据。我有15列,在这个jsp页面中绑定了5列

我的完整控制器代码:

@Controller
@RequestMapping("/user")
@SessionAttributes("education")
public class StudentController {

@Autowired
StudentService studentService;
Student student = new Student();

// Setup new Student form------------------------------------------------------------------ 

@RequestMapping(value= "/newuser", method= RequestMethod.GET)
public String setupAddStudentForm(Model model, @ModelAttribute("student") Student student){

    return "registerstudent";
}

// Add new Student------------------------------------------------------------------    

@RequestMapping(value= "/newuser", method= RequestMethod.POST)
public String sddStudent(Model model, @ModelAttribute("student") Student student, BindingResult result, SessionStatus sessionStatus){

    if(result.hasErrors()){
        return "registerstudent";
    }
    studentService.addStudent(student); 
    sessionStatus.setComplete();
    return "redirect:/";
}

// Setup Edit Profile Form------------------------------------------------------------------    

@RequestMapping(value= "/updateprofile", method= RequestMethod.GET)
public String setupStudentProfileForm(Model model, @ModelAttribute("profile") Student student ){

    Student stu = studentService.getStudent(getStudentName());
    model.addAttribute("name", student.getStudentId());
    model.addAttribute("studentprofile", stu);
    return "studentprofile";        
}

// Update student profile------------------------------------------------------------------ 

@RequestMapping(value= "/updateprofile", method= RequestMethod.POST)
public String studentProfileForm(Model model, @ModelAttribute("profile") Student student, BindingResult result, SessionStatus sessionStatus ){

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

    student.setStudentId(getStudentName());
    studentService.updateStudent(student);      
    sessionStatus.setComplete();
    return "redirect:/";        
}

// Setup Update Education Form------------------------------------------------------------------    

@RequestMapping(value= "/studenteducation", method= RequestMethod.GET)
public String setupUpdateEducationForm(Model model, @ModelAttribute("education") Student student){

    student = studentService.getStudent(getStudentName());
    model.addAttribute("education", student);
    return "studenteducation";
}

// Update Student Education------------------------------------------------------------------   

@RequestMapping(value= "/studenteducation", method= RequestMethod.POST)
public String studentEducationForm(Model model, @ModelAttribute("education") Student student, BindingResult result, SessionStatus sessionStatus){

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

    student.setStudentId(getStudentName());
    studentService.updateStudent(student);
    sessionStatus.setComplete();
    return "redirect:/";        
}
我的学生模型id很大,我正在通过2个控制器进行更新。不同jsp页面中的学生配置文件和具有不同控制器的不同jsp页面中的教育。当我更新一部分时,另一部分会删除。

这就是我的目的

在控制器类声明的顶部添加
@SessionAttributes(“教育”)

在第一个方法中去掉
@ModelAttribute(“教育”)Student
参数。将学生添加到该方法中的模型中

SessionStatus
参数添加到POST处理程序方法中,并在完成后调用
SessionStatus.setComplete()

你应该使用这个模式

如果不允许用户编辑此表单中的某些实体字段,则还应使用@InitBinder设置不允许的字段:

@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
  dataBinder.setDisallowedFields("foo");
}

这是SpringMVC和Hibernate协同工作的方式。当您在控制器方法un
@modeldattribute(“教育”)Student
中编写时,SpringMVC创建一个新对象并用表单数据填充它。所有其他字段保持其初始化值(通常为空)。 你可能会觉得这很愚蠢,但我从来没有在SpringMVC中找到一个好的、干净的方法来处理这个问题。常见的解决办法:

  • 使用Neil McGuigan建议的
    SessionAttributes
    。问题在于,当您不再需要该属性时,该属性可以保持会话状态,并且显然不能用于无会话API使用
  • 在URL中传递id,并使用
    转换器
    将其转换为正确的
    学生
    对象。但是您必须在服务层之外调用持久性层
表上有任何触发器吗?没有,它只是带有hibernate annotation@Entity的简单表。是否有其他更好的方法在hibernate中更新表。我使用的是getSessionFactory.update(学生),它给出了异常org.springframework.web.HttpSessionRequiredException:预期的会话属性“education”。我已经用新代码plz CHECK更新了这个问题。您可能需要用@modelattribute方法how加载默认sessionattributes,先生?我找不到这方面的例子。谢谢你的回答。我只是走了一条容易的路。我将我的student表拆分为两个不同的表,现在更新jsp页面中的完整列。因此,它解决了我在已填充的未使用列中获取空值的问题。