「Spring」@ModelAttributeとRequestMethod.POSTのサンプル

構文
JSP画面
<form:form action="abcForm" method="post" modelAttribute="abcForm">
Controller側
public String 関数名(@ModelAttribute xxxForm abcForm,Model model )

1.JSP側

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<form:form action="userForm" method="post" modelAttribute="userForm">
<form:hidden path-"userId"/>
<form:input path="userMemo"></form:input>
</form:form>
<form:form action="userForm" method="post" modelAttribute="userForm"> <form:hidden path-"userId"/> <form:input path="userMemo"></form:input> </form:form>
<form:form action="userForm" method="post" modelAttribute="userForm">
 <form:hidden path-"userId"/>
  <form:input path="userMemo"></form:input>
 </form:form>

2.Controller側

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@RequestMapping(value ='/create',method = RequestMethod.POST)
public String getProfile(@ModelAttribute UserForm userForm,Model model ) {
model.addAttribute("profileId", "123"+userForm.getUserId() );
model.addAttribute("profileMemo", "testmark"+userForm.getUserMemo())
// some code
}
@RequestMapping(value ='/create',method = RequestMethod.POST) public String getProfile(@ModelAttribute UserForm userForm,Model model ) { model.addAttribute("profileId", "123"+userForm.getUserId() ); model.addAttribute("profileMemo", "testmark"+userForm.getUserMemo()) // some code }
@RequestMapping(value ='/create',method = RequestMethod.POST)
public String getProfile(@ModelAttribute UserForm userForm,Model model ) {
   model.addAttribute("profileId", "123"+userForm.getUserId() );
   model.addAttribute("profileMemo", "testmark"+userForm.getUserMemo()) 
   // some code
}

3.Form側

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class UserForm {
private String userId;
private String userMemo;
//some code
}
public class UserForm { private String userId; private String userMemo; //some code }
public class UserForm {
    private String userId;
    private String userMemo;
       //some code
}

 

SpringMVC

Posted by arkgame