了解:
javaEE体系结构:
应用层:application;
web层:struts2,springMVC;
业务层:spring;
持久层:Hibernate,mybatis;
1.SpringMVC简介:
SpringMVC是javaEE的web层MVC框架。类似Struts2
2.什么事MVC:
MVC是一种设计模式,M(模型model)、V(试图view)、C(控制controlor);--解耦和
3.springmvc 执行流程
4.springmvc 入门程序
4.1创建一个DynamicwebProject
4.2导入jar包
4.3 配置web.xml 文件
4.3.1 配置前端控制器
<!-- 配置SpringMVC前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 默认加载方式
默认加载必须规范:
* 文件命名:servlet-name-servlet.xml====springmvc-servlet.xml
* 路径规范:必须在WEB-INF目录下面
-->
<!-- <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param> -->
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
4.4 配置springmvc-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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 配置处理器映射器,springmvc默认的处理器映射器
BeanNameUrlHandlerMapping:根据bean(自定义Controler)的name属性的url去寻找hanler(Action:Controller)
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 配置处理器适配器执行Controlelr ,springmvc默认的
SimpleControllerHandlerAdapter:执行Controller
-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 配置视图解析器 -->
<bean id="viewResover" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置自定义控制器 -->
<bean name="/cccController.do" class="com.zit.controller.UserController"></bean>
</beans>
4.5 自定义Controller
package com.zit.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class UserController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
ModelAndView mav = new ModelAndView();
mav.addObject(""
+ "username", "欧阳夏丹");
mav.addObject("userinfo", "springmvc从入门到精通!");
System.out.println("****************************");
mav.setViewName("index");
return mav;
}
}
4.6定义视图页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>用户名:</h1>
<a><h1>${username}</h1></a>
</body>
</html>
5.名词解释
5.1 处理器映射器
BeanNameUrlHandlerMapping
功能:寻找Controller
根据url请求去匹配bean的name属性url,从而获取Controller
SimpleUrlHandlerMaping
功能:寻找Controller
根据浏览器url匹配简单url的key,key又Controller的id找到Controller
ControllerClassNameHandlerMapping
功能:寻找Controller
根据类名(MyController)类名.do来访问,类名首字母小写
5.2 处理器适配器
SimpleControllerHandlerAdapter
功能:执行controller
调用controller里面方法,返回modelAndView。
HttpRequestHandlerAdapter
功能:执行controller
6.中文乱码解决
6.1Get请求乱码
6.2post乱码
Spring编码过滤器:在web.xml配置
<!-- post请求乱码解决 -->
<!-- post请求乱码解决 -->
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
7.时间类型转换
/**
* 时间转换--spring mvc 自己的方法
*/
@Override
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
String str =request.getParameter("birthday");
if(str.contains("/")) {
binder.registerCustomEditor(Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy/MM/dd"), true));
}else if(str.contains("-")) {
binder.registerCustomEditor(Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}
}