目录
什么是springmvc?
Spring Web MVC是一种基于Java的实现了MVC设计模式的、请求驱动类型的、轻量级Web框架。
导入pom依赖:
org.springframework
spring-webmvc
${spring.version}
缺少下面的这两个jar包会报java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
原因:org.springframework.web.servlet.view.JstlView在视图解析时需要这二个jar包
-
jstl -
jstl -
1.2 -
-
-
taglibs -
standard -
1.1.2 -
第一个springMVC程序:HelloWorld
配置文件配置:
1.扫描base-package
[ 定义注解扫描,开启注解扫描,开启动态扫描]
2.配置视图解析器
本地资源视图解析器:internalResourceViewResolver
springmvc-servlet.xml
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">
value="org.springframework.web.servlet.view.JstlView">
做静态资源映射
修改web.xml
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">
Archetype Created Web Application
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/springmvc-servlet.xml
1
true
SpringMVC
/
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
HelloController:
- package com.xbb.ssm.controller;
-
- import org.springframework.stereotype.Component;
- import org.springframework.stereotype.Controller;
- import org.springframework.stereotype.Repository;
- import org.springframework.stereotype.Service;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
-
- import javax.servlet.http.HttpServletRequest;
-
- /**
- * 被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...");
- // /hello.jsp
- return "hello";
- }
-
- @RequestMapping("/hello2")
- public ModelAndView hello2(HttpServletRequest req){
- ModelAndView mv=new ModelAndView();
- mv.setViewName("hello");
- mv.addObject("msg","success");
- return mv;
- }
- }
-
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" %>
-
Title - SpringMVC 你好
- ${msg}
-
运行效果:

BookController
- package com.xbb.ssm.controller;
-
- import com.xbb.ssm.biz.BookBiz;
- import com.xbb.ssm.model.Book;
- import com.xbb.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" %>
-
Title -
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"%>
-
Title - SpringMVC 你好
- ${msg}
- ${lst}
"${lst}" var="l"> - ${l.bid}:${l.bname}
-
运行效果:
