Getting started using annotations Spring -MVC

Earlier learned to use a configuration file to complete SpringMVC, but with the controller to write more and more, the code will look very bloated, using annotations to avoid this situation, make the code even more concise.

Programming steps

Preparation is pom.xml guide package Spring-webmvc , the deployment descriptor file web.xml configuration DispatcherServlet, under the resources directory Add Spring xml configuration files needed to start , keep this article makes no difference, programming, note the point as follows:

 1 Model Controller interface classes are no longer implemented

 2 Model class does not implement the Controller interface, the method name is not required to do, and not necessarily before handlerRequest. In addition to the return type can be ModleAndView, may also be of type String

 3 Model class can add multiple methods

 4 Model class uses @Controller annotation, bean element need not be configured in the configuration file

 5 may be added before @RequestMapping prior methods or a class, the equivalent effect handlerMapping

 6 in the configuration file, adding ViewResolver configuration, the scanning components, the scan added MVC annotation (identification @RequestMapping)

Implementation Notes using SpringMVC

Guide package

1   <dependency>
2   <groupId>org.springframework</groupId>
3   <artifactId>spring-webmvc</artifactId>
4   <version>4.2.3.RELEASE</version>
5   </dependency>

web.xml configuration DispatcherServlet

 1 <!-- 配置DispatcherServlet 使用注解完成springMVC -->
 2   <servlet>
 3     <servlet-name>myspringmvc</servlet-name>
 4     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 5     <init-param>
 6         <param-name>contextConfigLocation</param-name>
 7         <param-value>classpath:my-spring-mvc.xml</param-value>
 8     </init-param>
 9     <!-- 设置tomcat启动就开启Spring -->
10     <load-on-startup>1</load-on-startup>
11   </servlet>
12   <servlet-mapping>
13     <servlet-name>myspringmvc</servlet-name>
14     <url-pattern>*.get</url-pattern>
15   </servlet-mapping>

Xml configuration file spring start reading

Assembly needs to be configured inside the scan, the scanning bags into Spring container management, need to configure the drive annotation, finally configure view resolver.

1  <! - Configuration component scans -> 
2     < context: Scan-Component Base-Package = "com.boe" > </ context: Component-Scan >     
. 3     <! - Configure Scan MVC annotation -> 
4     < MVC: Annotation-Driven > </ MVC: Annotation-Driven > 
. 5     <-! configuration view resolver -> 
. 6      < the bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > 
. 7             < Property name = "prefix" value = "/ the WEB-INF /" />
 8            <Property name = "suffix" value =. "JSP"  /> 
. 9             <-! prefix and suffix, and hello view name may be mapped to the above configuration /WEB-INF/hello.jsp -> 
10      </ the bean > 

Write a Model

Model @Controller annotations in use, it is representative of a Controller, also a need to make the request to the corresponding Controller, where @RequestMapping annotations added directly in to complete the method.

. 1  Package com.boe;
 2  
. 3  Import org.springframework.stereotype.Controller;
 . 4  Import org.springframework.web.bind.annotation.RequestMapping;
 . 5  Import org.springframework.web.servlet.ModelAndView;
 . 6  
. 7  / ** 
. 8  * testing using a complete annotation helloSpringMVC
 . 9  * @author yangchaolin
 10   * / 
. 11  @Controller
 12 is @RequestMapping ( "/ Demo" )
 13 is  public  class HelloController {
 14      // 1 
15     @RequestMapping("/hello.get")
16     public String hello() {
17         System.out.println("返回一个String");
18         return "hello";
19     }
20     //方法2
21     @RequestMapping("/helloModelAndView.get")
22     public ModelAndView helloModelAndView() {
23         System.out.println("返回ModelAndView");
24         return new ModelAndView("hello");
25     }
26 }

Write JSP

Slightly, jsp content reference Bowen.

Test Results

Right-click on the selected item to start the project on server, and then access the Model in the written request path, pay attention to the front of the class also added a @RequestMapping, so after a full path name and a project name / demo, without directly if the project name after /hello.get access.

Console case, it can be seen in the method returns String Model and ModelAndView.

SpringMVC above is used to complete the annotation process, which does not use the code annotation Model reduced compared to reduce the spring-mvc HandlerMapping configuration and reduce the corresponding Model bean configuration.

 

Reference Hirofumi

(1) https://www.cnblogs.com/youngchaolin/p/11347771.html

Guess you like

Origin www.cnblogs.com/youngchaolin/p/11349493.html