2017-10-20 59 views
0

我遇到了控制器/虛擬機數據傳輸問題,無法找到任何解決方案。 Ive得到了一個用戶(見下類)彈簧+速度不成功嘗試保存對象

package com.Entity; 

import org.springframework.transaction.annotation.Transactional; 
import javax.persistence.*; 
import java.util.Date; 

@Entity 
@Transactional 
@Table(name = "USERS") 
public class User { 
    private Long id; 
    private UserType type; 
    private String email; 
    private String password; 
    private String name; 
    private String tel; 
    private Date regDate; 
    private Date lastActive; 
    private Agent office; 

    //Constructors 
    public User(){ 

    } 

    public User(UserType type, String email, String password, String name, String tel, Agent office) { 
     this.type = type; 
     this.email = email; 
     this.password = password; 
     this.name = name; 
     this.tel = tel; 
     this.regDate = new Date(); 
     this.lastActive = null; 
     this.office = office; 
    } 

    //Getters 
    @Id 
    @SequenceGenerator(name = "USERID_SEQ", sequenceName = "USERID_SEQ",allocationSize = 1) 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USERID_SEQ") 
    @Column(name = "ID") 
    public Long getId() { 
     return id; 
    } 
    @Column(name = "TYPE") 
    public UserType getType(){ 
     return type; 
    } 
    @Column(name = "EMAIL") 
    public String getEmail() { 
     return email; 
    } 
    @Column(name = "PASSWORD") 
    public String getPassword() { 
     return password; 
    } 
    @Column(name = "NAME") 
    public String getName() { 
     return name; 
    } 
    @Column(name = "TEL") 
    public String getTel() { 
     return tel; 
    } 
    @Column(name = "DATE_REG") 
    public Date getRegDate() { 
     return regDate; 
    } 
    @Column(name = "LAST_ACTIVE") 
    public Date getLastActive() { 
     return lastActive; 
    } 
    @ManyToOne (targetEntity = Agent.class, fetch = FetchType.EAGER) 
    @JoinColumn(name = "OFFICEID") 
    public Agent getOffice() { 
     return office; 
    } 

    // Setters 
} 

控制器它

package com.Controllers; 

import com.Entity.AgentType; 
import com.Entity.User; 
import com.Services.AgentService; 
import com.Services.UserService; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 
import java.util.List; 

//TODO: TEST CONTROLLER SUBJECT TO DELETE 
@Controller 
public class ViewController { 

    @Autowired 
    private UserService userService; 
    @Autowired 
    private AgentService agentService; 

    @RequestMapping(value = "/list", method = RequestMethod.GET) 
    public ModelAndView listUsersPage(){ 
     List<User>list = userService.getAll(); 
     return new ModelAndView("fragments/userss.vm","users",list); 
    } 

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET) 
    public ModelAndView edit(@PathVariable Long id){ 
    return new ModelAndView("fragments/edit.vm", 
      "user", (User)userService.getById(id)); 
    } 

    //FUNCTIONAL 
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET) 
    public ModelAndView delete(@PathVariable Long id){ 
     userService.delete(userService.getById(id)); 
     return new ModelAndView("redirect:/list"); 
    } 

    @RequestMapping(value = "/update", method = RequestMethod.POST) 
    public ModelAndView update(User user){ 
     User user1 = user; 
     //userService.update(user1); 
     return new ModelAndView("redirect:/list"); 
    } 



    //Model Attributes 
    @ModelAttribute 
    public void userTypesList(Model model){ 
     model.addAttribute("types", userService.getPositions()); 
    } 
    @ModelAttribute 
    public void officesList(Model model){ 
     model.addAttribute("offices", agentService.getAllByType(AgentType.OFFICE)); 
    } 
} 

和頁面(.vm)來添加新的或修改現有用戶(只是一個例子編輯頁面):

<title>EDIT USER</title> 
<body> 
<form method="post" action="/update"> 
    id: 
    <input type="text" name="id" path="id" value="$user.id"/> <br> 

    Type: 
    <select name="type" path="type"> 
     <option hidden selected>$user.type</option> 
     #foreach($type in $types) 
      <option value="$type">$type</option> 
     #end 
    </select> <br> 

    e-mail: 
    <input type="text" name="email" path="email" value="$user.email"/> <br> 

    Password: 
    <input type="text" name="password" path="password" value="$user.password"/> <br> 

    Name: 
    <input type="text" name="name" path="name" value="$user.name"/> <br> 

    Tel: 
    <input type="text" name="tel" path="tel" value="$user.tel"/> <br> 

    Reg Date: 
    <input type="date" name="regDate" path="regDate" value="$user.regDate"/> <br> 

    Last Active: 
    <input type="date" name="lastActive" path="lastActive" value="$user.lastActive"/> <br> 

    Office: 
    <select name="office" path="office"> 
     <option hidden selected value="$user.office">$user.office.name</option> 
     #foreach($office in $offices) 
      <option value="$office">$office.name</option> 
     #end 
    </select> <br> 

    <input type="submit" value="Update"/> 

</form> 

</body> 

問題是,我可以t manage to save the updated User via /update(User user). I已嘗試不同的方式,但仍然沒有成功。 Whis this code I`m getting HTTP Status 400 - Bad Request。由於某些被認爲是客戶端錯誤(例如,格式錯誤的請求語法,無效的請求消息框架或欺騙性請求路由),服務器無法或不會處理該請求。 你能幫我一下嗎?它有什麼問題?

+0

也許如果您取消註釋此行:'//userService.update(user1);'... – Henry

+0

不幸的是,這並不能解決問題。我已經評論它只是爲了檢查它是不是錯誤的原因。 – Vampirenostra

回答

0

在你的代碼中你缺少了一些東西。所有的 弗里斯特,你錯過的形式指定的模型屬性:

<form method="post" action="/update" modelAttribute="user"> 

其次,你缺少的POST方法指定的模型屬性:

@RequestMapping(value = "/update", method = RequestMethod.POST) 
public ModelAndView update(@ModelAttribute("user") User user){ 
    User user1 = user; 
    userService.update(user1); 
    return new ModelAndView("redirect:/list"); 
} 

如果您需要進一步的細節,你可以閱讀Getting Started with Forms in Spring MVC

+0

謝謝你的觀點。但它並沒有太多的幫助,我仍然得到了HTTP狀態400(((在我看來,.vm沒有識別modelAttrinbute,Idea並不建議這麼做),Maby應該有一些標籤庫,就像jsp?但它的方式是在教程中你已經發布了id does not wotk)) – Vampirenostra

+0

檢查請求傳遞的參數我看到傳入的唯一參數是:type和office。其餘的不會(( – Vampirenostra

+0

))我發現HTTP 400是由辦公室領域引起的,實際的對象在哪裏被傳輸,有什麼建議如何打敗它呢? – Vampirenostra