springMVC
SpringMVC框架简单的介绍
springmvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合。
springmvc是一个基于mvc的web框架。
springmvc 表现层:方便前后端数据的传输。
Spring MVC 拥有控制器,作用跟Struts类似,接收外部请求,解析参数传给服务层。
MVC是指,C控制层,M模块层,V显示层这样的设计理念,而SSM框架里面SPRING MVC本身就是MVC框架,作用是帮助(某种意义上也可以 理解为约束)我们要按照MVC这样的设计来开发WEB项目。
springmvc项目架构图
Springmvc架构原理解析
- 客户端(浏览器)发送请求,直接请求到 DispatcherServlet。
- DispatcherServlet 根据请求信息调用 HandlerMapping,解析请求对应的 Handler。
- 解析到对应的 Handler(也就是我们平常说的 Controller 控制器)后,开始由 HandlerAdapter 适配器处理。
- HandlerAdapter 会根据 Handler 来调用真正的处理器开处理请求,并处理相应的业务逻辑。
- 处理器处理完业务后,会返回一个 ModelAndView 对象,Model 是返回的数据对象,View 是个逻辑上的 View。
- ViewResolver 会根据逻辑 View 查找实际的 View。
- DispaterServlet 把返回的 Model 传给 View(视图渲染)。
- 把 View 返回给请求者(浏览器)
springMVC需要的jar
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
修改web.xml
<servlet>
<servlet-name>MySpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MySpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
spring的配置文件springmvc-servlet.xml
<context:component-scan base-package="controller"></context:component-scan>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前綴 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!--后綴 -->
<property name="suffix" value=".jsp"></property>
</bean>
配置所在页面的所在的文件夹和后缀
controller层的代码
进行页面跳转
@RequestMapping("/index")
public String index(){
return "index";
}
@RequestMapping :访问路径
反回json格式
@RequestBody("/index")
public String index(){
return "index";
}
@RequestBody:反回json格式
跳方法上的路径
@ResponseBody
@RequestMapping("/login")
public String index(){
return "redirect:/index";
}
pringMVC上传
jar
<!-- 文件上传 start-->
<!-- commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
配置文件springmvc-servlet.xml
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置编码,默认是iso-8859-1,如果页面是utf-8的,这个一定要改 -->
<property name="defaultEncoding" value="utf-8"/>
<!-- 限制文件上传的大小:字节B为单位 -->
<!--<property name="maxUploadSize" value="1024" />-->
</bean>
controller层
@RequestMapping("/fileUpload")
public String fileUpload(
@RequestParam(value="myfiles", required=false) MultipartFile myfiles,
User user, HttpServletRequest request){
System.out.println("用户名:" + user.getName());
if(myfiles.isEmpty()){
System.out.println("上传的文件为空");
return "file_succes";
}
System.out.println("文件名:" + myfiles.getOriginalFilename());
System.out.println("后缀:" + FilenameUtils.getExtension(myfiles.getOriginalFilename()));
String path = "上传路径"
System.out.println(path);
try {
FileUtils.copyInputStreamToFile(myfiles.getInputStream(), new File(path, myfiles.getOriginalFilename()));
} catch (IOException e) {
e.printStackTrace();
}
return "file_succes";
}
springMVC整合ssm
pom.xml
<!-- spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!--aspectj weaver-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!-- spring-cn.aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--网不好下不下来5.1.9-->
<!--spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<!--spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
<!--aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!--mysql jar-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency>
<!-- commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<!-- jsqlparser -->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.61</version>
</dependency>
<!-- 文件上传 start-->
<!-- commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!-- commons-io -->
<!--<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>-->
<!-- 文件上传 end-->
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
<!-- jsqlparser -->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>3.0</version>
</dependency>
在pom.xml中我添加了json、文件上传、分页等额外的架包
配置文件
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">
<!-- 配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url">
<!-- 千万注意,这里不能换行 -->
<value><![CDATA[jdbc:mysql://127.0.0.1:3306/standard?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8]]></value>
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 引入mybatis的配置文件-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--AOP事务处理-->
<aop:aspectj-autoproxy />
<aop:config proxy-target-class="false">
<aop:pointcut expression="execution(* *biz..*(..))" id="tansService" />
<aop:advisor pointcut-ref="tansService" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"/>
</bean>
<context:component-scan base-package="biz"/>
</beans>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置mybatis的log实现log4j-->
<settings>
<setting name="logImpl" value="LOG4J"></setting>
</settings>
<!--实体类取别名-->
<typeAliases>
<package name="pojo"/>
</typeAliases>
</configuration>
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
<import resource="classpath:applicationContext.xml"/>
<context:component-scan base-package="controller"/>
<mvc:annotation-driven />
<!--<mvc:resources mapping="/statis/**" location="/statis/" />-->
<!-- <bean name="/index.html" class="com.controller.IndexController"/>-->
<!--完成视图的对应-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/" />
<!--后缀-->
<property name="suffix" value=".jsp" />
</bean>
<!--文件上传-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置编码,默认是iso-8859-1,如果页面是utf-8的,这个一定要改 -->
<property name="defaultEncoding" value="utf-8"/>
<!-- 限制文件上传的大小:字节B为单位 -->
<!--<property name="maxUploadSize" value="1024" />-->
</bean>
</beans>
springmvc-servlet.xml和applicationContext.xml两个配置文件可以放在一起
mapper层
创建StandardMapper.xml和StandardMapper接口
mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.standard.StandardMapper">
<select id="selectList" resultType="Standard">
SELECT id, std_num stdNum, zhname, version, `keys`, `release`, impl_date implDate, package_path packagePath FROM standard
</select>
</mapper>
StandardMapper接口
package mapper.standard;
import org.apache.ibatis.annotations.Param;
import pojo.Standard;
import java.util.List;
public interface StandardMapper {
List<Standard> selectList();
}
biz层
在创建biz接口和biz的实现类
biz实现类
package biz.impl;
import biz.StandardBiz;
import mapper.standard.StandardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Standard;
import java.util.List;
@Service
public class StandardBizImpl implements StandardBiz {
@Autowired
private StandardMapper standardMapper;
@Override
public List<Standard> selectList(){
List<Standard> list = standardMapper.selectList();
return list;
}
}
@Service:是扫描biz的注解
controller层
controller进行页面数据传递
package controller;
import biz.StandardBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import pojo.Standard;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
public class StandardController {
@Autowired
private StandardBiz standardBiz;
@RequestMapping(value = "index")
public ModelAndView index(){
List<Standard> list = standardBiz.selectList(currentPageNo);
modelAndView.addObject("list",list);
return modelAndView;
}
public StandardBiz getStandardBiz() {
return standardBiz;
}
public void setStandardBiz(StandardBiz standardBiz) {
this.standardBiz = standardBiz;
}
}
修改web.xml
<servlet>
<servlet-name>MySpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MySpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Biz = standardBiz;
}
}
修改web.xml
<servlet>
<servlet-name>MySpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MySpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>