springmvc入门一
第一章回顾javaweb中的MVC设计模式
1)MVC这种设计模式,不光运用于Web领域,而且也能用于非Web领域
2)今天说的MVC特指一种表现层设计模式,不限于Java语言
第二章回顾struts2+spring+jdbc+oracle开发
1)需求:学生注册
2)struts2框架有如下特点:
每次请求action时,都创建action实例,多例/原型
action类一成不变的直接或间接继续ActionSupport类
action类中的业务控制方法总是相类似的签名且无参
action类中,接收参数要用实例变量和对应的set方法或set/get方法
struts.xml配置文件,必须以struts.xml命名,且放在src目录下
.. .. ..
第三章什么是springmvc,它与spring有什么关系
springmvc属于spring框架的后续产品,用在基于MVC的表现层开发,类似于struts2框架
参见<<springmvc与spring的关系.JPG>>
第四章初识springmvc工作流程
参见<<初识springmvc工作流程.JPG>>
第五章 springmvc快速入门(XML版本)
1)springmvc快速入门(传统版)
步一:创建javaee-springmvc-day01这么一个web应用
步二:导入springioc,springweb , springmvc相关的jar包
步三:在/WEB-INF/下创建web.xml文件
<!-- 注册springmvc框架核心控制器 --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> |
步四:创建HelloAction.java控制器类
/** * 控制器 */ public class HelloAction implements Controller{ /** * 业务方法 */ public ModelAndView handleRequest(HttpServletRequest requqest,HttpServletResponse response) throws Exception { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message","这是我的第一个springmvc应用程序"); modelAndView.setViewName("/jsp/success.jsp"); return modelAndView; } } |
步五:在/WebRoot/下创建jsp/success.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>这是我的第一个springmvc应用程序</title> </head> <body> success.jsp<br/> ${message} </body> </html> |
步六:在/WEB-INF/创建DispatcherServlet-servlet.xml配置文件,xml头部信息与spring.xml相同
注意:该配置文件的命名规则:web.xml文件中配置的<servlet-name>的值-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 控制器 --> <bean name="/hello.action" class="cn.itcast.javaee.springmvc.app05.HelloAction"> </bean> <!-- 映射器 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> </bean> <!-- 适配器 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"> </bean> <!-- 解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> </bean> </beans> |
步七:部署web应用到tomcat中,通过浏览器访问如下URL:
http://127.0.0.1:8080/javaee-springmvc-day01/hello.action
第六章加载自定义目录下的springmvc.xml配置文件
在默认情况下:springmvc框架的配置文件必须叫<servlet-name>-servlet.xml
且必须放在/WEB-INF/目录下,我们可以在web.xml文件中,为DispatcherServlet配置一个初始化参数,
让它去我们指定的目录下加载springmvc.xml配置文件,web.xml代码如下:
<!-- 注册springmvc框架核心控制器 --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/cn/itcast/javaee/springmvc/config/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> |
如果springmvc.xml配置文件放在src目录下,web.xml代码如下:
<!-- 注册springmvc框架核心控制器 --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> |
第七章解析器InternalResourceViewResolver
ModelAndView对象中即可以封装真实视图路径名,也可以封装视图路径的逻辑名,springmvc.xml
代码如下:
<!-- 解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean> |
Action类代码如下:
modelAndView.setViewName("success"); |
注意:如果ModelAndView对象中封装的是真实视图的路径名,那么视图解析器的配置可省
第八章映射器BeanNameUrlHandlerMapping
1) org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping(核心)
将程序员定义的Action所对应的<bean>标签的name属性作为请求路径
<!-- 注册控制器(程序员) --> <bean name="/add.action" class="cn.itcast.javaee.springmvc.mapping.UserAction"></bean>
<!-- 注册映射器(handler包)(框架) --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> |
注意:springmvc中,默认将<bean>标签的name属性作为请求路径,即BeanNameUrlHandlerMapping可省
2) org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
/delete.action和/update.action和/find.action请求路径都交由<bean>标签为id的Action,即
多个路径对应同一个Action
<!-- 控制器--> <bean name="/add.action" class="cn.itcast.javaee.springmvc.app02.UserAction"/> <bean name="/update.action" class="cn.itcast.javaee.springmvc.app02.UserAction"/> <bean name="/delete.action" class="cn.itcast.javaee.springmvc.app02.UserAction"/> <bean name="/find.action" class="cn.itcast.javaee.springmvc.app02.UserAction"/> |
<!-- 控制器--> <bean id="userActionID" class="cn.itcast.javaee.springmvc.app02.UserAction"/> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/add.action">userActionID</prop> <prop key="/update.action">userActionID</prop> <prop key="/delete.action">userActionID</prop> <prop key="/find.action">userActionID</prop> </props> </property> </bean> |
第九章适配器SimpleControllerHandlerAdapter
1) Action实现Controller接口
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
public class EmpAction implements Controller{ public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception { System.out.println("EmpAction::handleRequest"); request.setCharacterEncoding("UTF-8"); String username = request.getParameter("username"); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message",username); modelAndView.setViewName("/jsp/success.jsp"); return modelAndView; } } |
注意:springmvc中,默认适配器为SimpleControllerHandlerAdapte,即SimpleControllerHandlerAdapter可省
第十章控制器AbstractCommandController
1) org.springframework.web.servlet.mvc.ParameterizableViewController
如果请求是/hello.action的请求路径,则直接跳转到/jsp/success.jsp页面,不经过程序员定义的控制器Action
<bean name="/forward.action" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> <property name="viewName" value="/WEB-INF/forward.jsp"/> </bean> |
2) org.springframework.web.servlet.mvc.AbstractCommandController
能够以实体的形式,收集客户端参数
public class EmpAction extends AbstractCommandController{ public EmpAction(){ this.setCommandClass(Emp.class); } @Override protected ModelAndView handle( HttpServletRequest request, HttpServletResponse response, Object object, BindException bindException)throws Exception { request.setCharacterEncoding("UTF-8"); ModelAndView modelAndView = new ModelAndView(); if(object instanceof Emp){ Emp emp = (Emp) object; modelAndView.addObject("message","增加员工成功"); modelAndView.addObject("emp",emp); modelAndView.setViewName("/jsp/success.jsp"); } return modelAndView; } } |
<bean name="/add.action" class="cn.itcast.javaee.springmvc.app05.EmpAction"/> |
第十一章日期转换器和编码过滤器
1) 在默认情况下,springmvc不能将String类型转成Date类型,必须自定义类型转换器
<form action="${pageContext.request.contextPath}/add.action" method="POST"> <table border="2" align="center"> <tr> <th>姓名</th> <td><input type="text" name="username"/></td> </tr> <tr> <th>密码</th> <td><input type="password" name="password"/></td> </tr> <tr> <th>生日</th> <td><input type="text" name="birthday" value="2016-1-13"/></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="提交"/> </td> </tr> </table> </form> |
public class Emp { private String username; private String password; private Date birthday; |
public class EmpAction extends AbstractCommandController{ public EmpAction(){ System.out.println("EmpAction()"); this.setCommandClass(Emp.class); } @Override protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception { System.out.println("initBinder()"); binder.registerCustomEditor(java.util.Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true)); } @Override protected ModelAndView handle( HttpServletRequest request, HttpServletResponse response, Object object, BindException bindException)throws Exception { System.out.println("handle()"); ModelAndView modelAndView = new ModelAndView(); if(object instanceof Emp){ Emp emp = (Emp) object; modelAndView.addObject("message","增加员工成功"); modelAndView.addObject("emp",emp); modelAndView.setViewName("/jsp/success.jsp"); } return modelAndView; } } |
success.jsp<br/> ${requestScope.message}<br/> 姓名:${requestScope.emp.username}<br/> 密码:${requestScope.emp.password}<br/> 生日:<fmt:formatDate value="${requestScope.emp.birthday}" type="date" dateStyle="full"/><br/> |
2)spring提供的,专用于解决POST提交中文乱码问题,需要在web.xml文件中配置
<!-- 编码过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
第十二章 springmvc与struts2的区别
1)springmvc的入口是一个servlet,即前端控制器,例如:*.action
struts2入口是一个filter过虑器,即前端过滤器,例如:/*
2)springmvc是基于方法开发,传递参数是通过方法形参,可以设计为单例
struts2是基于类开发,传递参数是通过类的属性,只能设计为多例或原型
3)springmvc通过参数解析器是将request对象内容进行解析成方法形参,将响应数据和页面封装成
ModelAndView对象,最后又将模型数据通过request对象传输到页面
struts采用值栈存储请求和响应的数据,通过OGNL存取数据
.. …
第十三章员工管理系统--增加员工
技术:Springmvc + Spring/IOC + Jdbc + Oracle开发
步一:使用orcl数据库emp表
步二:创建Emp.java
public class Emp { private Integer empno; private String ename; private String job; private Integer mgr; private java.util.Date hiredate; private Integer sal; private Integer comm; private Integer deptno; public Emp(){} |
步三:创建JdbcUtil.java
public final class JdbcUtil { private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); public static ComboPooledDataSource getDataSource() { return dataSource; } } |
步四:创建EmpDao.java
public class EmpDao { public void add(Emp emp) throws Exception{ String sql = "insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values(?,?,?,?,?,?,?,?)"; QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource()); Object[] params = { emp.getEmpno(), emp.getEname(), emp.getJob(), emp.getMgr(), new java.sql.Date(emp.getHiredate().getTime()), emp.getSal(), emp.getComm(), emp.getDeptno() }; runner.update(sql,params); } |
步五:创建EmpService.java
public class EmpService { private EmpDao empDao; public void setEmpDao(EmpDao empDao) { this.empDao = empDao; System.out.println("注入EmpDao"); } public void register(Emp emp) throws Exception{ if(emp == null){ throw new Exception(); } empDao.add(emp); } |
步六:创建EmpAction.java
public class EmpAction extends AbstractCommandController{ public EmpAction() { this.setCommandClass(Emp.class); } @Override protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception { binder.registerCustomEditor( java.util.Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true)); } @Override protected ModelAndView handle(HttpServletRequest request,HttpServletResponse response, Object command, BindException errors)throws Exception { ModelAndView modelAndView = new ModelAndView(); Emp emp = (Emp)command; if(emp.getHiredate()==null){ emp.setHiredate(new java.util.Date()); } empService.register(emp); modelAndView.setViewName("/emp/success.jsp"); return modelAndView; } private EmpService empService; public void setEmpService(EmpService empService) { this.empService = empService; System.out.println("注入EmpService"); } } |
步七:创建add.jsp
<form action="${pageContext.request.contextPath}/add.action" method="POST"> <table border="2" align="center"> <tr> <th>编号</th> <td><input type="text" name="empno" value="3333"></td> </tr> <tr> <th>姓名</th> <td><input type="text" name="ename" value="SISI"></td> </tr> <tr> <th>职位</th> <td><input type="text" name="job" value="IT"></td> </tr> <tr> <th>上级编号</th> <td><input type="text" name="mgr" value="7788"></td> </tr> <tr> <th>入职时间</th> <td><input type="text" name="hiredate" value="2016-1-13"></td> </tr> <tr> <th>薪水</th> <td><input type="text" name="sal" value="7000"></td> </tr> <tr> <th>佣金</th> <td><input type="text" name="comm" value="3000"></td> </tr> <tr> <th>部门编号</th> <td><input type="text" name="deptno" value="40"></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="注册"> </td> </tr> </table> </form> |
步八:配置src/spring.xml
<bean id="empDaoID" class="cn.itcast.javaee.springmvc.emp.dao.EmpDao"> </bean>
<bean id="empServiceID" class="cn.itcast.javaee.springmvc.emp.service.EmpService"> <property name="empDao" ref="empDaoID"/> </bean>
<bean name="/add.action" class="cn.itcast.javaee.springmvc.emp.controller.EmpAction"> <property name="empService" ref="empServiceID"/> </bean> |
步九:配置WEB-INF/web.xml
<!-- springmvc核心控制器 --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
<!-- springmvc核心过滤器,专用于POST请求 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
步十:启动服务器,打开浏览器,输入:http://localhost:8080/javaee-springmvc-day01/emp/add.jsp