Spring MVC basics

  Web project development process are generally using MVC (Model-View-Controller) pattern, earlier Struts2 to Spring MVC, to the current Spring Boot, are similar thinking

road. Spring Boot with more really want to go back SSM or SSH framework (although the company still using Spring MVC), wrote this essay, be your own review for Spring MVC contents of it.

work process

 

DispatchServlet of configuration

1、web.xml

2, by being arranged in Servlet Container (Tomcat7 + Version)

/ ** 
 * AbstractAnnotationConfigDispatcherServletInitializer expansion class and automatically configures DispatchServlet 
 * Spring ApplicationContext (Servlet context in the context of the application is in the application) 
 * / 
public  class SpittrWebAppInitializer the extends AbstractAnnotationConfigDispatcherServletInitializer { 

    / ** 
     * returns the class with annotation used @Configuration ContextLoaderListener application context definition created in the bean 
     * @return 
     * / 
    @Override 
    protected <?> class [] getRootConfigClasses () {
         return  new new class [] {RootConfig. class }; 
    } 

    / ** 
     * returns annotated with @Configuration class is used to define the context of the application created in the bean DispatchServlet
     * @Return 
     * / 
    @Override 
    protected <?> Class [] getServletConfigClasses () {
         return  new new Class [] {WebConfig. Class }; 
    } 

    / ** 
     * mapped onto one or more paths DispatchServlet. "/" Indicates the default application Servlet, will manage all requests 
     * @return 
     * / 
    @Override 
    protected String [] getServletMappings () {
         return  new new String [] { "/" }; 
    } 
}
Configuration DispatchServlet
public  class WebConfig   the extends WebMvcConfigurerAdapter {
 // generally comprise parser configuration view
 // static resource processing
 // . . . . . 
}
WebConfig

DispatchServlet startup, created Spring application context loaded statement bean, Spring Web applications, usually there is another application context by

Creating ContextLoaderListener

DispatchServlet: loading bean contains Web components, such as controllers, view resolver, mapping processor

ContextLoaderListener: loading applications other bean, rear drive applications are generally intermediate layer and the data layer assembly

Common Annotations

1, @ EnableWebMvc: open class configuration support in the configuration of the Web MVC, as some ViewResolver or MessageConverter, which does not use notes, need to be rewritten

WebMvcConfigurerAdapter of a method for configuring SpringMVC.

2、@RequestParam:http://host:port/path?id=1001

public String test1(@RequestParam("id") String id)

3、@PathVariable:http://host:port/path/1001

public String test2(@PathVariable("id") String id) 

4, form validation: all packets inside the javax.validation.constraints

public class Login {
    @NotNull
    @IsMobile
    private String mobile;
    
    @NotNull
    @Length(min=32)
    private String password;
}
public Result<String> doLogin(HttpServletResponse response, @Valid LoginVo loginVo) {
    log.info(loginVo.toString());
    //登录
    String token = userService.login(response, loginVo);
    return Result.success(token);
}

5、@ResponseBody:支持将返回值放在response内,而不是一个页面,通常用户返回json数据

@ResponseBody
public String test3() {

    return "id";//这时候返回到页面的是String,而不是解析到id.html等类似的页面
}

6、@RequestBody:允许request的参数在request体中,而不是在直接连接在地址后面。

7、@RestController:该注解为一个组合注解,相当于@Controller和@ResponseBody的组合,注解在类上,该Controller的所有方法都默认加上了@ResponseBody。

@Controller
@ResponseBody
public @interface RestController {
    String value() default "";
}

8、@ModelAttribute:绑定键值对到Model里

先写到这,同学来了,去喝酒了。。。。

 

Guess you like

Origin www.cnblogs.com/huigelaile/p/10963320.html