IntelliJ IDEA创建Web SpringMVC项目(二):添加springMVC框架

1.添加springMVC依赖以及log依赖
  • spring-core
  • spring-beans
  • spring-context
  • spring-context-support
  • spring-web
  • spring-webmvc
  • spring-orm
  • spring-tx
  • spring-test
  • spring-security-web
  • jcl-over-slf4j
  • slf4j-api
  • slf4j-log4j12
  • log4j

在pom.xml里面配置所有依赖。


2.resources添加spring配置文件webapp-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd    http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
             http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <description>spring 业务层配置</description>

    <context:annotation-config/>


</beans>


3.resource添加springMVC配置文件  servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
             http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
             http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <description>spring web层配置</description>

    <mvc:annotation-driven/>

    <context:component-scan base-package="com.toughegg.webapp">
    </context:component-scan>

    <mvc:resources location="/statics/" mapping="/statics/**"
                   cache-period="31556926"/>


    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/pages"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>


4.web.xml配置对应的spring配置信息

<?xml version="1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:webapp-context.xml
        </param-value>
    </context-param>

    <!-- spring监听入口 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>



    <!-- spring MVC设置 -->
    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:servlet-context.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>


5.Project Structure设置对应的spring关系


6.项目包分层 bean  , dao , service ,  servliceImpl
IntelliJ IDEA创建Web SpringMVC项目(二):添加springMVC框架_第1张图片


7.测试小例子

package com.toughegg.webapp.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author cf
 * @description IntelliJ IDEA maven web springMVC测试
 * @create 2016-04-07 11:41
 **/

@Controller
public class IndexController {

    public IndexController() {
        System.out.println("=====================初始化============");
    }

    @RequestMapping(value = "/index")
    public String indexJSP() {
        return "/index";
    }
}



测试:

IntelliJ IDEA创建Web SpringMVC项目(二):添加springMVC框架_第2张图片


项目结构图:



你可能感兴趣的:(IntelliJ IDEA创建Web SpringMVC项目(二):添加springMVC框架)