SSM integrated learning BUG summary

Table of contents

一、Result Maps collection already contains value for...

2. org.apache.jasper.JasperException: Failed to load or instantiate TagLibraryValidator class: [org.apache.taglibs.standard.tlv.JstlCoreTLV]

3. The browser sends an AJAX request without refreshing the page.

4. Regarding the issue of how to directly send PUT and other requests with AJAX requests


一、Result Maps collection already contains value for...

        The reason for this exception

  • Reverse engineering configuration issues
  • Spring integration MyBatis configuration issues
  • Multiple generation of reverse engineering leads to code appending
  • MyBatis related files are not packaged in the targer directory.

1. First check the reverse engineering generated file

        Pay attention to whether the database connection information and the generation strategy of MyBatis related files are configured correctly.

        Because if there are many local database tables with the same name (perhaps the existing tables have already created corresponding beans through reverse engineering), then creating the same beans this time will cause conflicts.

        

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>

    <!--
            targetRuntime: 执行生成的逆向工程的版本
                MyBatis3Simple: 生成基本的CRUD(清新简洁版)
                MyBatis3: 生成带条件的CRUD(奢华尊享版)
    -->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--设置生成出来的文件无注释信息-->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!-- 数据库的连接信息 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/ssm_crud?serverTimezone=UTC"
                        userId="......"
                        password="........">
        </jdbcConnection>
        <!-- javaBean的生成策略-->
        <javaModelGenerator targetPackage="com.pengpeng.crud.bean" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />  <!--enableSubPackages:是否可以生成子包-->
            <property name="trimStrings" value="true" />   <!--trimStrings:将表中的字段名前后空格截取生成对应的属性名-->
        </javaModelGenerator>
        <!-- SQL映射文件的生成策略 -->
        <sqlMapGenerator targetPackage="com.pengpeng.crud.dao" targetProject=".\src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- Mapper接口的生成策略 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.pengpeng.crud.dao" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 逆向分析的表 -->
        <!-- tableName设置为*号,可以对应所有表,此时不写domainObjectName -->
        <!-- domainObjectName属性指定生成出来的实体类的类名 -->
        <table tableName="t_emp" domainObjectName="Employee"/>
        <table tableName="t_dept" domainObjectName="Department"/>

    </context>

</generatorConfiguration>

2. Check the configuration related to MyBatis integration in the spring configuration file.

  • Is the attribute value of SqlSessionFactoryBean's configLocation attribute (the path to the MyBatis core configuration file
    ) configured correctly? Classpath must be added before the configuration file name.
  • The data source of SqlSessionFactoryBean must be set
  • The name of the directory where the mapper mapping file is located should be consistent with the name of the directory where the mapper interface is located, and be pointed out with the <mappers/> tag in the core configuration file of MyBatis (this can save the configuration of the mapperLocations attribute of SqlSessionFactoryBean )
<!--配置 SqlSessionFactoryBean,可以直接在Spring的IOC容器中获取 SqlSessionFactory 对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--设置MyBatis核心配置文件的路径-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!-- 设置数据源 -->
        <property name="dataSource" ref="dataSource"></property>
<!--        &lt;!&ndash;指定mybatis,mapper文件位置&ndash;&gt;-->
<!--        <property name="mapperLocations" value="classpath:mappers/*.xml"></property>-->
    </bean>

    <!-- 配置一个可以执行批量的sqlSession -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
        <constructor-arg name="executorType" value="BATCH"></constructor-arg>
    </bean>

    <!--
        配置mapper接口的扫描,可以将指定包下的所有的mapper接口
        通过IOC容器中已经配置好的SqlSessionFactoryBean所创建的sqlSession对象
        来为我们创建对应的代理实现类对象,并将这些对象交给IOC容器管理
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.pengpeng.crud.dao"></property>
    </bean>
  • It can also be understood that if the name of the directory where the mapper mapping file is located is consistent with the name of the directory where the mapper interface is located, and the scanning of SqlSessionFactoryBean and mapper interface has been configured, then the scanning of configuring the mapper interface will already put the mapper implementation class object into the IOC. The container manages and finds the corresponding mapping file, so there is no need to configure the mapperLocations attribute.

3. Generate reverse engineering multiple times

        If you generate reverse engineering multiple times, you must completely delete the previously generated one, otherwise conflicts will occur, and the reverse engineering generated again must be checked. It is possible that code was added when generating the reverse engineering, resulting in multiple errors. Repeated code ( such as the repeated id attribute of resultMap ) needs to be retained only once .

4. MyBatis related files are not packaged in the targer directory.

        Check whether MyBatis related files are packaged in the targer directory. If not, clear the maven project and repackage it.

2. org.apache.jasper.JasperException: Failed to load or instantiate TagLibraryValidator class: [ org.apache.taglibs.standard.tlv.JstlCoreTLV ]

        This error may be caused by the lack of jstl jar package.

        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

3. The browser sends an AJAX request without refreshing the page.

        If this happens and there is no problem with the front-end and back-end codes, you must pay attention to the path and parameters of the ajax request. There cannot be spaces.

function to_page(pageNum) {
        $.ajax({
            url:"${APP_PATH}/emps",
            data:"pageNum=" + pageNum,
            type:"GET",
            success:function (result){
                //1、解析并显示员工数据
                build_emps_table(result)
                //2、解析并显示分页信息
                build_page_info(result)
                //3、解析分页条数据
                build_page_nav(result)
            }
        })
    };

        

4. Regarding the issue of how to directly send PUT and other requests with AJAX requests

The request method for the update operation in a REST-style request is PUT, but Tomcat can only parse post requests by default. In this case, an additional " _method         " parameter needs to be passed in the request parameters to indicate the actual request method to be sent and then be used by web.xml Parsed by the HiddenHttpMethodFilter filter configured in.

        At this time, if you use AJAX to directly send a PUT request, there will be a situation where there is data in the request body but the Bean object data cannot be encapsulated , such as:

        Employee[empId=1014, empName=null, gender=null, email=null, dId=null]

        At this time, the sql in the EmployeeMapper automatically generated by reverse engineering will be spliced ​​into:

                update t_emp where emp_id = 1014;

The reason is

        Tomcat gets data:

1. Encapsulate the data in the request body into a map.

2. request.getParameter("empName") will get the value from this map.

3. When SpringMVC encapsulates the POJO object, it will obtain the value of each attribute in the POJO using request.getParamter("email");

        AJAX directly sends a PUT request. The data in the request body cannot be obtained by request.getParameter("empName"). When Tomcat sees that it is a PUT request, it will not encapsulate the data in the request body as a map. Only requests in the form of POST are encapsulated. The request body is map

solution

We need to be able to support direct sending of requests such as PUT and also encapsulate the data in the request body.

1. Configure the HttpPutFormContentFilter filter in web.xml;

2. Its role: parse and package the data in the request body into a map.

3. The request is repackaged, request.getParameter() is rewritten, and the data will be obtained from the map encapsulated by itself.

<filter>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

Guess you like

Origin blog.csdn.net/weixin_64709241/article/details/128762819