码迷,mamicode.com
首页 > 编程语言 > 详细

Maven-SpringMVC

时间:2018-02-26 17:49:06      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:SpringMVC maven

Maven-SpringMVC

1.创建maven工程

2.在pom.xml文件中添加依赖项
<!-- 固定spring版本号 -->
<properties>
    <springframe.version>4.3.9.RELEASE</springframe.version>
</properties>

<dependencies>
    <!-- Spring 依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springframe.version}</version>
    </dependency>

    <!-- Jsp,Servlet 相关依赖 -->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <!-- 提供范围 使用容器提供的jar -->
        <scope>provided</scope>
    </dependency>

    <!-- Jsp,Servlet 相关依赖 -->

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.1</version>
    </dependency>

</dependencies>

    3.创建文件 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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd">;

<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="org.clarck.web.controller"></context:component-scan>

<!-- 静态资源 -->
<mvc:resources location="/js/" mapping="/js/**" />
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

</beans>

4.配置web.xml , 加载 spring-mvc.xml
        <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:spring-mvc.xml</param-value>
    </init-param>
</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>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

    5.创建控制层代码 IndexController.java
        @Controller

@RequestMapping("/")
public class IndexController {

    @RequestMapping("index")
    public String index(){
            System.out.println("index");
            return "index";
    }
}
在地址栏输入http://localhost:8080/工程名/index就可访问index.jsp

Maven-SpringMVC

标签:SpringMVC maven

原文地址:http://blog.51cto.com/doctorcai/2073141

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!