SSM系列1 SpringMVC框架搭建

创建一个空白maven项目

maven引入spring-webmvc依赖

<dependency>
   <groupId>org.springframework</groupId>
    <!--SpringMVC 最核心依赖-->
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.4.RELEASE</version>
</dependency>

配置web.xml

此文件刚创建的项目没有,自己创建即可,main目录下创建webapp/WEB-INF目录,WEB-INF下创建web.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">
    <!--SpringMVC最核心的servlet:DispatcherServlet-->
    <servlet>
        <servlet-name>ssm</servlet-name>
        <!--拦截请求,创建对应的Controller进行处理-->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <!--指明DispatcherServlet初始化时加载的配置文件-->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <!--启动程序时对Servlet进行加载 0:优先级最高-->
        <load-on-startup>0</load-on-startup>
    </servlet>
    <!--spring mvc 影射 ,启用上面的Servlet需要作以下配置-->
    <servlet-mapping>
        <servlet-name>ssm</servlet-name>
        <!-- / :拦截所有请求-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--此项配置为POST提交参数乱码处理配置,使用TOMCAT时必须配置,使用jetty时无须配置,jetty自带了乱码配置-->
    <filter>
        <filter-name>characterFilter</filter-name>
        <!--CharacterEncodeingFilter 将Post请求中的参数字符集设置为UTF-8-->
        <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>characterFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

上面代码里面有一行指明了DispatcherServlet初始化时加载的配置文件-applicationContext.xml,此文件新创建的项目也是没有的,需要自己创建,resources目录下创建applicationContext.xml

applicationContext.xml配置如下

<?xml version="1.0" encoding="UTF-8" ?>
<!--
//mvc命名空间,用于对mvc进行控制和配置
xmlns:mvc="http://www.springframework.org/schema/mvc"
//引入context命名空间,用于启用注解
xmlns:context="http://www.springframework.org/schema/context"
-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--启用Spring注解形式扫描对象-->
    <context:component-scan base-package="com.zhangyx"></context:component-scan>
    <!--启用SpringMVC的注解模式-->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <!--StirngHttpMessageConverter 用于设置文本类型http响应的设置-->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <!--配置支持的媒体类型(MIME-->
                <property name="supportedMediaTypes">
                    <list>
                        <!--响应输出的文本被浏览器作为html进行解释,使用字符集为UTF-8-->
                        <value>text/html;charset=utf-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!--将静态资源排队在外,用于提高执行效率【img,js,css等】-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>

测试

SpringMVC基本框架完成,很简单吧,简单测试一下
java目录下创建controller类,com.zhangyx.mvc.TestController,[包.类]

@Controller
public class TestController {
    @GetMapping("/test")
    @ResponseBody
    public String test(){
        return "success";
    }
}

直接访问localhost:80/test,输出success
我的端口是80,你的是什么写什么

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章