---------------------------------------Spring MVC---------------------------------
1、mvc设计模式的介绍
M(model):模型层,
数据模型:pojo
业务模型:service、mapper
V(view):视图层,jsp和html,负责和用户交互
C(controller):控制层,servlet和controller,负责接受请求和响应
2、springmvc的介绍
springmvc是spring框架的一个模块,是一个基于mvc设计模式的web框架,本质上相当于servlet
1.index.jsp
<a href="/hello">hello</a>
2.controller
@Controller
public class HelloControler{
@RequestMapping("/hello")
public ModelAndView hello(){
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "师姐你好");
mv.setViewName("success");
return mv;
}
}
3、配置springmvc要扫描的包
<context:component-scan base-package="com.by.controller"></context:component-scan>
3、配置视图解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--jsp的目录-->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!--jsp的扩展名-->
<property name="suffix" value=".jsp"></property>
</bean>
5、web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<!--前端控制器-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--tomcat启动就创建加载DispatcherServlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--/只能拦截路径,而 / *能够拦截路径和页面-->
<url-pattern>/</url-pattern>
</servlet-mapping>
DispatcherServlet(前端控制器):扫描controller,接受请求
HandlerMapping(处理器映射器):查找handler
HandlerAdaptor(处理器适配器):调用handler
ViewResolver(视图解析器):解析jsp的具体路径
view(视图渲染器):把数据填充到request,跳转页面
1、value属性:设置url,窄化路径
<a href="/一级路径/二级路径">
@RequestMapping("/一级路径")
public class AccountController{
@RequestMapping("/二级路径")
public ModelAndView findAccount(){
}
}
2、method属性:设置请求方式
@RequestMapping(value="/请求路径", method=RequestMethod.Get|Post|Put|Delete)
@GetMapping("/请求路径")
@PutMapping("/请求路径")
1、返回ModelAndView
public ModelAndView findAccount(ModelAndView mv){
mv.addObject("", "");//request.setAttribute("", "");
mv.setViewName("jsp名称");
return mv;
}
2、返回字符串
1)返回视图名
public String findAccount(Model model){
model.addAttribute("", "");//request.setAttribute("", "");
return "success";
}
2)重定向(1.地址栏会变 2.数据丢失)
return "redirect:/请求路径";
3)请求转发(1.地址栏不会变 2.数据不丢失)
return "forward:/请求路径";
1、基本类型(少量)
<a href="/account/findAccount?id=123">
public String findAccount(Integer id){
return "success";
}
2、pojo类型(多个)
<a href="/account/findAccount?id=123&name=tomcat=&money=100">
public String findAccount(Account account){
return "success";
}
3、restful
<a href="/account/findAccount/123">
@RequestMapping("/findAccount/{id}")
public String findAccount(@PathVariable Integer id){
return "success";
}
4、json类型
$.ajax({
type:"post",
url:"/account/findAccount",
contentType:"application/json;charset=UTF-8",
data:'{"id":1,"name":"张二狗","money":999.0}',
success:function(data){
//{id:2, name:铁柱, money:100}
alert(data.name);
}
})
@RequestMapping("/findAccount")
@ResponseBody
public Account findAccount(@RequestBody Account account){
return account;
}
5、日期类型转换器
public class DateConverter implements Converter<String, Date> {
}
<!--注册转换器工厂-->
<mvc:annotation-driven conversion-service="cs"></mvc:annotation-driven>
<!--配置类型转换器工厂-->
<bean id="cs" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.by.converter.DateConverter"></bean>
</set>
</property>
</bean>
6、中文乱码
1)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>
2)get
new String(name.getBytes(), "UTF-8");
7、servlet API接收参数
public String findAccount(HttpServletRequest request){
String name = request.getParameter("name");
}
1、ModelAndView
public ModelAndView findAccount(ModelAndView mv) {
mv.addObject("msg", "欢迎你 springmvc");
mv.setViewName("success");
return mv;
}
2、Model
public String findAccount(Model model) {
model.addAttribute("msg", "欢迎你 springmvc");
return "success";
}
3、json
@ResponseBody
public Account findAccount(){
}
4、servlet API数据传递
public String findAccount(HttpServletRequest request){
request.setAttribute("key", "value")
}
1、pom.xml
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
2、配置上传解析器
<!--配置文件上传解析器-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5242880" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
3、form
<form action="/account/upload" method="post" enctype="multipart/form-data">
图片:<input type="file" name="img"><br>
<input type="submit" value="上传">
</form>
3、文件上传
@RequestMapping("/upload")
public String upload(MultipartFile img) throws IOException {
//1、获得tomcat的路径
//2、创建uploads目录
//3、上传文件
}
@Component
public class GlobalExceptionHandler implements HandlerExceptionResolver {
/**
1.发邮件、发短信
2.跳转错误页面
*/
}
1、什么是拦截器?
拦截器(interceptor):类似于filter,用于拦截用户请求,基于aop实现
2、filter和interceptor的区别?
依赖 原理
filter 无依赖 方法回调
interceptor 依赖于spring aop
3、拦截器入门
1、interceptor
public class LoginInterceptor implements HandlerInterceptor {
...
}
2、springmvc.xml
<!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<!--匹配所有url:/user/register-->
<mvc:mapping path="/**"/>
<!--排除/usr/*-->
<mvc:exclude-mapping path="/user/login"/>
<mvc:exclude-mapping path="/user/toRegister"/>
<mvc:exclude-mapping path="/user/register"/>
<bean class="com.by.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>