简介
1. 什么是springMVC?
Spring Web MVC是一种基于Java的实现了MVC设计模式的、请求驱动类型的、轻量级Web框架。
2. SpringMVC处理请求的流程
2.1 首先用户发送请求–>DispatherServlet
2.2 DispatcherServlet–>HandlerMapping
2.3 DispatcherServlet–>HandlerAdapter
2.4 HandlerAdapter–>处理器功能处理方法的调用
2.5 ModelAndView的逻辑视图名–>ViewRecolver
2.6 View–>渲染
2.7 返回控制权给DispatcherServlet,由DispatcherServlet返回呼应给用户,流程结束
3. SpringMVC核心开发步骤
3.1 DispatcherServlet在web.xml中的部署描述,从而拦截请求到springMVC
3.2 HandlerMapping的配置,从而将请求映射到处理器
3.3 HandlerAdapter的配置,从而支持多种类型的处理器
3.4 处理器(页面控制器)的配置,从而刊行功能处理
3.5 ViewResolver的配置,从而将逻辑视图名解析为具体的视图技术
4. SpringMVC的组件
4.1 前端控制器(DispatcherServlet)
4.2 请求到处理器映射(HandlerMapping)
4.3 处理器适配器(HandlerAdapter)
4.4 视图解析器(ViewResolver)
4.5 处理器或页面控制器(Controller)
4.6 验证器(Validator)
4.6 命令对象(Command 请求参数绑定到的对象就叫命令对象)
4.7 表单对象(Form Object提供给表单展示和提交到的对象就叫表单对象)
springmvc的一个搭建
1.配置相关pom.xml
tomcat 与Jsp之间的api文件
<!--tomcat-jsp-->
<tomcat-jsp-api.version>8.0.47</tomcat-jsp-api.version>
<!--tomcat与jsp的-->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat-jsp-api.version}</version>
</dependency>
自定义标签所可能用到的,比如PageTag,
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
这个下面不是加上,而是替换掉之前的spring-context
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
2.配置相关xml文件
把相关的路径文件修改好就行了,也就是爆红的改了
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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 通过context:component-scan元素扫描指定包下的控制器-->
<!--1) 扫描com.javaxl.zf及子子孙孙包下的控制器(扫描范围过大,耗时)-->
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com.thf"/>
<!--2) 此标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<!--两个bean,这两个bean是spring MVC为@Controllers分发请求所必须的。并提供了数据绑定支持,-->
<!--@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--3) ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- viewClass需要在pom中引入两个包:standard.jar and jstl.jar -->
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--4) 单独处理图片、样式、js等资源 -->
<!--<mvc:resources location="/css/" mapping="/css/**"/>-->
<!-- <mvc:resources location="/images/" mapping="/images/**"/>-->
<!--<mvc:resources location="/js/" mapping="/js/**"/>-->
<mvc:resources location="/static/" mapping="/static/**"/>
</beans>
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationCatext.xml</param-value>
</context-param>
<!-- 读取Spring上下文的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring和web项目集成end -->
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- 中文乱码处理 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--web.xml 3.0的新特性,是否支持异步-->
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring MVC servlet -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--此参数可以不配置,默认值为:/WEB-INF/springmvc-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<!--web.xml 3.0的新特性,是否支持异步-->
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
SpringMVC HelloWorld 代码实现
注解说明:
@Controller:用于标识处理器类
@RequestMapping: 请求到处理器功能方法的映射规则,可定义到类和方法
常用参数:value、method 。可将@RequestMapping标签定义到类名处窄化路径
@RequestParam: 请求参数到处理器功能处理方法的方法参数上的绑定常用参数:value、required、defaultValue
注:required设置成false的参数类型必须是引用类型,因为基本数据类型是不能为null的
@ModelAttribute: 请求参数到命令对象的绑定常用参数:value
(1)可用@ModelAttribute标注方法参数,方法参数会被添加到Model对象中(作用:向视图层传数据)
(2)可用@ModelAttribute标注一个非请求处理方法,此方法会在每次调用请求处理方法前被调用(作用:数据初始化)
(3)可用@ModelAttribute标注方法,方法返回值会被添加到Model对象中(作用:向视图层传数据) 但此方法视图的逻辑图就会根据请求路径解析,例如:a/test42 --> /WEB-INF/a/test42.jsp太麻烦几乎不用,不用直接保存到Model或ModelAndView中
@SessionAttributes:指定ModelMap中的哪些属性需要转存到session 常用参数:value、types
注1:必须放到class类名处
@InitBinder(本章暂不介绍):用于将请求参数转换到命令对象属性的对应类型
@RequestBody:用于目前比较流行的ajax开发的数据绑定(即提交数据的类型为json格式)
注1:使用@RequestBody注解的时候,前台的Content-Type必须要改为application/json,
如果没有更改,前台会报错415(Unsupported Media Type)。
后台日志就会报错Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported。
这些错误Eclipse下Tomcat是不会显示错误信息的,只有使用了日志才会显示
HelloController
package com.dengrenli.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
/**
* @author小科比
* @site www.dengrenli.com
* @company 科比公司
* @create 2019-10-25 15:10
*/
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("hello1")
public String hello1(){
System.out.println("hello 0000,科比加油");
return "hello 0000,科比加油";
}
@RequestMapping("hello2")
public String hello2(){
System.out.println("hello 0000,科比加油");
return "hello";
}
@ResponseBody
@RequestMapping("hello3")
public Map hello3(){
System.out.println("hello 0000,科比加油");
Map map=new HashMap();
map.put("total",12);
map.put("rows","一串数据");
return map;
}
可以测试五种返回方式
转发到jsp和重定向到Action界面
package com.dengrenli.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
/**
* @author小科比
* @site www.dengrenli.com
* @company 科比公司
* @create 2019-10-25 15:10
*/
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello1")
public String hello1(){
System.out.println("hello springmvc 啦啦啦");
return "hello springmvc 啦啦啦";
}
@RequestMapping("/hello2")
public String hello2(){
System.out.println("hello springmvc 啦啦啦");
return "hello";
}
@ResponseBody
@RequestMapping("/hello3")
public Map hello3(){
System.out.println("hello springmvc 啦啦啦");
Map map=new HashMap();
map.put("total",12);
map.put("rows","一串数据");
return map;
}
/*1.转发到页面*/
@RequestMapping("/forwardl")
public String forwardl() {
System.out.println("进来forwardl");
return "aaa";
}
/*2.转发到根路径下的页面*/
@RequestMapping("/forward2")
public String forward2() {
return "forward:/bbb.jsp";
}
/*3.转发到requestMapping下*/
@RequestMapping("/forward3")
public String forward3() {
return "forward:/forwardl";
}
/*4.重定向到根路径下*/
@RequestMapping("/redirect1")
public String redirect1() {
return "redirect:/bbb.jsp";
}
/*5.重定向到requestMapping下*/
@RequestMapping("/redirect2")
public String redirect2() {
return "redirect:/forwardl";
}
}
以此来完成增删查改(book案例)
1.先把相关分页标签资源文件配置好。
PageTag
package com.dengrenli.tag;
import com.dengrenli.util.PageBean;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class PageTag extends BodyTagSupport {
private static final long serialVersionUID = 1L;
private PageBean pageBean;
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() {
StringBuffer sb = new StringBuffer();
/*
* 拼接向后台提交数据的form表单
* 注意:拼接的form表单中的page参数是变化的,所以不需要保留上一次请求的值
*/
sb.append("<form id='pageBeanForm' action='"+pageBean.getUrl()+"' method='post'>");
sb.append("<input type='hidden' name='page'>");
Map<String, String[]> parameterMap = pageBean.getMap();
if(parameterMap != null && parameterMap.size() > 0) {
Set<Entry<String, String[]>> entrySet = parameterMap.entrySet();
for (Entry<String, String[]> entry : entrySet) {
if(!"page".equals(entry.getKey())) {
String[] values = entry.getValue();
for (String val : values) {
sb.append("<input type='hidden' name='"+entry.getKey()+"' value='"+val+"'>");
}
}
}
}
sb.append("</form>");
/*
* 展示的分页条
*/
sb.append("<div style='text-align: right; font-size: 12px;'>");
sb.append("每页"+pageBean.getRows()+"条,共"+pageBean.getTotal()+"条,第"+pageBean.getPage()+"页,共"+pageBean.getMaxPage()+"页 <a href='javascript:gotoPage(1)'>首页</a> <a");
sb.append(" href='javascript:gotoPage("+pageBean.getMaxPage()+")'>上一页</a> <a");
sb.append(" href='javascript:gotoPage("+pageBean.getNextPage()+")'>下一页</a> <a");
sb.append(" href='javascript:gotoPage("+pageBean.getMaxPage()+")'>尾页</a> <input type='text'");
sb.append(" id='skipPage'");
sb.append(" style='text-align: center; font-size: 12px; width: 50px;'> <a");
sb.append(" href='javascript:skipPage()'>Go</a>");
sb.append("</div>");
/*
* 给分页条添加与后台交互的js代码
*/
sb.append("<script type='text/javascript'>");
sb.append(" function gotoPage(page) {");
sb.append(" document.getElementById('pageBeanForm').page.value = page;");
sb.append(" document.getElementById('pageBeanForm').submit();");
sb.append(" }");
sb.append(" function skipPage() {");
sb.append(" var page = document.getElementById('skipPage').value;");
sb.append(" if(!page || isNaN(page) || parseInt(page)<1 || parseInt(page)>"+pageBean.getMaxPage()+"){");
sb.append(" alert('请输入1~N的数字');");
sb.append(" return;");
sb.append(" }");
sb.append(" gotoPage(page);");
sb.append(" }");
sb.append("</script>");
return sb.toString();
}
}
z.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>deng 1.1 core library</description>
<display-name>deng core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>z</short-name>
<uri>/deng</uri>
<tag>
<name>page</name>
<tag-class>com.dengrenli.tag.PageTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>pageBean</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
2.写好jsp页面代码(bookList.jsp,bookEdit.jsp)
bookList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="/deng" prefix="z" %>
<!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>
<h2>小说目录</h2>
<br>
<form action="${pageContext.request.contextPath}/book/list"
method="post">
书名:<input type="text" name="bname"> <input type="submit"
value="确定">
<input type="hidden" name="rows" value="15">
</form>
<a href="${pageContext.request.contextPath}/book/preSave">增加</a>
<table border="1" width="100%">
<tr>
<td>编号</td>
<td>名称</td>
<td>价格</td>
<td>操作</td>
</tr>
<c:forEach items="${bookList }" var="b">
<tr>
<td>${b.bid }</td>
<td>${b.bname }</td>
<td>${b.price }</td>
<td>
<a href="${pageContext.request.contextPath}/book/preSave?bid=${b.bid}">修改</a>
<a href="${pageContext.request.contextPath}/book/del/${b.bid}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<z:page pageBean="${pageBean }"></z:page>
</body>
</html>
bookEdit.jsp
<%@ 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>
<form action="${pageContext.request.contextPath}${book2.bname == null ? '/book/add' : '/book/edit'}" method="post">
书籍ID:<input type="text" name="bid" value="${book2.bid }"><br>
书籍名称:<input type="text" name="bname" value="${book2.bname }"><br>
书籍价格:<input type="text" name="price" value="${book2.price }"><br>
<input type="submit">
</form>
</body>
</html>
3.写好后台控制文件BookController.java
因为我们的业务层和持久层都已经在前面已经都解决了,所以我们仅仅只需要把控制层写好就行
package com.dengrenli.controller;
import com.dengrenli.model.Book;
import com.dengrenli.service.BookService;
import com.dengrenli.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* @author小科比
* @site www.dengrenli.com
* @company 科比公司
* @create 2019-10-25 15:10
*/
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService bookService;
/*查询*/
@RequestMapping("/list")
public String list(Book book, HttpServletRequest req){
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
List<Book> books = this.bookService.listPager(book, pageBean);
req.setAttribute("bookList",books);
req.setAttribute("pageBean",pageBean);
return "bookList";
}
/*跳转增加和修改页面*/
@RequestMapping("/preSave")
public String preSave(Book book,HttpServletRequest req){
if (book!=null && book.getBid() !=null && book.getBid() !=0){
Book book1 = this.bookService.selectByPrimaryKey(book.getBid());
req.setAttribute("book2",book1);
}
return "bookEdit";
}
/*增加*/
@RequestMapping("/add")
public String add(Book book,HttpServletRequest req){
this.bookService.insertSelective(book);
return "redirect:/book/list";
}
/*修改*/
@RequestMapping("/edit")
public String edit(Book book,HttpServletRequest req){
this.bookService.updateByPrimaryKeySelective(book);
return "redirect:/book/list";
}
/*删除*/
@RequestMapping("/del/{bid}")
public String del(@PathVariable("bid") Integer bid,HttpServletRequest req){
this.bookService.deleteByPrimaryKey(bid);
return "redirect:/book/list";
}
/*删除*/
@RequestMapping("/del/{typeid}/{bid}")
public String xxx(@PathVariable("typeid") Integer typeid,@PathVariable("bid") Integer bid,HttpServletRequest req){
this.bookService.deleteByPrimaryKey(bid);
return "redirect:/book/list";
}
}
运行结果:
静态资源处理
当我们直接去访问这张图片是访问不到的,应为没有配静态资源的映射。springmvc不知道去哪找这张图片的地址。所以我们就需要配请求映射:
在springmvx-serlvet.xml这个配置文件中加上下面的配置就Ok了。如果你要要映射js,和css文件一样的套路:
<mvc:resources location="/static/" mapping="/static/**"/>
接下来看一下运行结果:
这是我的图片
谢谢大家,多多指教!!!