Spirng MVC +velocity Form Binding Command object

Source: Internet
Author: User

Typically, the data in the form can be extracted from the parameter list of the controller function by spring MVC's @requestparam annotations, but once the form data is too large, the parameter list becomes very long, The best solution is to encapsulate the data in the form into a custom object so that the data contained in the entire form can be passed directly with a single Command object.

Keywords: #springBind宏

Yes, this macro is the key to binding. First we need to turn on spring MVC's support for macros in the velocity template, which requires the following settings:

<Bean ID= "Viewresolver"
class= "Org.springframework.web.servlet.view.velocity.VelocityViewResolver"
>
## ....
< Property name= "Exposespringmacrohelpers">
<value>True</value>
</ Property>
</Bean>

We use a simple login control system to illustrate the binding process, assuming we want to implement an interface like this:

From the spring MVC point of view, Rendering this template (assuming this template is LOGIN.VM) first needs to go through the function of a controller, assuming that the function name is Autobindlogin, then this function does one thing when rendering the template, which is to tell the template what the instance of the command object needs to be bound.

Of course, we first have to introduce the definition of the class to which the command object belongs:

Package Com.lipan.DO;

public class Accountmodel {
Private String username;
private String password;

public void Setusername (String username) {
This.username = Username;
}
public void SetPassword (String password) {
This.password = password;
}
Public String GetUserName () {
return username;
}
Public String GetPassword () {
return password;
}
}

As you can see, this is just a simple Pojo class that is generated purely for encapsulating data, and its two member variables correspond to two form inputs in the LOGIN.VM respectively.

Back to the question, how do you tell an instance of a form-bound object? The following code is the answer-we generate one for it and then pass it to the template by name Convention (the name is the same).

Lipan: The object that passed in the form
@RequestMapping (value="/autobind", Method={requestmethod.get})
Public String Autobindlogin (model model) {
Model.addattribute ("Accountmodel"new Accountmodel ());
return "Login";
}


After this, the template can be used to pass in the Command object index "$accountmodel", the use of the following:

# # LOGIN.VM
# # Author:lipan
<!DOCTYPE HTML>
<HTML>
<Head>
<Meta http-equiv= "Content-type" content= "TEXT/HTML;CHARSET=GBK">
<title>Data Binding Example</title>
</Head>
<Body>
<form Method= "POST">
#springBind ("Accountmodel.username")
Name:<input type= "text" name= "${status.expression}" value= "$!status.value"/>
<Font Color= "#FF0000">${status.errormessage}</Font><BR/>
#springBind ("Accountmodel.password")
Password:<input type= "text" name= "${status.expression}" value= "$!status.value"/>
<Font Color= "#FF0000">${status.errormessage}</Font><BR/>
<input type= "Submit" name= "Submit"/><BR/>
</form>
</Body>
</HTML>


Once the form content is successfully posted, the instance can be uploaded to the controller that handles the form request (here is the POST request), which is handled by the corresponding function, such as the following function:

@RequestMapping (value= "/autobind", Method={requestmethod.post})
Public String Autobindresult (Model Model,accountmodel am) {
/*am.setpassword ("LIPANTESTPW");
Am.setusername ("Lipan"); */
Model.addattribute ("Accountmodel", AM);
return "Autobindresult";
}

Then AUTOBINDRESULT.VM can show the specific contents of the Command object:

# #autoBindResult. VMs
# #author: Lipan
Username: ${accountmodel.username}</br>
Password: ${accountmodel.password}</br>

The results of the AUTOBINDRESULT.VM rendering are as follows:

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.