搭建ssm项目+搭建过程中可能遇到的各种坑(idea+maven+spring+springMVC+mybatis)

本文详细介绍了如何使用IDEA创建基于Maven的SSM(Spring+SpringMVC+Mybatis)项目,包括从创建项目、设置目录结构、配置文件到设置Tomcat服务器的全过程。同时,文章还列举了搭建过程中可能遇到的配置文件找不到、Maven clean报错、首页无法跳转等常见问题,并提供了相应的解决方案。
摘要由CSDN通过智能技术生成

目录

一、在idea上搭建一个基于maven的web项目

1.1、点击file-->new--->project

1.2、在“New Project”页面左侧选中maven选项,我们创建的是一个最简单的maven项目,需要做的是勾选图示所示的“Create From Archetype”复选框,在下面的下拉选项中我们选择“webapp”,之后点击【Next】;

1.3、在接下来的面板中,我们填写maven的坐标,“groupId”,“artifactId”,以及“version”,其中groupId是公司域名的反写,而artifactId是项目名或模块名,而version就是该项目或模块所对应的版本号。填写完之后,点击【Next】;

1.4、在接下来的面板中选择你的本地的maven的安装路径以及配置maven时设置的setting文件,选择完成后点击【Next】;

1.5、在接下来的界面中填写项目名,填写完成后点击【Finish】;

2.开始创建项目的目录结构,创建的结构如下图

2.1 这是总体的结构

2.2 这个是展开的结构

3.创建各个配置文件

3.1  web.xml

3.2   spring-config.xml

3.3  spring-config-mvc.xml

3.4   spring-config-mybatis.xml

3.5  jdbc.properties

3.6  log4j.properties

3.7  UserMapper.xml

4. pom.xml

5.  设置project structure

6.  设置tomcat

7.启动和终止tomcat服务器

在搭建项目时可能遇到的问题

1.报错内容:

分析:出现以下报错原因是因为找不到配置文件,可能是因为以下2种原因引起的:

2.报错内容

分析:这是在maven进行clean的时候报的错(clean也就是删除target)

3.报错内容

4.如下在web.xml中设置了首页但是不能正确跳转


使用的编辑器:idea+maven

使用的框架ssm(spring+springMVC+mybatis)

使用的服务器:tomcat

创建整个项目的顺序:

1、在idea上搭建一个基于maven的web项目


1.1、点击file-->new--->project

1.2、在“New Project”页面左侧选中maven选项,我们创建的是一个最简单的maven项目,需要做的是勾选图示所示的“Create From Archetype”复选框,在下面的下拉选项中我们选择“webapp”,之后点击【Next】;

1.3、在接下来的面板中,我们填写maven的坐标,“groupId”,“artifactId”,以及“version”,其中groupId是公司域名的反写,而artifactId是项目名或模块名,而version就是该项目或模块所对应的版本号。填写完之后,点击【Next】;

1.4、在接下来的面板中选择你的本地的maven的安装路径以及配置maven时设置的setting文件,选择完成后点击【Next】;

 

 

1.5、在接下来的界面中填写项目名,填写完成后点击【Finish】;

2、开始创建项目的目录结构,创建的结构如下图

2.1、 这是总体的结构

2.2 、这个是展开的结构

3、创建各个配置文件

3.1 、 web.xml

<?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Archetype Created Web Application</display-name>

    <!--可以使其找到配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-config.xml</param-value>
    </context-param>

    <!-- 解决工程编码过滤器 -->
    <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>
    <!-- Spring配置(监听器) -->
   <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- SpringMVC配置 -->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/index.jsp</url-pattern><!--浏览器访问地址-->
    </servlet-mapping>

    <!-- 添加do请求 -->
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!-- 添加json请求 -->
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.json</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

3.2  、 spring-config.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
       default-autowire="byName">
    <context:component-scan base-package="com.job" />
    <aop:aspectj-autoproxy/>
    <!-- 属性文件读入 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:prop/*.properties</value>
            </list>
        </property>
    </bean>

    <import resource="classpath:spring/spring-*.xml"/>
</beans>

3.3  、spring-config-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:p="http://www.springframework.org/schema/p"
       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-3.1.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!--  自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器  -->
    <context:component-scan base-package="com.job.controller"/>
    <mvc:annotation-driven/>
    <!-- 避免IE执行AJAX时,返回JSON出现下载文件  -->
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <!--  启动SpringMVC的注解功能,完成请求和注解POJO的映射  -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter"/>
                <!--  JSON转换器  -->
            </list>
        </property>
    <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值