用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建四:配置springmvc

用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试的基础上 继续进行springmvc的配置

一:配置完善web.xml文件

当前的web.xml文件是这样的

那么我们需要在这个基础上  加入springmvc的中心控制器和加载配置文件

<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"
         metadata-complete="true">

    <!--配置SpringKMVC重要控制器-->
    <servlet>
        <servlet-name>demo-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--
             配置springMVC需要加载的配置文件
             MyBatis->spring->springMVC
        -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-*.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>demo-dispatcher</servlet-name>
        <!--默认匹配所有请求-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

二:先继续晚上目录结构 在com.peakfortake下面加入controller文件夹  在\webapp\WEB-INF下建立page用于存放页面 在webapp下面建立css、javascript、images等文件夹并在 在resource的spring文件夹下面加入spring-web.xml  进行springmvc 的配置

<?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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
     ">
    <!--配置SpringMVC-->
    <!-- 1:开启SpringMVC注解模式-->
    <!-- 简化配置
        (1)自动注解DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
         (2) 提供了一系列的功能:如数据绑定,数字和日期的format
    -->
    <mvc:annotation-driven/>

    <!-- 2:servlet-mapping 映射路径:"/"-->
    <!--静态资源默认servlet配置
        1:加入对静态资源的处理  如js,png
        2:允许使用“\”做整体映射
    -->
    <mvc:default-servlet-handler/>

    <!--3:配置jsp显示ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/page/"/>
        <property name="suffix" value=".html"/>
    </bean>

    <context:component-scan base-package="com.peakfortake.controller" />
</beans>


三:基本配置已经完成

然后在page页面建立一个list.html文件 

我的controller层的测试代码如下

/**
 * Created by 草帽boy on 2016/11/28.
 */
@Controller
@RequestMapping(value = "/peakfortake")
public class TestUserController {

    @Autowired
    private TestUserService testUserService;

    @RequestMapping(value="/list",method = RequestMethod.GET)
    public String getListPage(){
        return "list";
    }

    @RequestMapping(value="/userList",method = RequestMethod.GET)
    @ResponseBody
    public List<TestUser> getList(){
        return testUserService.userList();
    }

}

四 配置tomcat

 

五  启动tomcat

peakfortake/list返回list页面

peakfortake/userList返回数据库中的json数据

 

测试通过

 

posted @ 2016-11-28 18:57  草帽boy  阅读(1989)  评论(0编辑  收藏  举报