SSM开发web再议+404排错

1. 大体流程

step 1. 创建数据库
step 2. 利用IDEA创建一个maven程序,引入web框架支持
step 3. 导入MySQL数据库
step 4. 引入相关依赖 pom.xml
step 5. 编写配置文件

5.1 database.properties
5.2 mybatis-config.xml
5.3 applicationContext.xml
5.4 spring-dao.xml
5.5 spring-service.xml
5.6 spring-mvc.xml

step 6. pojo层、dao层、service层、controller层代码编写
step 7. 配置web.xml
step 8. utils工具类、config配置类编写(可有可无)

文件结构:
参考之前文章:SSM+MySQL实现简易图书管理系统

2. 创建数据库

use smartCS;

CREATE TABLE IF not EXISTS `query`(
	`id` int auto_increment,
	`description` VARCHAR(100) not NULL,
	`keywords` VARCHAR(100) not null,
	`answer` VARCHAR(100) not null,
	`date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
	PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=UTF8;

INSERT INTO `query`
(`description`,`keywords`,`answer`)
VALUES
("请问什么时候可以收到货?","时候;时间;收;收货时间","一般发货后两天内可以送达。"),
("可以退货吗?","退;退货","不支持的,亲!");

3. 配置pom.xml

3.1 导入依赖

<!--依赖:junit,数据库去掉、连接池、servlet、jsp、mybatis、mybatis-spring、spring-->
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.2</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.17</version>
    </dependency>

    <!--数据库连接池-->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.2</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.6</version>
    </dependency>


    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.7</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.3.7</version>
    </dependency>



    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.12</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
</dependencies>

3.2 静态资源导出

<!--静态资源导出-->
<build>
    <resources>
        <resource>
            <directory>src/main/java/</directory>
            <includes>
                <include>**/*.xml</include>
                <include>**/*.properties</include>
            </includes>
            <!--                <filtering>true</filtering>-->
        </resource>
    </resources>
</build>

4. 编写配置文件

4.1 database.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.56.10:3306/mybatis?userSSL=true&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root

4.2 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>
    <typeAliases>
        <package name="com.ling.pojo"/>
    </typeAliases>
    <mappers>
<!--        <mapper resource="com.ling.dao.QueryMapper"/>-->
    </mappers>
</configuration>

4.3 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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--    Model-->
    <import resource="classpath:spring-dao.xml"/>
    <!--    Controller-->
    <import resource="classpath:spring-service.xml"/>
    <!--    View-->
    <import resource="classpath:spring-mvc.xml"/>

    <!--拦截器配置-->
    <!--    <mvc:interceptors>-->
    <!--        <mvc:interceptor>-->
    <!--            <mvc:mapping path="/**"/>-->
    <!--            <bean class="com.ling.config.LoginInterceptor"/>-->
    <!--        </mvc:interceptor>-->
    <!--    </mvc:interceptors>-->
</beans>

4.4 spring-dao.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:contex="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--整合MyBatis-->
    <!--1.关联数据库配置文件-->
    <contex:property-placeholder location="classpath:database.properties"/>

    <!--2.连接池
        dbcp: 半自动化 不能自动连接
        c3p0: 自动化操作(自动化的加载配置文件 并且可以自动设置在对象中)
        druid:
        hikari:
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <!--c3p0的私有属性-->
        <!--最大/小连接池-->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!--关闭连接后不自动commit-->
        <property name="autoCommitOnClose" value="false"/>
        <!--获取连接超时时间-->
        <property name="checkoutTimeout" value="10000"/>
        <!--当获取连接失败重试次数-->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>

    <!--3.SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--绑定mybatis的配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--4. 配置dao的接口扫描包,动态的实现Dao接口可以注入到Spring容器中 自动装配-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--要扫描的Dao包-->
        <property name="basePackage" value="com.ling.dao"/>
    </bean>
</beans>

4.5 spring-service.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:contex="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="classpath:spring-dao.xml"/>
    <!--扫描service层的包(注解) 自动装配

        扫描类路径以查找带注释的组件,这些组件将自动注册为springbean。
        默认情况下,将检测Spring提供的@Component、@Repository、@Service、@Controller、@RestController、
        @ControllerAdvice和@Configuration原型。
        可以看到 service层并没有加注解,即,并没有交给spring托管,为什么也可以被查找到呢?
        原因:在本配置文件借助bean注入到IoC容器中,交给spring托管了 从而被扫描被,进而被自动装配。
             除了本方法,xml中可配置autowire属性实现自动装配 实验证明 确实可以!!!! 6的飞起!
        这件事证明了对象交给spring管理,但是创建、赋值并不是自然而然的 依然还是要自己操作的
    -->
    <contex:component-scan base-package="com.ling.service"/>

    <!--2. 将我们的所有业务类,注入到Spring 可以通过配置
        或者注解实现:@Service @Autowried 在 BookServiceImpl
    -->

    <!--利用set方法注入-->
<!--    <bean id="QueryService" class="com.ling.service.QueryServiceImpl">-->
<!--        <property name="queryMapper" ref="queryMapper"/>-->
<!--    </bean>-->

    <!--3.声明式事务配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--4.aop事务支持
        1. 配置通知
        2. 配置切入点
        3. 配置切面
    -->
</beans>

4.6 spring-mvc.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:contex="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://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">

    <!--映射器、适配器、视图解析-->
    <!--1.注解驱动-->
    <mvc:annotation-driven/>

    <!--2.静态资源过滤
        webapp 文件夹下有 css、img、js 等存放静态资源的文件夹。

        如果在 jsp 中直接引入静态资源会被 DispathcerServlet 拦截,从而不能被使用这些静态资源。
    -->
    <mvc:default-servlet-handler/>

    <!--3.扫描包 controller-->
    <contex:component-scan base-package="com.ling.controller"/>

    <!--4.视图解析-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

5. 配置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">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <!--与tomcat同时启动-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--将请求映射到对应的Servlet-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--乱码过滤-->
    <filter>
        <filter-name>encodingFilter</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>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--Session-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
</web-app>

6. 排错!

6.1. 404错误

在这里插入图片描述
此时,要检查编译好的文件,即out文件夹!
在这里插入图片描述
上图是404报错的文件夹。

正常的文件夹长这样!
上图正常的文件夹!

可以看出,报错的文件夹,编译之后缺了很多文件,比如,java文件、配置文件以及jsp网页。
所谓404,就是找不到文件,所以,接下来就是要让这些文件在编译的时候,进入到out文件夹中。
可是,怎么做呢?
具体步骤:
project structure --> artifacts --> output root.
即,把没有在里面的文件添加进去。
当然,有时候也可能会缺少相关依赖,此时要把依赖添加进去!

  1. 报错的工程结构长什么样?
    在这里插入图片描述

  2. 修改后的文件长什么样?
    在这里插入图片描述

  3. 修改后的结果
    网页正常显示!

  4. 心酸往事:
    当时没想到查看out文件夹,以为是工程创建过程可能出了错误,就在那重复又重复,甚至又打开狂神的视频看。想来,还是不理解其中原因所致。
    在这里插入图片描述

不过,最终解决了,也还可以了。

其他,比如500错误,已经在配置文件中改过了。下次,应该会一帆风顺了吧。

最后需要指出的是,改了之后如果结果没有变化,很可能是out中的文件没有改变,此时最好把out文件删除,重新编译!

最后的最后,需要指出,利用SpringBoot开发确实很方便,看看这一大堆配置文件!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值