SpringMvc入门

目标

1.springmvc简介以及配置

2.springmvc之helloword实现

3.springmvc常用注解及返回值处理

4.springmvc静态资源处理

对比学习:

struts:使用的时候每一个action都需要配置

 springmvc:

一、Springmvc简介及配置 

1. 什么是springMVC?

   Spring Web MVC是一种基于Java的实现了MVC设计模式的、请求驱动类型的、轻量级Web框架。

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提供给表单展示和提交到的对象就叫表单对象)

2、在项目中添加springMVC


①、添加相关依赖

(1)替换:引入jar包的时候尽可能不要引入相同的jar包,以免发生冲突

②、在WEB-INF下添加springmvc-servlet.xml(spring-mvc.xml)

做前后端分离  3) ViewResolver(它会自动补全后台传过来的字符串,InternalResourceViewResolver它会将后台传过来的解析拼接) 可以不写

<?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.lsy"/>

    <!--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/**"/>-->


</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>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 读取Spring上下文的监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 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>

3、测试

①、新建controller包,controller新建HelloController类

package com.lsy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class HelloController {
    //@RequestMapping("/hello")就相当于struts-sy.xml中的Action标签的配置
    @RequestMapping("/hello")
    public String hello(){
        System.out.println("hello spring mvc");
        return "hello";
    }

}

新建hello.jsp

<%@page  contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
SpringMvc hello
</body>
</html>

②、配置Tomcat

部署-》工件-》选择exploded的,这个默认编译了项目,启动速度快一些  

修改后,它会自动刷新 

③、测试结果: 

二、SpringMvc的工作流程

经典面试题

 SpringMVC处理请求的流程

 1 首先用户发送请求-->DispatherServlet

 2 DispatcherServlet-->HandlerMapping

 3 DispatcherServlet-->HandlerAdapter

 4 HandlerAdapter-->处理器功能处理方法的调用

 5 ModelAndView的逻辑视图名-->ViewRecolver

 6 View-->渲染

 7 返回控制权给DispatcherServlet,由DispatcherServlet返回呼应给用户,流程结束

工作流程图:

 

 三、Springmvc常用注解及返回值处理

 通过crud进行实现

 Controller、   Requestmapping  、Requestparam 传参

 记得在bookmapper.xml中把缓存关闭 

页面跳转:
  1 转发:"forward:path"
  2 重定向:"redirect:path"
  注1:这两种跳转方式将会绕开视图解析器的前缀和后缀
  注2:还有就是如果是在同一controller中则不用使用"/"从根目录开始,而如果是在不同的controller则一定要从根目录开始。

常用注解
  1 @Controller:用于标识处理器类

  2 @RequestMapping:请求到处理器功能方法的映射规则,可定义到类和方法
      常用参数:value、method
      可将@RequestMapping标签定义到类名处窄化路径

  3 @RequestParam:请求参数到处理器功能处理方法的方法参数上的绑定
      常用参数:value、required、defaultValue
      注:required设置成false的参数类型必须是引用类型,因为基本数据类型是不能为null的

  4 @ModelAttribute:请求参数到命令对象的绑定
      常用参数:value
    4.1 可用@ModelAttribute标注方法参数,方法参数会被添加到Model对象中(作用:向视图层传数据)
    4.2 可用@ModelAttribute标注一个非请求处理方法,此方法会在每次调用请求处理方法前被调用(作用:数据初始化)
    4.3 可用@ModelAttribute标注方法,方法返回值会被添加到Model对象中(作用:向视图层传数据)
          但此方法视图的逻辑图就会根据请求路径解析,例如:a/test42 --> /WEB-INF/a/test42.jsp
          太麻烦几乎不用,不用直接保存到Model或ModelAndView中

  5 @SessionAttributes:指定ModelMap中的哪些属性需要转存到session
      常用参数:value、types
      注1:必须放到class类名处

  6 @InitBinder(本章暂不介绍):用于将请求参数转换到命令对象属性的对应类型

  7 @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是不会显示错误信息的,只有使用了日志才会显示
      $.ajax({
                url : "jsontest",
                type : "POST",
                async : true,
                contentType : "application/json",
                data : JSON.stringify(json),
                dataType : 'json',
                success : function(data) {
                    if (data.userstatus === "success") {
                        $("#errorMsg").remove();
                    } else {
                        if ($("#errorMsg").length <= 0) {
                            $("form[name=loginForm]").append(errorMsg);
                        } }} });
 其它
  @CookieValue cookie数据到处理器功能处理方法的方法参数上的绑定
  @RequestHeader:请求头(header)数据到处理器功能处理方法的方法参数上的绑定
  @RequestBody:请求的body体的绑定(通过HttpMessageConverter进行类型转换);
  @ResponseBody:处理器功能处理方法的返回值作为响应体(通过HttpMessageConverter进行类型转换);
  @ResponseStatus:定义处理器功能处理方法/异常处理器返回的状态码和原因;
  @ExceptionHandler:注解式声明异常处理器;
  @PathVariable:请求URI中的模板变量部分到处理器功能处理方法的方法参数上的绑定,

 请求处理方法的参数及返回值
1请求处理方法的参数
      ServletRequest/HttpServletRequest
      HttpServletResponse/HttpServletResponse
      HttpSession
      RedirectAttributes
      Model/ModelAndView

      WebRequest/NativeWebRequest
      Locale
      InputStream/Reader
      OutputStream/Writer
      Principal
      HttpEntiry<?>
      Errors
      BindingResult
      SessionStatus
      UriComponentsBuilder
      带@PathVariable,@MatrixVariable注释的对象
      @RequestParam,@RequestHeader,@RequestBody,@RequestPart

      特别重要的是:org.springframework.ui.Model类型,每次调用请求处理方法时,SpringMVC都创建Model对象并将
      其注入到各种对象

 2返回值的类型
      ModelAndView
      Model
      String(代表一个逻辑视图名)

      void
      Map
      View
      Callable
      DeferredResult
      其它任意类型,spring将其视作输出给View的对象模型

1、 controller包下新建BookController

package com.lsy.controller;

import com.lsy.model.Book;
import com.lsy.util.PageBean;
import com.lsy.service.BookService;
import com.lsy.util.StringUtils;
import com.lsy.model.Book;
import com.lsy.service.BookService;
import com.lsy.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 org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 小李飞刀
 * @site www.javaxl.com
 * @company
 * @create  2019-09-27 21:51
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/preSave")
    public String preSave(Book book, HttpServletRequest request){
        if(book.getBid() != null){
            Book b = this.bookService.selectByPrimaryKey(book.getBid());
            request.setAttribute("book2",b);
        }

        return "bookEdit";
    }

    @RequestMapping("/add")
    public String add(Book book){
        this.bookService.insert(book);
        return "redirect:/book/list";
    }

    @RequestMapping("/del/{bid}")
    public String del(@PathVariable(value = "bid") Integer bid){
        this.bookService.deleteByPrimaryKey(bid);
        return "redirect:/book/list";
    }

    @RequestMapping("/list")
    public String list(HttpServletRequest request,Book book){
        PageBean pageBean = new PageBean();
        pageBean.setRequest(request);
        Map map = new HashMap();
        map.put("bname","%圣墟%");
        List<Map> list = this.bookService.listPager(map, pageBean);
        request.setAttribute("bookList",list);
        request.setAttribute("pageBean",pageBean);
        return "bookList";
    }

    @ResponseBody
    @RequestMapping("/list1")
    public Map list1(HttpServletRequest request,Book book){
        PageBean pageBean = new PageBean();
        pageBean.setRequest(request);
        Map map = new HashMap();
        map.put("bname","%圣墟%");
        List<Map> list = this.bookService.listPager(map, pageBean);
        map.put("bookList",list);
        map.put("pageBean",pageBean);
        return map;
    }

    @RequestMapping("/edit")
    public String edit(Book book){
        this.bookService.updateByPrimaryKeySelective(book);
        return "redirect:/book/list";
    }
}

2、新增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="/zking" 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>
<script type="text/javascript">
	function add(){
		// window.location.href = "bookEdit.jsp";
		window.location.href="${pageContext.request.contextPath}/book/preSave";
	}
	
	function update(bid){
		window.location.href = "${pageContext.request.contextPath}/book/preSave?bid="+bid;
	}
	
	function del(bid){
		window.location.href = "${pageContext.request.contextPath}/book/del/"+bid;
	}
</script>
</head>
<body>
	<form action="${pageContext.request.contextPath}/book/list"
		method="post">
		书名:<input type="text" name="bname"> <input type="submit"
			value="确定">
	</form>
	<button onclick="add();">新增</button>
	<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>
					<button onclick="update(${b.bid });">修改</button>&nbsp;&nbsp;&nbsp;
					<button onclick="del(${b.bid });">删除</button>
				</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>
<script type="text/javascript">
	function doSubmit(bid){
		var bookForm = document.getElementById("bookForm");
		if(bid){
			//修改时候执行
			bookForm.action = '${pageContext.request.contextPath}/book/edit';
		}else{
			//新增时候执行
			bookForm.action = '${pageContext.request.contextPath}/book/add';
		}
		bookForm.submit();
	}
</script>
</head>
<body>
	<form id="bookForm" action="" method="post">
		bid:<input type="text" name="bid" value="${book2.bid }"><br>
		bname:<input type="text" name="bname" value="${book2.bname }"><br>
		price:<input type="text" name="price" value="${book2.price }"><br>
		<input type="submit" value="提交" onclick="doSubmit('${book2.bid }');"><br>
	</form>
</body>
</html>

3、结果界面展示

①、查询:

 

②、 增加:

  ③、修改

修改前

圣墟第34章

修改后

 ④、删除了35

删除前

删除后

4、返回json串

BookController

 

@ResponseBody
    @RequestMapping("/list1")
    public Map list1(HttpServletRequest request,Book book){
        PageBean pageBean = new PageBean();
        pageBean.setRequest(request);
        Map map = new HashMap();
        map.put("bname","%圣墟%");
        List<Map> list = this.bookService.listPager(map, pageBean);
        map.put("bookList",list);
        map.put("pageBean",pageBean);
        return map;
    }

结果:所以说以后查到结果直接返回就可以了,不需要使用工具类了

 

 四、静态资源处理

1、新建static文件

 2、访问方式

①、以往访问方式

直接找不到 

 

 访问不到的原因:

SpringMvc是把1.jpg当成了请求处理,需要在HelloController里面填写地址访问后台

解决方式:

在spring-mvc.xml中做资源处理映射

 

这样就能访问到了 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值