目录
Spring Web MVC 是一种基于java的实现了MVC设计模式的,请求驱动类型的,轻量级Web框架
也有人声称SpringMVC
是视图层框架 :原因:SpringMVC部分功能是向用户渲染展示数据
是控制层框架 原因:DispatherServlet:接受浏览器端的任意请求
然后寻找对应的 子控制器(适配器*Adapter)处理浏览器请求二者皆有所理
我们使用框架首先第一步:搭建环境
1.导入pom依赖
- "1.0" encoding="UTF-8"?>
-
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>org.examplegroupId>
- <artifactId>ssmartifactId>
- <version>1.0-SNAPSHOTversion>
- <packaging>warpackaging>
-
- <name>ssm Maven Webappname>
-
- <url>http://www.example.comurl>
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <maven.compiler.source>1.8maven.compiler.source>
- <maven.compiler.target>1.8maven.compiler.target>
- <maven.compiler.plugin.version>3.7.0maven.compiler.plugin.version>
-
-
-
- <spring.version>5.0.2.RELEASEspring.version>
-
- <mybatis.version>3.4.5mybatis.version>
-
- <mysql.version>5.1.44mysql.version>
-
- <pagehelper.version>5.1.2pagehelper.version>
-
- <mybatis.spring.version>1.3.1mybatis.spring.version>
-
- <commons.dbcp2.version>2.1.1commons.dbcp2.version>
- <commons.pool2.version>2.4.3commons.pool2.version>
-
- <log4j2.version>2.9.1log4j2.version>
-
- <junit.version>4.12junit.version>
- <servlet.version>4.0.0servlet.version>
- <lombok.version>1.18.2lombok.version>
- properties>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-ormartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-txartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-aspectsartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>${spring.version}version>
- dependency>
-
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>${mybatis.version}version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>${mysql.version}version>
- dependency>
-
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>${pagehelper.version}version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>${mybatis.spring.version}version>
- dependency>
-
-
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-dbcp2artifactId>
- <version>${commons.dbcp2.version}version>
- <exclusions>
- <exclusion>
- <artifactId>commons-pool2artifactId>
- <groupId>org.apache.commonsgroupId>
- exclusion>
- exclusions>
- dependency>
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-pool2artifactId>
- <version>${commons.pool2.version}version>
- dependency>
-
-
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-coreartifactId>
- <version>${log4j2.version}version>
- dependency>
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-apiartifactId>
- <version>${log4j2.version}version>
- dependency>
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-webartifactId>
- <version>${log4j2.version}version>
- dependency>
-
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>${junit.version}version>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>${servlet.version}version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>${lombok.version}version>
- <scope>providedscope>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>${spring.version}version>
- dependency>
-
- <dependency>
- <groupId>jstlgroupId>
- <artifactId>jstlartifactId>
- <version>1.2version>
- dependency>
- <dependency>
- <groupId>taglibsgroupId>
- <artifactId>standardartifactId>
- <version>1.1.2version>
- dependency>
-
- dependencies>
-
- <build>
- <finalName>ssmfinalName>
- <resources>
-
- <resource>
- <directory>src/main/javadirectory>
- <includes>
- <include>**/*.xmlinclude>
- includes>
- resource>
-
- <resource>
- <directory>src/main/resourcesdirectory>
- <includes>
- <include>jdbc.propertiesinclude>
- <include>*.xmlinclude>
- includes>
- resource>
- resources>
-
- <pluginManagement>
- <plugins>
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <version>${maven.compiler.plugin.version}version>
- <configuration>
- <source>${maven.compiler.source}source>
- <target>${maven.compiler.target}target>
- <encoding>${project.build.sourceEncoding}encoding>
- configuration>
- plugin>
- <plugin>
- <groupId>org.mybatis.generatorgroupId>
- <artifactId>mybatis-generator-maven-pluginartifactId>
- <version>1.3.2version>
- <dependencies>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>${mysql.version}version>
- dependency>
- dependencies>
- <configuration>
- <overwrite>trueoverwrite>
- configuration>
- plugin>
-
- <plugin>
- <artifactId>maven-clean-pluginartifactId>
- <version>3.1.0version>
- plugin>
-
- <plugin>
- <artifactId>maven-resources-pluginartifactId>
- <version>3.0.2version>
- plugin>
- <plugin>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.8.0version>
- plugin>
- <plugin>
- <artifactId>maven-surefire-pluginartifactId>
- <version>2.22.1version>
- plugin>
- <plugin>
- <artifactId>maven-war-pluginartifactId>
- <version>3.2.2version>
- plugin>
- <plugin>
- <artifactId>maven-install-pluginartifactId>
- <version>2.5.2version>
- plugin>
- <plugin>
- <artifactId>maven-deploy-pluginartifactId>
- <version>2.8.2version>
- plugin>
- plugins>
- pluginManagement>
- build>
- project>
缺少最下面的这两个jar包会报java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config-->
原因:org.springframework.web.servlet.view.JstlView在视图解析时需要这二个jar包
2.框架配置文件
- "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">
-
-
- <aop:aspectj-autoproxy/>
- <context:component-scan base-package="com.ljj.ssm"/>
-
-
-
-
- <mvc:annotation-driven>mvc:annotation-driven>
-
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView">property>
- <property name="prefix" value="/"/>
- <property name="suffix" value=".jsp"/>
- bean>
-
-
-
-
-
-
- beans>
注意:此依赖为设置访问路径
3.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 Applicationdisplay-name>
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:applicationContext.xmlparam-value>
- context-param>
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
-
-
- <servlet>
- <servlet-name>SpringMVCservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
-
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>/WEB-INF/springmvc-servlet.xmlparam-value>
- init-param>
- <load-on-startup>1load-on-startup>
-
- <async-supported>trueasync-supported>
- servlet>
- <servlet-mapping>
- <servlet-name>SpringMVCservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
- web-app>
放置框架配置文件的路径
HelloController:
- package com.ljj.ssm.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- /**
- * @author ljj
- * @site www.ljj.com
- * @create 2022-08-17 2:49
- */
- /*
- 被controller标记的类 会交给Spring 进行管理
- @Controller
- @Service
- @Repository
- @Component
- 以上四个都代表 当前类 交给Spring 容器进行管理
- */
- @Controller
- public class HelloController {
- // 自定义mvc: 浏览器发送请求 http://localhost:8080/hello.action?methodName=hello
- // springmvc: 浏览器发送请求 http://localhost:8080/helloReq
- @RequestMapping("/helloReq")
- public String hello(){
- System.out.println("hello springmvc...");
- return "hello";
- }
- }
-
-
Hello.jsp:
- <%--
- Created by IntelliJ IDEA.
- User: Administrator
- Date: 2022/8/17
- Time: 8:23
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- Spring Mvc...你好 oyang
- body>
- html>
运行效果:
输入网址:
http://localhost:8080/helloReq
所用的注解
Controller
Requestmapping
Requestparam
- package com.ljj.ssm.controller;
-
- import com.ljj.ssm.biz.BookBiz;
- import com.ljj.ssm.model.Book;
- import com.ljj.ssm.util.PageBean;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
-
- import javax.servlet.http.HttpServletRequest;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- /**
- * @RequestMapping 加在 类上面,称窄化路径,其实相当于包的概念
- */
- @Controller
- @RequestMapping("/book")
- public class BookController {
- @Autowired
- private BookBiz bookBiz;
-
- // http://localhost:8080/book/list
- @RequestMapping
- // @GetMapping=@RequestMapping(value = "/list",method = RequestMethod.GET)
- // @PostMapping
- // @DeleteMapping
- // @PutMapping
- public String hello(HttpServletRequest req){
- System.out.println("hello springmvc...");
- PageBean pageBean=new PageBean();
- pageBean.setRequest(req);
- Map map=new HashMap();
- String bname = req.getParameter("bname");
- map.put("bname",bname);
- List
- req.setAttribute("lst",maps);
- return "hello";
- }
-
- @RequestMapping("/add")
- public String add(Book book){
- this.bookBiz.insertSelective(book);
- return "redirect:/book/list";
- }
-
- @RequestMapping("/edit")
- public String edit(Book book){
- this.bookBiz.insertSelective(book);
- return "redirect:/book/list";
- }
-
- @RequestMapping("/del/{bid}")
- public String del(@PathVariable("bid") Integer bid){
- this.bookBiz.deleteByPrimaryKey(bid);
- return "redirect:/book/list";
- }
- }
index.jsp:
- <%--
- Created by IntelliJ IDEA.
- User: Administrator
- Date: 2022/8/16
- Time: 21:52
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- <a href="${pageContext.request.contextPath}/book/list?bname=圣墟">查询所有a>
- <a href="${pageContext.request.contextPath}/book/add?bid=2&bname=sb&price=9.9">增加a>
- <a href="${pageContext.request.contextPath}/book/edit?bid=2&bname=suibian&price=9.8">修改a>
- <a href="${pageContext.request.contextPath}/book/del/2">删除a>
- body>
- html>
hello.jsp
- <%--
- Created by IntelliJ IDEA.
- User: Administrator
- Date: 2022/8/16
- Time: 17:26
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- SpringMVC 你好
- ${msg}
- <hr>
- ${lst}
- <c:forEach items="${lst}" var="l">
- ${l.bid}:${l.bname}<hr>
- c:forEach>
- body>
- html>
点击新增后:
补充:springMvc的工作原理
1、浏览器发送请求到中央控制器Dispatcherservlet
2、中央控制器DispatcherServlet通过处理器映射器HandlerMapping找到处理器适配器HandlerAdapter
3、执行HandlerAdapter,返回值ModelAndView回传给DispatcherServlet
4、视图解析器viewResolver会对 返回值ModelAndView进行渲染,得到view视图对象
5、最后DispatcherServlet会将view视图对象返回给用户
转发与重定向的设置
"hello"转发到/hello.jsp
"forward:hello" 转发到/hello.jsp
"redirect:/hello" 重定向到.../项目名/hello book/list
四、静态资源的配置