2017-11-29 9 views
2

これは以前のスレッドでは回答されていますが、すべての解決策を実行した後、私はこの問題を解決できませんでした。第3ラウンドBean名 'user'のプレーンターゲットオブジェクトもリクエスト属性として利用できません

ですので、私は春を使用しています& thymeleafは、mySQLデータベースで休止状態の上に。 シンプルなフォームでデータベースにuserloginオブジェクトを登録しようとしましたが、実行時にシステムがただちにクラッシュし、エラーが表示され続けます。

BindingResultもBean名 'user'属性

注:同じ実装を使用してデータベースにclubオブジェクトを追加できます。そのため、userloginオブジェクトに関連付けられた余分な変数と関連付けることができますか?

最初の投稿ですので、簡単に行ってください!ここ

は私の制御コードは、(第2の2つの方法がuserloginの関連するものである)である:ここでは

package com.FYP.Club.controller; 

@Controller 
public class HomeController { 
    @Autowired 
    UserLoginRepository userRepository; 

    @Autowired 
     ClubRepository clubRepository; 

    @RequestMapping(value="/registerclub", method=RequestMethod.GET) 
    public String index(Club club) { 
     return "clubindex"; 
    } 

    @RequestMapping(value = "/club", method = RequestMethod.POST) 
    public String addNewPost(@Valid Club club, Model model) { 
     clubRepository.save(club); 
     model.addAttribute("clubName", club.getClubName()); 
     return "clubresult"; 
    } 


@RequestMapping(value="/register", method=RequestMethod.GET) 
public String index(UserLogin user) { 
    return "index"; 

} 


@RequestMapping(value = "/", method = RequestMethod.POST) 
public String addNewPost(@Valid UserLogin user, Model model) { 
    user.setUserStatus(true); 
    model.addAttribute("email", user.getEmail()); 
    return "result"; 
    } 

} 

ユーザーフォームを有するindex.htmlページである:

<h3>Register</h3> 

<form action="#" th:action="@{/}" th:object="${user}" method="post"> 
    <table> 
    <tr> 
    <td>First name:</td> 
      <td><input type="text" th:field="*{firstName}" /></td> 
     </tr> 
     <tr> 
      <td>Last name:</td> 
      <td><input type="text" th:field="*{lastName}" /></td> 
     </tr> 
     <tr> 
      <td>Phone:</td> 
      <td><input type="number" th:field="*{phone}" /></td> 
     </tr> 

     <tr> 
      <td>Email:</td> 

      <td><input type="text" th:field="*{email}" /></td> 
     </tr> 
      <tr> 

      <td>Address:</td> 
      <td><input type="text" th:field="*{address}" /></td> 
     </tr> 

     <tr> 
      <td>Password:</td> 
      <td><input type="text" th:field="*{password}" /></td> 
     </tr> 
     <tr> 
      <td>UserType:</td> 
      <td><input type="text" th:field="*{userType}" /></td> 
     </tr> 
     <tr> 
      <td><button type="submit">Submit</button></td> 
     </tr> 

    </table> 
</form> 

</td> 

ここにuserloginモデルがあります:

@Entity 
public class UserLogin { 


@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
private Long id; 
private String firstName; 
private String lastName; 
private Long phone; 
private String email; 
private String address; 
private String password; 
private Boolean userStatus; 
private String userType; 


public UserLogin() 
{ 

} 

public UserLogin(Long id, String firstName, String lastName, Long phone, 
     String email, String address, String password, Boolean userStatus, 
     String userType) { 
    super(); 
    this.id = id; 
    this.firstName = firstName; 
    this.lastName = lastName; 
    this.phone = phone; 
    this.email = email; 
    this.address = address; 
    this.password = password; 
    this.userStatus = userStatus; 
    this.userType = userType; 
} 

public String getUserType() { 
    return userType; 
} 

public void setUserType(String userType) { 
    this.userType = userType; 
} 

public Long getId() { 
    return id; 
} 

public void setId(Long id) { 
    this.id = id; 
} 

public String getFirstName() { 
    return firstName; 
} 

public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

public String getLastName() { 
    return lastName; 
} 

public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 

public Long getPhone() { 
    return phone; 
} 

public void setPhone(Long phone) { 
    this.phone = phone; 
} 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

public String getAddress() { 
    return address; 
} 

public void setAddress(String address) { 
    this.address = address; 
} 

public String getPassword() { 
    return password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 

public Boolean getUserStatus() { 
    return userStatus; 
} 

public void setUserStatus(Boolean userStatus) { 
    this.userStatus = userStatus; 
    } 
} 

答えて

0

UserLoginオブジェクトを使用する前に、以下のようにUserLoginオブジェクトを追加する必要があります。

@RequestMapping(value="/register", method=RequestMethod.GET) 
public String index(Model model) { 
    model.addAttribute("user", new UserLogin()); //add model to view 


    return "index"; 

} 

、あなたがBindingResult変数にエラーをキャプチャする必要@Validアノテーションを使用している場合は、この

のようなもの
@RequestMapping(value = "/", method = RequestMethod.POST) 
public String addNewPost(@Valid UserLogin user, Model model, 
         BindingResult errors) { 

    user.setUserStatus(true); 
    model.addAttribute("email", user.getEmail()); 
    return "result"; 
    } 
+1

ホルヘL. Morlaはあなたの助けをありがとう!それがエラーを解決した –

関連する問題

 関連する問題