当前位置:网站首页>Build SSM project with JSP as view parser
Build SSM project with JSP as view parser
2022-07-28 23:59:00 【Xinyou - Lin Xi】
Catalog
1. File directory (web If the service can't be built, just persuade them to quit )
4. The results of checking users can also come out edit
1. Pay attention to this, pay attention to this !!!( If there is a lack of cleanup, repack )
2. There was no wrong configuration before
One Environmental preparation
1. File directory (web If the service can't be built, just persuade them to quit )

2.tomcat8.5

notes : The following image is selected to realize the above hot deployment

3.maven rely on
notes : There is a paging plug-in that depends on , If you need it, you can write , Don't write if you don't need it
Why import logs , because mybatis It needs to realize the log function logback yes sl4j The implementation of the
<packaging>war</packaging>
<dependencies>
<!-- json rely on @RequestBody It may be used -->
<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>
<!-- Database driven -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!-- Database connection pool druid-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<!-- Paging plug-ins -->
<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 Integrate 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 The object manager class is in this dependency , Although with mybatis But you still need to import this dependency spring -jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.1</version>
</dependency>
<!-- journal -->
<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>
<!-- File upload dependency File download does not need -->
<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>
Two . The configuration file
1.web.xml
notes : Coding filter and front-end controller must have , The rest is just the convenience of realizing some functions .
<?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">
<!-- Set the encoding filter Get before get request , Getting after a request is meaningless -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<!-- Custom encoding -->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!-- Requests and responses are filtered -->
<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 for handling requests -->
<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>
<!-- To configure springMVC Front end controller for -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Initialize parameters Set up springMVC Location and name of -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- take DispatcherServlet Initialization is advanced until the server starts -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- To configure spring The monitor for effect Is loaded when the server starts spring Configuration file for load ioc Containers -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Set up spring Self defined location and name -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
</web-app>2.springmvc.xml
notes : The view parser and scanner must have , The rest depends on whether you need
jsp and themleaf A view parser is different . Other configurations are the same
<?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">
<!-- Scanner -->
<context:component-scan base-package="top.remained.controller">
</context:component-scan>
<!-- View parser object -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- Open access to static resources -->
<mvc:default-servlet-handler/>
<!-- When you open the top, you must open this Turn on mvc Annotation driven
First use DispatcherServlet analysis , If you can't resolve it, give it to the default servlet-->
<mvc:annotation-driven/>
<!-- web-INF Not accessible through browser or redirection No location name Because it will be parsed by the following view parser -->
<!-- Configure view controller -->
<!-- <mvc:view-controller path="/" view-name="index"/>-->
<!-- Profile upload parser id Fix -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
</beans>
3.spring-mybatis.xml
notes : Most configurations are mybatis What's inside (IOC), The reason why the scanning component removes the control layer is ,springmvc It's been scanned , Yes, but not necessarily
The process
1. load jdbc.property
<context:property-placeholder location="classpath:jdbc.properties"/>
2. To configure datasource With bean In the form of <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
3. To configure sql factory With bean In the form of <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
then sql factory Inside property Including the configured data source to ref="dataSource" <property name="dataSource" ref="dataSource"/> Reload property mybatis Configuration file for <property name="configLocation" value="classpath:mybatis.xml"/>
Scan annotation package <context:component-scan base-package="top.remained"/>
scanning mapper The package is automatically injected @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">
<!-- Scan components ( Remove control layer )-->
<context:component-scan base-package="top.remained">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- Import database configuration file -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- Configure data sources -->
<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>
<!-- Configure transaction manager The action faces the section -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- Turn on annotation driven for transactions Annotations will be used @Transactional Identify the way or all methods in the class to manage transactions -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<!-- You don't have to set it id Because I didn't create the object myself Can be directly in spring Of ioc In order to get SqlSessionFactory object -->
<!-- Put in mybatis All the configurations inside are configured by attributes , You don't have to mybatis The configuration file -->
<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"/>
<!-- If The package where the mapping file is located and resources The package name inside is the same , You don't have to set it -->
<!-- No, MapperScannerConfigurer This bean when This is different, no matter what All to set -->
<!-- <property name="mapperLocations" value="classpath:top/remained/mapper/*.xml"/>-->
</bean>
<!-- The scan is mapper Interface After configuring this spring Can pass ioc Automatic assembly of completely take over mapper Interface -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="top.remained.mapper"/>
</bean>
</beans>4.mybatis.xml
notes :mybatis Why is there only one paging plug-in , Because in spring-mybatis It has been configured ,
Paging plug-ins It can also be in spring-mybatis It's equipped with , In this case spring-mybatis You don't have to load
mybatis.xml This file , But I feel troublesome . Paging plug-ins can also be configured without
<?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>
<!-- Configure paging plug-ins -->
<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 Is the scope of -->
<!-- level Is the level FATAL( deadly )>ERROR( error )>WARN( Warning )>INFO( Information )>DEBUG( debugging )-->
<logger name="org.apache.ibatis">
<level value="info"/>
</logger>
<root>
<level value="debug"/>
<appender-ref ref="STDOUT"/>
</root>
</log4j:configuration>3、 ... and . test
1.UserController.java
@Controller
public class UserController {
@RequestMapping("/")
public String index(){
// Unable to obtain employee information directly , Because you need to get the number of page numbers and pass them to 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"> Query user information </a>
</body>
</html>3. result

4. The results of checking users can also come out

Four . Some mistakes
1. Pay attention to this, pay attention to this !!!( If there is a lack of cleanup, repack )

2. There was no wrong configuration before
Error creating bean with name 'xxxBizImpl': Unsatisfied dependency expressed through field 'xxxMapper';
understand
Use bean Tag creation SqlSessionFactory object , Inject DataSource data Equivalent to the bottom layer has been created SqlSession object hand Spring management
Use bean Tag creation MapperConfigurer object , For scanning dao package , hand Spring management
spring The bottom layer has been created SqlSession object hand Spring management So if you're mybatis It's equipped with
<mappers>
<package name="top.remained.mapper"/>
</mappers>
Is it a little redundant , And in mybatis This is configured inside , We need to manually create sqlSession object Isn't it unnecessary
So in spring Just configure this inside
<!-- Scan custom Mapper Interface -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="top.remained.mapper"> </property>
</bean>
边栏推荐
- EN 1873屋面用装配附件.塑料单个屋面灯—CE认证
- JS advanced ES6 ~ es13 new features
- fastdfs工作原理(技术原理)
- Classification and determination method of Worthington stemxyme
- SAP 临时表空间错误处理
- 多传感器融合定位(三)——惯性技术
- RHCE the next day
- 实时数仓:网易严选基于Flink的实时数仓实践
- Tyrosine decarboxylase -- characteristics of tyrosine decarboxylase of Streptococcus faecalis in Worthington
- Multi sensor fusion positioning (II) -- map based positioning
猜你喜欢

Pycharm new project

Leetcode64. 最小路径和

Worthington - chemical properties and related studies of Worthington trypsin

EN 1935建筑五金.单轴铰链—CE认证

CANoe应用案例之DoIP通信

Worthington丨Worthington胰蛋白酶抑制剂说明书

GhostNets on Heterogeneous Devices via Cheap Operations

Deep analysis of integrated learning xgboost

【C】替换空格,宏实现整数的二进制奇偶位交换

迅为IMX6开发板QT系统创建AP热点基于RTL8723-交叉编译iptables
随机推荐
EN 12101-8:2011 smoke dampers for smoke and heat control systems - CE certification
器利而工善,以RPA+LCAP赋能企业司库管理数字化升级
DoIP测试开发实践
Pycharm configuring the running environment
YOLOV5学习笔记(一)——原理概述
leetcode 763. Partition Labels 划分字母区间(中等)
Kingbasees client programming interface guide ODBC (4. Create data source)
软件设计师的错题汇总
GhostNets on Heterogeneous Devices via Cheap Operations
[data mining engineer - written examination] Dahua shares in 2022
【TA-霜狼_may-《百人计划》】图形3.6 纹理压缩——包体瘦身术
失败率高达80%,数字化转型如何正确完成战略规划?
Exchange 2013 SSL证书安装文档
使用Pytorch快速训练网络模型
以JSP为视图解析器搭建SSM项目
pycharm配置运行环境
【详细超简单】如何使用WebSocket链接
leetcode 763. Partition Labels 划分字母区间(中等)
Jincang database kingbasees client programming interface guide ODBC (2. Overview)
PowerCL 批量创建及管理虚拟交换机