文章目录
1、springMVC起步
SSH 通常指的是 Struts2 做控制器(controller),spring 管理各层的组件,hibernate 负责持久化层。
SSM 则指的是 SpringMVC 做控制器(controller),Spring 管理各层的组件,MyBatis 负责持久化层。
1.1、创建项目
maven环境的搭建参考:https://blog.youkuaiyun.com/a__int__/article/details/107775505
创建maven项目



maven官网spring mvc:https://mvnrepository.com/artifact/org.springframework/spring-webmvc
这里我们使用的spring mvc的版本是4.3.28

<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.28.RELEASE</version>
</dependency>
</dependencies>
把上这串代码复制到pom.xml,然后保存

这里我们发现jdk版本是1.5

使用spring mvc 4后的版本,jdk应采用1.8以上
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

然后按Alt+f5刷新项目,jdk就变成1.8了

然后生成web.xml文件

然后向pom.xml里放三件套
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

1.2、测试运行
新建类Hello

package com.haha.hello;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class Hello implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("新项目开始运行了");
return null;
}
}
然后新建配置文件spring-servlet.xml,放在Hello.java同级目录下
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean name="/hc.do" class="com.haha.hello.Hello"></bean>
</beans>

关于bean里面id和name的区别:https://blog.youkuaiyun.com/q309572960/article/details/90473772
配置web,xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/haha/hello/spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
然后将项目加入到tomcat服务中
运行并访问

1.4、返回值

新建一个jsp

这样写可以直接访问到index.jsp,但是我们一般不这样写
先在spring.xml中添加一个bean
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

Hello.java

ModelAndView mav = new ModelAndView();
mav.setViewName("index");
// 传递键值对
mav.addObject("aa","11");
return mav;
index.jsp

启动运行,访问

1.3、 原理解析
web.xml
spring-servlet.xml
springmvc整体架构
运行步骤

1.4、 一个完整的springmvc配置
新建项目
配置pom.xml
<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.0</modelVersion>
<groupId>com.haha</groupId>
<artifactId>springmvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.28.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/haha/hello/Hello.java</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
新建Hello.java
package com.haha.hello;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class Hello implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("新项目开始运行了");
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
// 传递键值对
mav.addObject("aa","11");
return mav;
}
}
新建index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的jsp</title>
</head>
<body>
${aa }
</body>
</html>
新建spring-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- controller控制器 -->
<bean id="/hc.do" class="com.haha.hello.Hello"></bean>
</beans>
项目目录结构

将项目加入tomcat server,运行,访问


这里需要注意的是
web.xml里面spring-servlet.xml的路径

spring-servlet.xml里面controller控制器 的包名


1.5、log4j日志
在pom中添加

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
添加一个log4j的配置log4j.properties

log4j.rootLogger=debug,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%-5p] --> [%t]: %m %x %n
1.6、替换springmvc各个组件
spring-servlet.xml中
映射器
作用:接收url地址,根据地址找到对应方法,封装到handler
映射器可选实现类:
- BeanNameUrlHandlerMapping (拿着url与bean的id比较)

- SimpleUrlHandlerMapping (拿着url与自己mapping属性中的key比较,找到对应的值以后,再拿着值去找bean)

图解SimpleUrlHandlerMapping 作用
- RequestMappingHandlerMapping
- …
适配器
作用:接收handler,执行其中方法,返回ModelAndView
适配器可选实现类:
- SimpleControllerHandlerAdapter (要求控制器必须实现springmvc中的Controller接口)
- HttpRequestHandlerAdapter (要求控制器必须实现HttpRequestHandler接口)
- RequestMappingHandlerAdapter (要求控制器有@Controller注释)
- …
视图解析器
作用:接收ModelAndView,把逻辑视图解析为物理视图
视图解析器可选实现类:
-
InternalResourceViewResolver (会在逻辑视图名前后加上前后缀)

-
BeanNameViewResolver (把逻辑视图名当作ioc容器中bean的id来找bean,这个bean必须实现view接口)

-
…
1.7、spring-servlet注解适配器
新建包com.haha.hello2

新建Hello2.java
package com.haha.hello2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("user")
public class Hello2 {
@RequestMapping("save")
public String save() {
System.out.println("save执行了");
return null;
}
}
新建spring-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 扫包 -->
<context:component-scan base-package="com.haha.hello2"></context:component-scan>
<!-- 映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
修改web.xml里的spring-servlet路径

启动,访问:http://localhost:8080/springmvc/user/save.do


图解

1.8、spring-servlet省略写法
新建包com.haha.hello3

新建Hello3.java
package com.haha.hello3;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("user")
public class Hello3 {
@RequestMapping("save")
public String save() {
System.out.println("save执行了");
return null;
}
}
新建spring-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 扫包 -->
<context:component-scan base-package="com.haha.hello3"></context:component-scan>
<!-- mvc -->
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
<mvc:annotation-driven />包括了映射器和适配器,还配置了其他更多功能
修改web.xml里的spring-servlet路径

启动,访问:http://localhost:8080/springmvc/user/save.do


1.9、浏览器传参数
新建包com.haha.hello4

新建Hello4.java
package com.haha.hello4;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("user")
public class Hello4 {
@RequestMapping("save")
public String save(byte b,short s) {
System.out.println("byte:"+b);
System.out.println("short:"+s);
return null;
}
}
新建spring-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 扫包 -->
<context:component-scan base-package="com.haha.hello4"></context:component-scan>
<!-- mvc -->
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
<mvc:annotation-driven />包括了映射器和适配器,还配置了其他更多功能
修改web.xml里的spring-servlet路径

在webapp下新建h4.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="/springmvc/user/save.do" method="post">
byte:<input name="b" />
short:<input name="s" />
<button type="submit">发送</button>
</form>
</body>
</html>
运行,访问:http://localhost:8080/springmvc/h4.jsp


1.9、流程回顾

1、先由 客户端 发起请求
2、请求被 前端控制器 拦截、过滤
3、前端控制把url交给 url映射器 ,映射器根据请求的url进行路由分发(也就是找到控制器里对应的方法) 找到对应方法后把他包装成handler对象,然后交给前端控制器
4、前端控制器拿着handler找 适配器 ,适配器执行handler里面的方法,并返回ModelAndView
5、视图解析器 解析ModelAndView里面的逻辑视图,并变成物理视图,然后渲染物理视图,渲染结果交给前端控制器,然后返回给客户端
2、springMVC进阶
2.1、转发和重定向
转发 forward

重定向 redirect

2.2、类型转换器
接收日期
jsp里面

接收
user.java

控制器

运行后浏览器传递日期

这里只能传斜杠这样的日期,否则会报错,
为了满足填入2019-01-01这样的格式也不会报错,需要我们写类型转换器
新建一个类

配置spring-servlet.xml (注册类型转换器)

这样一个类型转换器就做完了
下面我们添加第二个类型转换器


2.3、获取web元素

jsp

运行访问

几大作用域


jsp

访问

给重定向加参数

2.4、/和/*的区别

/* :拦截所有请求
/ :除了jsp、jspx, 会拦截所有请求
我们一般使用的是/
2.5、去掉.do

2.6、在访问路径中传参
基本用法

注意参数名

2.7、参数类型配置
配置一个(就仅支持一个)

配置多个

如果不写,支持默认的五种类型



3、http的一些知识
1、
客户端给服务器发送http请求,由3部分组成:请求行、请求头、请求体
服务器给客户端响应,由3部分组成:响应行、响应头、响应体
2、
请求头中有一个属性:accept(表示要什么格式的数据)
对应响应头中的属性:content-type(表示发送什么格式的数据)
3、
请求头中有一个属性:content-type(表示请求发送什么格式的参数)

本文详细介绍了SpringMVC的起步与进阶知识,从创建项目、配置环境到理解核心组件,再到转发与重定向、类型转换等高级主题。






585

被折叠的 条评论
为什么被折叠?



