SPRINGMVC Note Controller Details

Source: Internet
Author: User

Mainly includes four categories: @Component, @Repository @Service, @Controller


Description

@Controller control layer, that's our action layer.
@Service The business logic layer, which is our service or manager layer
@Repository persistence layer, which is what we often call the DAO layer
@Component (literally a component), it is used when you can't decide which layer to use.
In fact, the effects of these four annotations are the same, and spring will load them as a bean that needs to be injected into the context, but in the project, it is recommended that you use the project in strict accordance with the meaning of the remaining three annotations except Componen. This is good for the web architecture of the layered structure.

  

Example:

1. Control layer
@Controller//Comment as Controller
@Scope ("prototype")
public class Loginaction {
   @Autowired
   @Qualifier ("UserService")//Note Specify the injected Bean
   Private Iuserservice UserService;
@RequestMapping (value = "/login/{username}", method = Requestmethod.get)
Public Modelandview MyMethod (httpservletrequest request, httpservletresponse response,
@PathVariable ("username") String username, Modelmap modelmap) throws Exception {
Modelmap.put ("Loginuser", username);
return new Modelandview ("/login", Modelmap);
}

@RequestMapping (value = "/welcome", method = Requestmethod.get)
Public String registpost (user user) {
return "/welcome";
}
}


Parameters:
Username=xxx&password=yyy
----------------
Class user{
Private String UserName;
private String password;
}


(1) Loginaction uses the @scope annotation, @Scope ("prototype") that declares the scope of the action as a prototype, and can take advantage of the container's scope= "prototype" to ensure that each request has a separate action to handle, Avoid thread-safety issues with action in struts. Spring default scope is a singleton mode (scope= "singleton"), so that only one action object is created, each access is the same action object, the data is unsafe, and the STRUTS2 requires a different action for each access. Scope= "Prototype" ensures that an action object is created when a request is made.
(2) The @PathVariable ("user") String user is the acquisition of a variable in the URL.
(3) Common annotations @controller, @RequestMapping, @RequestParam, @PathVariable, @CookieValue
(4) Common parameter types
1). HttpServletRequest, HttpServletResponse, or httpsession.
2). Any type of request parameter with @requestparam annotations added
3). Any type of model property with @modelattribute annotations added
4). Any type of command object, for spring to bind request parameters
5). Map or Modelmap, for handler methods to add attributes to the model
6). Errors or Bindingresult, let the handler method access the binding and validation results of the Command object
7). Sessionstatus, let the handler method issue a notification that the session processing has completed
(5) Common return value types
The return type of the handler method can be Modelandview, Model, Map, String, void


2. Business Logic Layer
@Service ("UserService")
public class Userserviceimpl implements Iuserservice {
   @Autowired
   @Qualifier ("Userdao")
   Private Iuserdao Userdao;
   ...
}
(1) @Autowired exposed required attribute, which is an important attribute that bears the task of dependency checking. By default, all properties and methods that apply the @autowired annotation must find the appropriate collaborator, otherwise the Di container throws an exception, and by adjusting the value of the required property can change the behavior, and when the required property of the @autowired annotation is set to False, Exceptions are never thrown even if a suitable collaborator is not found.
(2) @Autowired annotations can act on builders, properties, methods. The method here is not limited to the set value method, that is, setter method, the common methods can be applied to this annotation.


3. Persistence Layer
@Repository ("Userdao")
public class Userdaoimpl implements Iuserdao {
   private static Logger Logger = Loggerfactory.getlogger (Userdaoimpl.class);
   Private DataSource DataSource;
   private JdbcTemplate template;
   @Autowired
   Public Userdaoimpl (DataSource DataSource) {
   This.datasource= DataSource;
   Template = new JdbcTemplate (This.datasource);
}


4. Persistence layer
@Component ("Component")
public class Testcompoment {
@Autowired
Private ApplicationContext CTX;
Public ApplicationContext Getctx () {
return CTX;
}
}
(1) Use the @component annotation on the class to indicate that the class is defined as a spring management bean, using the default value (optional) attribute to represent the bean identifier.
(2) This is a common form of all spring management components, @Component annotations can be placed on the head of a class, @Component deprecated.



SPRINGMVC Note Controller Details

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.