0

このオブジェクトを休止状態で保存する前に、オブジェクトを事前入力するためのベストプラクティスは何ですか?

私がしたこと:

私のコントローラー:

//The Form
@RequestMapping(value = "user/{id}/edit", method = RequestMethod.GET)
public String edit(@PathVariable("id") Long userId, ModelMap modelMap) {
    modelMap.addAttribute("user", userService.find(userId));    
    return "user/userEdit";

}
//Updating database
@RequestMapping(value = "user/edit", method = RequestMethod.POST)
public String update(@ModelAttribute("user") @Valid User user, BindingResult result,
                                                RedirectAttributes redirectAttrs) {
    if (result.hasErrors()) {
            return "user/userEdit";
    }else{
        userService.update(user);
        redirectAttrs.addFlashAttribute("message", "Success");

        return "redirect:user/userEdit";
    }
}

すべてのフィールド(ユーザー名、パスワード、ID)を含むフォームを作成すると機能しますが、ユーザーにパスワードのみを更新させたい場合はどうすればよいですか?

ユーザー名に@NotEmptyがあるので、ユーザー名がnullであるというエラーが表示されます。これは、フォームにないためですが、ユーザー名フィールドは入力せず、パスワードだけを入力します。

私のhtmlフォーム:

<c:url var="url" value="/user/edit" />
<form:form method="post" action="${url}" modelAttribute="user" class="form-horizontal">
    <form:hidden path="id"/>
    <form:hidden path="version"/>
    <fieldset>
        <div class="control-group">
            <form:label cssClass="control-label" path="password"><spring:message code="user.label.password"/>: </form:label>
            <div class="controls">
                <form:input cssClass="input-xlarge" path="password" />
            </div>
            <form:errors path="password"/>
        </div>
        <div class="control-group">
            <form:label cssClass="control-label" path="userRole"><spring:message code="user.label.role"/>: </form:label>
            <div class="controls">
                <form:select path="userRole">
                       <form:options items="${userRoleList}" itemValue="id" itemLabel="name"/>
                </form:select>
            </div>
            <form:errors path="userRole"/>
        </div>
        <div class="control-group">
            <form:label cssClass="control-label" path="costumer.id"><spring:message code="user.label.costumer"/>: </form:label>
            <div class="controls">
                <form:select path="costumer.id">
                       <form:options items="${costumerList}" itemValue="id" itemLabel="name"/>
                </form:select>
            </div>
            <form:errors path="costumer.id"/>
        </div>

        <div class="form-actions">
            <button type="submit" class="btn btn-primary">Save changes</button>
            <a class="btn cancel link" href="<c:url value="/user" />">Cancel</a>
        </div>
    </fieldset>
</form:form>
  • @Sessionattributesを使用してみましたが、ブラウザのタブを使用して2人以上のユーザーを編集しようとするとうまくいきません。
  • プロパティエディタを使用してみましたが、@ModelAtrributeユーザーユーザーでは機能しませんでした。
  • コンバーターを使ってみましたが、うまくいきませんでした。

最初にUseruser= userService.find(id)を作成してから、更新された値を設定する唯一の方法はありますか?何かのようなもの:

@RequestMapping(value = "user/edit", method = RequestMethod.POST)
public String update(@RequestParam("password") String password, BindingResult result, RedirectAttributes redirectAttrs) {
    User user = userService.find(id);
if (password == null{                           
    return "user/userEdit";
}else{
    user.setPassword("password");
    userService.update(user);
    redirectAttrs.addFlashAttribute("message", "Success");

    return "redirect:user/userEdit";
}
}

検証がないので、これは間違っているように見えます。

4

4 に答える 4

0

別の方法は、面倒で事故が発生しにくいと思いますが、たとえば、UIフォームをモデル化するクラスを作成することです。

public class EditUserForm {
    // getters and setters for password and other fields...
}

コントローラのupdate(EditUserForm,...)メソッドでは、のユーザーが入力したフィールドを、更新するEditUserFormインスタンスにマップするだけです。User

于 2012-05-22T15:40:54.427 に答える
0

投稿したコードでは、移動制御および永続化操作を実装する前に、GUI側の更新に関連付けるための外部ヘルパークラスが必要であることは明らかです。

于 2012-05-22T15:44:53.030 に答える
0

私もこの問題に遭遇しました。フィールドが2つしかない場合は、2番目の例を使用して、フィールドを1つずつ検証します。それ以外の場合は、他のポスターが言ったことを実行し、フォームに一致する新しいクラスを作成する必要があります。

于 2012-05-22T16:00:35.960 に答える
0

使用してみてください:

@PrePersist
@PreUpdate
public void prepare(){
  //エンティティで何かを行う//例:if(name == null){name = "MYNAMEVALUE";}
}

于 2012-05-22T16:03:00.823 に答える