diff --git "a/48 \351\251\254\345\256\217\350\276\276/1\346\234\2102\345\217\267.md" "b/48 \351\251\254\345\256\217\350\276\276/1\346\234\2102\345\217\267.md" new file mode 100644 index 0000000000000000000000000000000000000000..c68a02100872774b9017574692de9a1cc75551a3 --- /dev/null +++ "b/48 \351\251\254\345\256\217\350\276\276/1\346\234\2102\345\217\267.md" @@ -0,0 +1,76 @@ +SpringMvcConfig + +```java +package com.mdd.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.mdd.controller") +public class SpringMvcConfig { +} + +``` + +UserController + +```java +package com.mdd; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class UserController { + @RequestMapping("/save") + @ResponseBody + public String save(){ + System.out.println("666"); + return "{'info':'springmvc'}"; + } +} + +``` + +WebInitConfig + +```java +package com.mdd.config; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.filter.CharacterEncodingFilter; +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; +import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer; + +import javax.servlet.Filter; + +public class WebInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer { + + @Override + protected Class[] getRootConfigClasses() { + return new Class[0]; + } + + @Override + protected Class[] getServletConfigClasses() { + return new Class[]{SpringMvcConfig.class}; + } + + @Override + protected String[] getServletMappings() { + return new String[]{"/"}; + } + + @Override + protected Filter[] getServletFilters() { + CharacterEncodingFilter filter = new CharacterEncodingFilter(); + filter.setEncoding("utf-8"); + return new Filter[]{filter}; + } +} + +```