发表于: 2016-12-21 23:15:57
2 1860
今天完成的事情:
springmvc快速入门(注解版本)
1)springmvc快速入门(传统版)
步一:创建springmvc-day02这么一个web应用
步二:导入springioc,springweb和springmvc相关的jar包
步三:在/WEB-INF/下创建web.xml文件
步四:创建HelloAction.java控制器类
@Controller public class HelloAction{ @RequestMapping(value="/hello") public String helloMethod(Model model) throws Exception{ System.out.println("HelloAction::helloMethod()"); model.addAttribute("message","这是我的第二个springmvc应用程序"); return "/success.jsp"; } } |
步五:在/WebRoot/下创建success.jsp
步六:在/src/目录下创建spring.xml配置文件
步七:部署web应用到tomcat中,通过浏览器访问如下URL:
一个Action中,可以写多个类似的业务控制方法
1)通过模块根路径 + 功能子路径 = 访问模块下子功能的路径
@Controller @RequestMapping(value="/user") public class UserAction{ @RequestMapping(value="/add") public String add(Model model) throws Exception{ System.out.println("HelloAction::add()"); model.addAttribute("message","增加用户"); return "/success.jsp"; } @RequestMapping(value="/find") public String find(Model model) throws Exception{ System.out.println("HelloAction::find()"); model.addAttribute("message","查询用户"); return "/success.jsp"; } } |
在业务控制方法中写入模型变量收集参数,且使用@InitBind来解决字符串转日期类型
1) 在默认情况下,springmvc不能将String类型转成java.util.Date类型,所有我们只能在Action中自定义类型转换器
@Controller @RequestMapping(value = "/user") public class UserAction { @InitBinder protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception { binder.registerCustomEditor( Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true)); } @RequestMapping(value = "/add", method = RequestMethod.POST) public String add(int id, String name, double sal, Date hiredate, Model model) throws Exception { System.out.println("HelloAction::add()::POST"); model.addAttribute("id", id); model.addAttribute("name", name); model.addAttribute("sal", sal); model.addAttribute("hiredate", hiredate); return "/register.jsp"; } } |
明天计划的事情:
继续看springmvc
评论