SPRINGMVC using entity parameters and Servletapi

Source: Internet
Author: User

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

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.