用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數據

 

測試通過

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM