PREVIOUS:SPRINGMVC_2_ Real Controller next:springmvc_4_ Real FileUpload
Binding: Populate the Model object with the fields in the request in the name matching principle.
1) @ModelAttribute on Method
2) Redirect/forward
------------------------------------------------------
Coursecontroller.java
@RequestMapping (value= "/admin", method = Requestmethod.get, params = "add")//Intercept URL Object/admin 2) Request parameter add
Public String Createcourse () {
return "Course_admin/edit";
}
@RequestMapping (value= "/save", method = Requestmethod.post)
Public String Dosave (Course Course) {Original method
To complete the process of binding parameters @ModelAttribute the annotation of the method parameter level
Public String dosave (@ModelAttribute Course Course) {
Log information used to confirm the action
Log.debug ("Info of Course:");
Log.debug (reflectiontostringbuilder.tostring (course));
Doing business operations here, such as database persistence
Course.setcourseid (123);
Request redirection
Return "redirect:view2/" +course.getcourseid ();
}
-------------------------------------------------------------------
edit.jsp
The page space name matches the property name of the model. Pay attention to casesensitive.
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@ taglib Prefix="C"URI="Http://java.sun.com/jsp/jstl/core" %><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>I'm not really a web of classes.</title><Linkrel= "stylesheet"href= "<%=request.getcontextpath ()%>/resources/css/main.css"type= "Text/css" /></Head><Body> <DivID= "Main"> <Divclass= "Newcontainer"ID= "Course_intro"> <formname= "MainForm"Action= "<%= request.getcontextpath ()%>/courses/save"Method= "POST"> <Div> <span>Course Name:</span><inputtype= "text"ID= "title"name= "title"> </Div> <Div> <span>Course Length:</span><inputtype= "text"ID= "duration"name= "duration">seconds</Div> <Div> <span>Course Difficulty:</span> <SelectID= "Level"name= "Level"> <optionvalue= "0">Primary</option> <optionvalue= "1"selected= "Selected">Intermediate</option> <optionvalue= "2">Senior</option> </Select> </Div> <Div> <span>Course Description:</span> <textareaID= "Descr"name= "Descr"rows= "5"style= "width:480px"></textarea> </Div> <Div> <inputtype= "Submit"ID= "Btnpass"value= "Submit" /> </Div> </form> </Div> </Div></Body></HTML>
Verify the modification:
MAVEN Operations
1) Cmd:ctrl +c Stop Jetty
Cls
2) then restart MVN jetty:run
View the log of cmd
Springmvc_3_ Solid Binding