First, the Entity parameters
Before we know that using the annotation @requestparam can get the value of the parameter, it is time to submit a form to get the value. You can say you can use Request.getparameter ("") to get it, yes this method is actually available. So now if the form has 100 parameters, we're not going to get it one at a time and then in set to model, that's a lot of trouble. In fact, SPRINGMVC can directly correspond to the value one by one to model, the following example:
Package Com.ztz.springmvc.model;public class Users {private string Name;private string Password;private Roles roles;// Omit Get Set method//Rewrite toString () to conveniently test @overridepublic String ToString () {return "Users [name=" + name + ", password=" + password + ", roles=" + roles + "]";}}
Package Com.ztz.springmvc.model;public class Roles {private int id;private String name;//Omit Get Set method//rewrite ToString () Convenient to test @overridepublic String toString () {return "Roles [id=" + ID + ", name=" + name + "]";}}
Package Com.ztz.springmvc.controller;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import com.ztz.springmvc.model.users;@ Controller@requestmapping ("/user") public class Userscontroller {@RequestMapping (value= "/getparams", method= Requestmethod.post) Private String findAll (users) {System.out.println (users); return "Users";}}
<form action= "${basepath}user/getparams" method= "POST" ><label> User name: </label><input type= "Text" Name= "name"/><br/><label> Password: </label><input type= "password" name= "password"/><br/ ><label> No.: </label><input type= "text" name= "roles.id"/><br/><label> role name: </ Label><input type= "text" name= "Roles.name"/><br/><input type= "Submit" value= "Titus" /></ Form>
Page input values, click submit, console output results:
Users [Name=test, Password=password, Roles=roles [id=11, Name=admin]]
Second, Servletapi
The following see SPRINGMVC inside how to use SERVLETAPI, actually very useful, see the following example:
@Controller @requestmapping ("/user") public class Userscontroller {@RequestMapping (value= "/getparams", method= Requestmethod.post) Private String findAll (Users users,httpservletrequest request,httpsession Session, HttpServletResponse response) {//drop to request scope Request.setattribute ("Users", users);//Drop to Sessionsession.setattribute ("Session", "Session");//Output Cookieresponse.addcookie (New Cookie ("res", "response123"); return "Users";}}
The JSP for the form is still the same as the previous one. To see the submission succeed, forward to the new JSP page:
<! DOCTYPE html>
Click Submit to see the results:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
SPRINGMVC using entity parameters and Servletapi