[Email protected]
Used to indicate that the instance of the Spring class is a controller. The implementation class of the Controller interface can handle only one single request action, while the controller of the @controller annotation supports simultaneous processing of multiple request actions and is more flexible. Spring uses the scanning mechanism to find all the annotation-based controller classes in the application. The distribution processor scans the method of the class that used the annotation and detects whether the method uses @requestmapping annotations, and the method that uses the @requestmapping annotation is the processor that actually handles the request. To ensure that the controller can be found, two things need to be done:
In the spring MVC configuration file, you like Spring-context
Using the <context:component-scan/> element, the function of this element is: To start the package scanning function, so as to register with @controller, @Service, @repository, @ Component and other annotated classes become spring beans
<context:component-scan base-package= "package path" >
All controller classes should be under the base package and specified to scan the package
Http://localhost:8080/AnnotationTest/haha
package Com.game.controller; import Org.springframework.stereotype.Controller; import Org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class Hellocontroller {@RequestMapping (value = "/haha" public String hello (model model) {Model.addattribute (" Messa GE "," Ascendas Nova " return "haha" ; } }
Send a haha request, assign the model message "Ascendas Nova" and return to haha.jsp
<Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> < Propertyname= "prefix"> <value>/web-inf/content/</value> </ Property> < Propertyname= "suffix"> <value>. jsp</value> </ Property> </Bean>
Prefix view prefix, suffix view suffix
[Email protected]
Used to indicate which class or method spring uses to handle the request action.
PackageCom.game.controller;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping; @Controller @requestmapping (Value= "/user") Public classUsercontroller {@RequestMapping (value= "/register") PublicString Register () {return"Register"; } @RequestMapping (Value= "/login") PublicString Login () {return"Login"; } }
Because Usercontroller is preceded by a @requestmapping (value= "/user"), the relevant request is added/USER
Creating register.jsp and Login.jsp
Http://localhost:8080/AnnotationTest/user/register
Http://localhost:8080/AnnotationTest/user/login
can access success
PackageCom.game.controller;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod; @Controller Public classHellocontroller {@RequestMapping (value= "/haha", method=requestmethod.get,params= "Myparam=nidaye") PublicString Hello (model model) {Model.addattribute ("Message", "Ascendas Nova"); return"Haha"; } }
Http://localhost:8080/AnnotationTest/haha?myParam=nidaye
A request with a value of "Nidaye" named "Myparam" is included in the processing request only;
3.Model and Modelandview
In the parameter types that the request processing method can appear and return, the most important is model and modelandview, and for the MVC framework, the controller controllers execute the business logic to produce model data models, and view views are used to render model data.
SPRINGMVC uses a model interface to store data models internally, which functions like JAVA.UTIL.MAP, but is easier to use than maps. The Modelmap interface implements the map
PackageCom.game.controller;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;ImportOrg.springframework.ui.ModelMap;ImportOrg.springframework.web.bind.annotation.ModelAttribute;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.servlet.ModelAndView;ImportCom.game.model.User; @Controller Public classUser1controller {/**The modelattribute takes precedence over the login call, assigns the requested parameter to the corresponding variable, and can add the object to the model in the method*/@ModelAttribute Public voidUsermodel (String loginname,string password,model Model) {User User=NewUser (); User.setloginname (LoginName); User.setpassword (password); Model.addattribute ("User", user); } @RequestMapping ("/userlogin") PublicString Login (model) {User User= (user) Model.asmap (). Get ("user")); Model.addattribute ("Name", User.getloginname ()); Model.addattribute ("Password", User.getpassword ()); return"RESULT1"; } @RequestMapping ("/userlogin2") PublicString Login (Modelmap model) {User User= (user) model.get ("user")); Model.addattribute ("Name", User.getloginname ()); Model.addattribute ("Password", User.getpassword ()); return"RESULT1"; } }
The modelattribute takes precedence over the login call, assigns the requested parameter to the corresponding variable, and can add the object to the model in the method
Http://localhost:8080/AnnotationTest/userlogin1?loginName=haha&password=123
User user = (user) Model.asmap (). Get ("user"); Model
User user = (user) model.get ("User"); Modelmap
Look underneath Modelandview.
PackageCom.game.controller;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;ImportOrg.springframework.ui.ModelMap;ImportOrg.springframework.web.bind.annotation.ModelAttribute;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.servlet.ModelAndView;ImportCom.game.model.User; @Controller Public classUser2controller {/**The modelattribute takes precedence over the login call, assigns the requested parameter to the corresponding variable, and can add the object to the model in the method*/@ModelAttribute Public voidUsermodel (String loginname,string Password,modelandview model) {User User=NewUser (); User.setloginname (LoginName); User.setpassword (password); Model.addobject ("User", user); } @RequestMapping ("/userlogin3") PublicModelandview Login (modelandview mv) {User User= (user) Mv.getmodel (). Get ("user")); Mv.addobject ("Name", User.getloginname ()); Mv.addobject ("Password", User.getpassword ()); Mv.setviewname ("RESULT1"); returnMV; } }
Add model data with AddObject
Set the View Setviewname
Http://localhost:8080/AnnotationTest/userlogin3?loginName=hehe&password=123
Spring MVC Common Annotations @controller, @RequestMapping, model, and Modelandview