当前位置:网站首页>以JSP为视图解析器搭建SSM项目
以JSP为视图解析器搭建SSM项目
2022-07-28 22:07:00 【心有—林夕】
目录
1.注意这个注意这个注意这个!!!(如果缺少清除再重新打包)
一 环境准备
1.文件目录(web服务都建不成的直接劝退吧)

2.tomcat8.5

注:选下面图片这个是为了实现上面的热部署

3.maven依赖
注:有个分页插件依赖,如果用到可以写,用不到就不写
为什么导入日志,因为mybatis里面需要实现日志功能logback是sl4j的实现
<packaging>war</packaging>
<dependencies>
<!-- json依赖 @RequestBody可能用的到-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!-- 数据库连接池 druid-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<!-- 分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- spring整合mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.1</version>
</dependency>
<!-- spring里面的事物管理器这个类在这个依赖中,虽然用mybatis但是还是要导入这个依赖 spring -jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.1</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- ServletAPI -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- 文件上传依赖 文件下载不需要-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>
二.配置文件
1.web.xml
注:编码过滤器和前端控制器必须有,剩下的只是实现部分功能方便。
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<!-- 设置编码过滤器 获取请求前获取,请求后获取毫无意义-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<!-- 自定义编码-->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!-- 请求和响应都过滤-->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 处理请求方式的过滤器 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置springMVC的前端控制器-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数 设置springMVC的位置和名称-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 将DispatcherServlet初始化提前到服务器启动时-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置spring的监听器 作用 是在服务器启动时加载spring的配置文件 加载ioc容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--设置spring自定以的位置和名称-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
</web-app>2.springmvc.xml
注:视图解析器和扫描器必须有,剩下的看自己是否需要
jsp和themleaf就一个视图解析器不同。其他配置都一样
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描器-->
<context:component-scan base-package="top.remained.controller">
</context:component-scan>
<!-- 视图解析器对象 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 开放对静态资源的访问-->
<mvc:default-servlet-handler/>
<!-- 开启上面就一定要开启这个 开启mvc注解驱动
先用DispatcherServlet解析,解析不了交给默认的servlet-->
<mvc:annotation-driven/>
<!-- web-INF 通过浏览器和重定向都不能访问 不用位置名称 因为会被下面的视图解析器解析-->
<!-- 配置视图控制器-->
<!-- <mvc:view-controller path="/" view-name="index"/>-->
<!-- 配置文件上传解析器 id固定-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
</beans>
3.spring-mybatis.xml
注:大部分配置的都是mybatis里面的东西(IOC),扫描组件除去控制层的原因是,springmvc已经扫描过了,可以但没必要
过程
1.加载 jdbc.property
<context:property-placeholder location="classpath:jdbc.properties"/>
2.配置datasource 以bean的形式 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
3.配置sql工厂 以bean的形式 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
然后sql工厂 里面property 包括配好的数据源以ref="dataSource" <property name="dataSource" ref="dataSource"/>再加载 property mybatis的配置文件 <property name="configLocation" value="classpath:mybatis.xml"/>
扫描注解包 <context:component-scan base-package="top.remained"/>
扫描mapper包完成自动注入@AutoWired
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="top.remained.mapper"> </property>
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--扫描组件(除去控制层)-->
<context:component-scan base-package="top.remained">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 引入数据库配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pwd}"/>
</bean>
<!-- 配置事务管理器 作用面向切面-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务的注解驱动 将使用注解@Transactional标识的方式或类中所有的方法进行事务管理-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<!-- 不需要设置id 因为不是自己创建对象的 可以直接在spring的ioc中获取SqlSessionFactory对象-->
<!-- 把在mybatis里面配置的全部以属性配置,即可不用mybatis配置文件-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="top.remained.pojo"/>
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<!-- 如果 映射文件所在的包和resources里面包名一致,不需要设置-->
<!-- 没有MapperScannerConfigurer这个bean时 这个不管一样不一样 都要设置-->
<!-- <property name="mapperLocations" value="classpath:top/remained/mapper/*.xml"/>-->
</bean>
<!-- 扫描的是mapper接口 配置完这个 spring可以通过ioc的自动装配完全接管mapper接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="top.remained.mapper"/>
</bean>
</beans>4.mybatis.xml
注:mybatis为什么只有一个分页插件,因为在spring-mybatis里面已经配置过了,
分页插件 也可以在spring-mybatis里面配置,这样的话spring-mybatis就不用加载
mybatis.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>
<!-- 配置分页插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>
</configuration>5.jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/ssm?serverTimezone=UTC
jdbc.user=root
jdbc.pwd=1234566.log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS}
%m (%F:%L) \n"/>
</layout>
</appender>
<logger name="java.sql">
<level value="debug"/>
</logger>
<!-- name 是范围-->
<!-- level是级别FATAL(致命)>ERROR(错误)>WARN(警告)>INFO(信息)>DEBUG(调试)-->
<logger name="org.apache.ibatis">
<level value="info"/>
</logger>
<root>
<level value="debug"/>
<appender-ref ref="STDOUT"/>
</root>
</log4j:configuration>三.测试
1.UserController.java
@Controller
public class UserController {
@RequestMapping("/")
public String index(){
// 不能直接获取员工信息,因为要获取页码数等传递到web
return "index";
}2.index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
《success》
<a href="/findAllUser">查询用户信息</a>
</body>
</html>3.结果

4.查用户结果也能出来

四.一些错误
1.注意这个注意这个注意这个!!!(如果缺少清除再重新打包)

2.之前没乱配置报的错
Error creating bean with name 'xxxBizImpl': Unsatisfied dependency expressed through field 'xxxMapper';
理解
使用bean标签创建SqlSessionFactory对象,注入DataSource数据 相当于底层已经创建好SqlSession对象 交给Spring管理
使用bean标签创建MapperConfigurer对象,用于扫描dao包,交给Spring管理
spring 底层已经创建好SqlSession对象 交给Spring管理 所以你如果在mybatis里面配置
<mappers>
<package name="top.remained.mapper"/>
</mappers>
是不是有点多余,而且在mybatis里面配置这个,需要我们手动创建sqlSession对象 岂不是多此一举
所以在spring里面配置这个就行
<!-- 扫描自定义的Mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="top.remained.mapper"> </property>
</bean>
边栏推荐
- How powerful can top "hackers" be? Internet access without signal, expert: high-end operation!
- Deep analysis of integrated learning gbdt
- Exchange 2013 SSL证书安装文档
- 齐博建站指南(艾戈勒)
- Working principle of fastdfs (technical principle)
- AUTOCAD——Excel表格导入CAD、CAD合并两兄弟
- 毕业三年之际写给可能迷茫的你我[转]
- OpenCV宏定义
- 你学过的每样东西,都会在你一生中的某个时刻派上用场(转)
- Worthington核糖核酸测定详细攻略
猜你喜欢

【CNN】为什么CNN的卷积核大小一般都是奇数

通配符 SSL/TLS 证书

电脑不知卸载什么,打不开计算器无法编辑截图功能打不开txt文件等等解决方案之一

脲酶丨Worthington杰克豆脲酶的特性及测定方案

Use pytoch to quickly train the network model

Pycharm new project

Pagoda phpMyAdmin unauthorized access vulnerability

Class, leetcode919 -- complete binary tree inserter

Solve thread safety problems & singleton mode

Leetcode59. 螺旋矩阵 II
随机推荐
Zabbix 5.0 使用自带Redis模版监控
How to add the index of a set in mongodb to another set in mongodb
电商数据模型设计
Jincang database kingbasees client programming interface guide ODBC (2. Overview)
Worthington核糖核酸测定详细攻略
Worthington丨Worthington胰蛋白酶抑制剂说明书
Mongodb index add, view, export, delete
Where is the DP interface of desktop computer (what if the host has no DP interface)
Best practices for migration of kingbasees v8.3 to v8.6 of Jincang database (2. Compatibility of kingbasees v8.3 and v8.6)
Use pytoch to quickly train the network model
A new generation of ultra safe cellular battery, Sihao aipao, will be available from 139900 yuan
Classic topological sorting problem -- leetcode207 curriculum +leetcode210 curriculum II
【C】逆序字符串(俩种递归思路)
齐博建站指南(艾戈勒)
数据中台的那些“经验与陷阱”
Multisensor fusion positioning (III) -- inertial technology
有效供应链管理的八大绩效分析指标(上)
PowerCLi VMware vCenter 通过自建的PXE Server一键批量部署常规New-VM
PHP 海报二维码合成
hutool官网(hutool好用吗)