Springmvc_3_ Solid Binding

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.