今天主要花时间学习了SpringMVC相关知识,并创建了一个练习项目,实践操作Springmvc
package com.aurora.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { //handler -> springmvc/hello return "hello spring mvc!" @RequestMapping("springmvc/hello") //对外访问的地址,到handlerMapping注册的注解 @ResponseBody //直接返回字符串给前端,不要找视图解析器 public String hello(){ System.out.println("HelloController.hello"); //返回给前端 return "hello springmvc"; } }
package com.aurora.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; //可以被web项目加载,会初始化ioc容器,会设置dispatcherServlet的地址 public class SpringMvcInit extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[0]; } //设置我们项目对应的配置类 @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{MvcConfig.class}; } //配置SpringMVC内部自带Servlet 的访问地址 @Override protected String[] getServletMappings() { return new String[]{ "/" }; } }
package com.aurora.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { //handler -> springmvc/hello return "hello spring mvc!" @RequestMapping("springmvc/hello") //对外访问的地址,到handlerMapping注册的注解 @ResponseBody //直接返回字符串给前端,不要找视图解析器 public String hello(){ System.out.println("HelloController.hello"); //返回给前端 return "hello springmvc"; } }
标签:总结,web,return,org,springframework,import,今日,hello From: https://www.cnblogs.com/rsy-bxf150/p/17780843.html