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);
}
}