SpringMVC annotation controller, springmvc annotation Controller

Source: Internet
Author: User

SpringMVC annotation controller, springmvc annotation Controller
There are four main types: @ Component, @ Repository @ Service, and @ Controller.


Note:

@ Controller control layer: Our action Layer
@ Service business logic layer: Our service or manager layer
@ Repository: The persistence layer is the DAO layer.
@ Component (literally a Component), which is used when you cannot determine which layer to use.
In fact, the effects of these four annotations are the same. Spring loads them as beans to be injected in the context. However, in the project, however, we recommend that you strictly follow the meaning of the three annotations except Componen to use them in the project. This is good for the Layered web architecture.

  

Example:

1. Control Layer
@ Controller // comments as controller
@ Scope ("prototype ")
Public class LoginAction {
@ Autowired
@ Qualifier ("userService") // annotation specifies the Bean to be injected
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") indicates that the Action range is declared as a prototype, the scope = "prototype" of the container can be used to ensure that each request has a separate Action for processing, avoiding the thread security issue of the Action in struts. By default, scope in spring is a singleton mode (scope = "singleton"). In this way, only one Action object is created, and each access is the same Action object, causing insecure data, struts2 requires that each access correspond to a different Action. scope = "prototype" ensures that an Action object is created when there is a request.
(2) @ PathVariable ("user") String user is used to obtain the variable in the url.
(3) Common Annotations: @ Controller, @ RequestMapping, @ RequestParam, @ PathVariable, and @ CookieValue
(4) Common parameter types
1). HttpServletRequest, HttpServletResponse, or HttpSession.
2) added any type of request parameters annotated with @ RequestParam
3) added any type of model attributes annotated with @ ModelAttribute.
4). Any type of command object for Spring to Bind Request Parameters
5). Map or ModelMap for processing program methods to add attributes to the model
6). Errors or BindingResult to allow the handler method to access the binding and verification results of the command object
7). SessionStatus, so that the handler method can send a notification that the session processing has been completed.
(5) common return value types
The return type of the handler method can be ModelAndView, Model, Map, String, and void.


2. Business logic layer
@ Service ("userService ")
Public class UserServiceImpl implements IUserService {
@ Autowired
@ Qualifier ("userDao ")
Private IUserDao userDao;
...
}
(1) @ Autowired exposes the required attribute, which is an important attribute for undertaking dependency check tasks. By default, all attributes and methods that have the @ Autowired annotation applied must find the appropriate collaborators. Otherwise, the DI container will throw an exception. by adjusting the required attribute value, this line can be changed, when the required attribute of the @ Autowired annotation is set to false, the exception will not be thrown even if the appropriate collaborators are not found.
(2) @ Autowired annotation can act on the builder, attributes, and methods. The method here is not limited to the value setting method, that is, the setter method. This annotation can be applied to all common methods.


3. Persistence Layer
@ Repository ("userDao ")
Public class UserDaoImpl implements IUserDao {
Private static Logger logger = LoggerFactory. getLogger (UserDaoImpl. class );
Private 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 define the class as Spring management Bean, and use the default value (optional) attribute to represent the Bean identifier.
(2) This is a common form of all Spring management components. The @ Component annotation can be placed on the class head, And @ Component is not recommended.



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.