spring mvc (three) annotation

The annotation implements the Control, service, and dao of spring mvc.

1. Annotate the Controller
Create an annotation Controller class.
package com.sunbin.test.testSpring.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;

@Controller
public class AnnonationController {
	@RequestMapping("/annonation")
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("message", "annonation");
		modelAndView.setViewName("helloWorld");
		return modelAndView;
	}

}

The @Controller annotation indicates that it is a controller class.
The @RequestMapping("/annonation") annotation can be used on classes and methods, indicating that the class or method should be bound to the /annonation path under the servlet.

Add the following configuration to root-context.xml to support spring annotation, scan and mvc annotation support, and default parser handler.
<!-- Annotation declaration, can be omitted after using context:component-scan -->
<context:annotation-config />
<!-- Enable controller annotation support-->
<context:component-scan base-package="com.sunbin"></context:component-scan>
<!-- Default annotation mapping support -->
<mvc:annotation-driven/>
<!-- When the static resources to be accessed above are not included in the above configuration, they are accessed according to this configuration-->
<mvc:default-servlet-handler />

After deployment, visit http://localhost:8080/testSpringWeb/annonation to see the result:
annonation

2. Annotate service and dao
according to the test steps in http://sb33060418.iteye.com/blog/2372867 and create them in the same package Interface classes TestService, TestDao and implementation classes TestServiceAnnoImpl, TestDaoAnnoImpl.

Create a new TestController.
package com.sunbin.test.testSpring.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.sunbin.test.testSpring.service.TestService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


@RequestMapping(value = "/test")
public class TestController {
	@Autowired
	public TestService testService;

	@RequestMapping(value = "/test")
	public ModelAndView test(HttpServletRequest request,
			HttpServletResponse reponse) throws Exception {
		// TODO Auto-generated method stub
		String param = request.getParameter("param");
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("message", testService.test(param));
		modelAndView.setViewName("helloWorld");
		return modelAndView;
	}

}

Because of the annotations such as @Controller, @Service, @Repository, @Autowired, etc. in the class, spring will create beans and inject them automatically.
The address of the @RequestMapping(value = "/test") annotation on the method will be intercepted by /test after the class annotation /test.

After redeploying, visit http://localhost:8080/testSpringWeb/test/test?param=anno to see the result:
testServiceImpl.test:testDaoImpl.test:anno
is not an annotation service or dao implementation.
Because the TestServiceImpl and TestDaoImpl configured by the xml file are injected into the controller by the byName method.

Comment out the configuration of service and dao introduced in root-context.xml
<!--     <import resource="services.xml"/>
    <import resource="daos.xml"/> -->

After redeploying, visit the test address to see the expected result:
testServiceAnnoImpl.test:testDaoAnnoImpl.test:anno

3. Controller binds multiple addresses
In practical applications, multiple resources under a module are often placed in one Implemented in the Controller class to share the same service, variable definitions, etc. This requires binding different addresses for different methods of a single Controller.

Add a method to TestController:
	@RequestMapping(value = "/index")
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("message", "test/index");
		modelAndView.setViewName("helloWorld");
		return modelAndView;
	}

The @RequestMapping(value = "/index") annotation on the method will bind the /index address to the class address /test and intercept the /test/index request.
After redeploying, visit http://localhost:8080/testSpringWeb/test/index and the previous /test/test to see different results.
Because the annotation implements the Controller function, the class no longer needs to implement the Controller interface, and therefore no longer needs to implement the handleRequest abstract method in the interface, and other method names can be used.

4. Specify the supported request method
@RequestMapping(value = "/index") by default only supports get method access. If you use post, put and other methods to access, an error will be reported:
405 Method Not Allowed
Need to configure support for different request methods, modify The notes are as follows:
	@RequestMapping(value = "/index", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		...

This method can be accessed through get and post methods. The put and delete methods can also be configured to implement restful-style services.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326655095&siteId=291194637