3.定义Controller类4.0.0 com.wym springmvc_01_quickStart 1.0-SNAPSHOT war UTF-8 1.8 1.8 javax.servlet javax.servlet-api 3.1.0 provided org.springframework spring-webmvc 5.2.10.RELEASE org.apache.tomcat.maven tomcat7-maven-plugin 2.1 8080 /
@Controller public class UserController { //2.2、设置当前操作的访问路径 @RequestMapping("/save") //2.3、设置当前操作的返回值 @ResponseBody public String save(){ System.out.println("user save~"); return "{'name':'Helena'}"; } }4. 创建SpringMVC的配置文件
//创建springmvc的配置文件,加载controller对应的bean @Configuration @ComponentScan("com.wym.controller") public class SpringMvcConfig { }5. 定义Servlet容器启动的配置类
//定义一个servlet容器启动的配置类,在里面加载spring的配置 public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer { //加载SpringMVC容器配置 @Override protected WebApplicationContext createServletApplicationContext() { AnnotationConfigWebApplicationContext act = new AnnotationConfigWebApplicationContext(); act.register(SpringMvcConfig.class); return act; } //设置那些请求归属SpringMVC处理 @Override protected String[] getServletMappings() { return new String[]{"/"}; } //加载spring容器配置 @Override protected WebApplicationContext createRootApplicationContext() { return null; } }6. 配置 7. 启动后,在浏览器上输入地址,访问到save()方法的返回值;控制台输出相应内容。 Done!