SPRINGMVC Learning Note II (Get parameters, return model data)

Source: Internet
Author: User
Tags class definition

how controllers Get Parameters

1 using the original Servlet method, use the HttpServletRequest

2 using the @requestparam annotation @RequestParam ("username") String name, when username does not exist in the request, throws an exception, you can use @requestparam (value= " Username "Required=false default=" default ") so the request has a value to take, no value is not taken

3 Encapsulating with entity classes

--Write an entity class, and then add the Setter,getter method

--The setter method of the entity class is consistent with the requested parameters

Controller return parameter value

Way One:

Controller return type is Modelandview ================= "learn

map<string,object> map= new hashmap<string,object> ();

Map.put (String,object);

Return Modelandview (String JSP name, map);

Way two:

In the Controller method, add the model parameter such as: =================== "recommended to use

Public String Checklogin (model model) {

Model.addattribute (Key,value);

return "OK";

}

Way three:

Using @modelattribute annotations, you can put method parameters in model

@ModelAttribute ("name") String username the username value into the model,

Key is name, equivalent to Model.addattribute ("name", username);

Another use of @ModelAttribute (), put it above the method

Program has entered controller first execute @modelattribute () and then execute @requestmapping

@ModelAttribute ("MSG")//equivalent to Model.addattribute ("MSG", Findmsg ())

Public String findmsg () {

return "Hello";

}

@RequestMapping ()

Public String Controller () {}

--------------------------------------------------------------------------

storing data in the session

A using the servlet scheme, use the HttpSession parameter

b using the @sessionattribute ("name") tag, which is used before the class definition, means that the information stored in the model is the name of the key, and the request scope is converted to the session range.

You need to store the name set of values in the model before use

@SessionAttribute ("name") @SessionAttribute ({"Name", "Age"})

public class Logincontroller () {

Public String () {

Model.addattribute ("name", Object);

}

}

Related Article

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.