私はこのオブジェクトを持っています
public class Deportista implements Serializable {
private static final long serialVersionUID = 6229604242306465153L;
private String id;
...
@NotNull(message="{field.null}")
public String getId() {
return id;
}
...
}
次のコントローラーのメソッドがあります
@InitBinder(value="deportistaRegistrar")
public void registrarInitBinder(WebDataBinder binder) {
logger.info(">>>>>>>> registrarInitBinder >>>>>>>>>>>>>");
}
@RequestMapping(value="/registrar.htm", method=RequestMethod.GET)
public String crearRegistrarFormulario(Model model){
logger.info("crearRegistrarFormulario GET");
Deportista deportista = new Deportista();
model.addAttribute("deportistaRegistrar", deportista);
return "deportista.formulario.registro";
}
@RequestMapping(value="/registrar.htm", method=RequestMethod.POST)
public String registrarPerson(@Validated @ModelAttribute("deportistaRegistrar") Deportista deportista,
BindingResult result){
logger.info("registrarPerson POST");
logger.info("{}", deportista.toString());
if(result.hasErrors()){
logger.error("There are errors!!!!");
for(ObjectError objectError : result.getAllErrors()){
logger.error("Error {}", objectError);
}
return "deportista.formulario.registro";
}
logger.info("All fine!!!!");
this.fakeMultipleRepository.insertDeportista(deportista);
return "redirect:/manolo.htm";
}
ここまでは、コントローラーがフォームを作成 (GET) し、新しい コマンドオブジェクトを送信 (POST) できるようになるまで、Validation
コードはうまく機能します。
問題はアップデートにあります。
私は次のものを持っています:
@InitBinder(value="deportistaActualizar")
public void actualizarInitBinder(WebDataBinder binder) {
logger.info(">>>>>>>> actualizarInitBinder >>>>>>>>>>>>>");
binder.setDisallowedFields("id");
}
私が持っていることを観察しますbinder.setDisallowedFields("id")
public String crearActualizarFormulario(@PathVariable("id") String id, Model model){
logger.info("crearActualizarFormulario GET");
Deportista deportista = this.fakeMultipleRepository.findDeportista(id);
model.addAttribute("deportistaActualizar", deportista);
return "deportista.formulario.actualizacion";
}
@RequestMapping(value="/{id}/actualizar.htm", method=RequestMethod.POST)
public String actualizarPerson(@Validated @ModelAttribute("deportistaActualizar") Deportista deportista,
BindingResult result){
logger.info("actualizarPerson POST");
logger.info("{}", deportista.toString());
if(result.hasErrors()){
logger.error("There are errors!!!!");
for(ObjectError objectError : result.getAllErrors()){
logger.error("Error {}", objectError);
}
return "deportista.formulario.actualizacion";
}
logger.info("All fine!!!!");
this.fakeMultipleRepository.updateDeportista(deportista);
return "redirect:/manolo.htm";
}
問題は:
- フォームまたはコマンドにエラーがある場合、コントローラーはビューを再レンダリングし、フォームが表示され、エラーメッセージがどのように予想されるかを示しますが、ID 値はありません
また
- オブジェクトを更新しようとすると、もちろん id 値を保持し、エラーなしで単に更新を続行すると失敗します
コンソールに次のように表示されます。
- -------- createCollections ---------------
- >>>>>>>> actualizarInitBinder >>>>>>>>>>>>>
- Skipping URI variable 'id' since the request contains a bind value with the same name.
- actualizarPerson POST
- Deportista [id=null, nombre=Manuel, ...]
- There are errors!!!!
- Error Field error in object 'deportistaActualizar' on field 'id': rejected value [null]; codes [NotNull.deportistaActualizar.id,NotNull.id,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [deportistaActualizar.id,id]; arguments []; default message [id]]; default message [The field must be not empty]
IDがヌルです。Request Scopeを維持するこの問題を回避するにはどうすればよいですか?
動作している代替コントローラーがあり@SessionAttributes
、すべてが完璧に機能します。しかし、ユーザーが同じ Web ブラウザーで多くのタブ (1 つは作成用、もう 1 つは更新用) を開いていると、大きなリスクになるため、すべてが非常に間違ったものになります。Spring MVC + セッション属性と複数のタブに従って、セッション スコープの代わりにリクエスト スコープを使用する必要があります。意味があります。
悲しいことに、Spring はこれを修正しないようです: @SessionAttributes はタブ ブラウジングでは機能しません
添加
あなたの提案によると、私は以下を持っています:
@ModelAttribute("deportistaActualizar")
public Deportista populateActualizarFormulario(@RequestParam(defaultValue="") String id){
logger.info("populateActualizarFormulario - id: {}", id);
if(id.equals(""))
return null;
else
return this.fakeMultipleRepository.findDeportista(id);
}
メソッドの使用を観察してください@RequestParam
。私の問題は、更新する URL が次のスタイルの場合に、そのメソッドを更新して機能させる方法
http://localhost:8080/spring-utility/deportista/1/actualizar.htm
です。URL にパラメーターがないため、現在は役に立ちません@RequestParam
。
私はすでにSpring Referenceのドキュメントを読んでいます: Using @ModelAttribute on a method
2回目の加算
はい、あなたは正しかったです。私は昨日それを行いましたが、次のことを共有するのを忘れています。
@ModelAttribute("deportistaActualizar")
public Deportista populateActualizarFormulario(@PathVariable(value="id") String id){
logger.info("populateActualizarFormulario - id: {}", id);
if(id.equals(""))
return null;
else
return this.fakeMultipleRepository.findDeportista(id);
}
a@ModelAttribute
はどのメソッドでも常に前に呼び出されるためhandler
、次の URL は失敗http://localhost:8080/spring-utility/deportista/registrar.htm
し、ページに次のように表示されます。
HTTP Status 400 -
type Status report
message
description The request sent by the client was syntactically incorrect.
もちろん、URL に予期されるid
. したがって、後で編集/表示するために新しいレコードを作成することはできません。
次の作業について確認できます。
http://localhost:8080/spring-utility/deportista/1/detalle.htm
http://localhost:8080/spring-utility/deportista/1/actualizar.htm
ID (1) が取得されます。
どうすればこれを解決できますか?
ありがとうございました