搭建Spring MVC

作者: GentleWind | 来源:发表于2018-05-27 17:19 被阅读0次

    步骤一 使用intelliJ搭建maven项目并配置需要的jar包。

    新建项目

    如图慢慢下一步配置好以后生成文件,并配置pom.xml。

    pom.xml配置如下(也可从idea 直接添加module):

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>ssm</groupId>
        <artifactId>ssm</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <!-- maven构建编码格式 -->
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <!-- springframe 版本控制 -->
            <spring.version>4.3.13.RELEASE</spring.version>
        </properties>
    
        <dependencies>
            <!-- Spring start -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
        </dependencies>
    </project>
    

    步骤二 配置web.xml 和 applicationContext.xml

    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_3_1.xsd"
             version="3.1">
    
    
        <display-name>ssm</display-name>
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <!-- 不配置的话,默认寻找/WEB-INF/项目名-servlet.xml -->
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/applicationContext.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <context-param>
            <!-- 可以自己配置,默认寻找/WEB-INF/applicationContext.xml -->
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
    </web-app>
    

    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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- 扫描包目录的所有Controller -->
        <context:component-scan base-package="main.ftsp.controller"/>
        <mvc:annotation-driven/>
    
        <!-- 配置视图解析器,跳转jsp界面 -->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <!-- 需要新建对应路径的jsp目录 -->
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>
    

    步骤三 注解@Controller类并启动

    新建HelloController.java,如下:

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * description。
     *
     * @author rango
     *         2018-05-27 10:33
     **/
    @Controller
    public class HelloController {
    
        @RequestMapping("/hello")
        public String welcome(){
            System.out.println("进入controller!");
            return "hello";
        }
    }
    
    

    启动,访问地址:

    http://localhost:8080/项目名/hello.do

    相关文章

      网友评论

        本文标题:搭建Spring MVC

        本文链接:https://www.haomeiwen.com/subject/pcabjftx.html