SSM框架的簡單整合2-Spring+SpringMVC

需求:在Spring的基礎上整合SpringMVC

IDE工具:Eclipse

搭建環境:jdk1.8   tomcat8.5

目前的項目結構如下:

說明:在集成SpringMVC的時候,我將項目名稱改爲SSMTest2。

開整。

1.編寫SpringMVC的配置文件

1.1 配置文件內容如下:

<?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:aop="http://www.springframework.org/schema/aop"
       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-4.3.xsd
                                          http://www.springframework.org/schema/context 
                                          http://www.springframework.org/schema/context/spring-context-4.3.xsd
                                          http://www.springframework.org/schema/aop 
                                          http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <!-- 配置自動掃描的包,SpringMVC IOC 容器只掃描Controller -->
    <context:component-scan base-package="com.yzpt">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!-- 配置視圖解析器 -->
    <bean id="internalResourceViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 處理靜態資源,默認 Servlet 處理 -->
    <mvc:default-servlet-handler />

    <!-- 啓動 MVC 註解驅動 -->
    <mvc:annotation-driven />

    <!--啓用AOP -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

1.2 在配置中配置了aspectj,因此需要添加aspectj依賴,如下:

        <!-- 添加aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.2</version>
        </dependency>

 

2. 在web.xml中指定SpringMVC的加載路徑,並且配置字符編碼過濾器,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    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>SSMTest2</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 指定Spring配置文件的路徑,添加ContextLoader監聽器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 配置SpringMVC  -->
    <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:springMVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </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>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

 

3. 爲了方便測試,這裏添加了一個jsp頁面,如下圖:

test.jsp內容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
</head>
<body>
<div>書的id:${book.bookId }</div>
<div>書的姓名:${book.bookName }</div>
<div>書的作者:${book.writer }</div>
<div>書的出版社:${book.publishingHouse }</div>
</body>
</html>

頁面上使用了EL表達式,因此需要引入2個jar包,一個是Servlet 2.5,一個是jstl 1.2,如下:

        <!-- 添加servlet依賴-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <!-- 添加jstl依賴 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

4. 編寫BookController類,內容如下:

package com.yzpt.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yzpt.entity.Book;

@Controller
@RequestMapping
public class BookController {

    @RequestMapping(value ="/test" , produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getResult() {
        return "Hello,EveryOne,I'm ZhaZhaHui!\n大家好,我是渣渣輝!";
    }
    
    @RequestMapping(value ="/Test")
    public String getTestInfo(HttpServletRequest request) {
        Book book = new Book();
        book.setBookId("10086");
        book.setWriter("YZP");
        book.setBookName("論客戶的重要性");
        book.setPublishingHouse("HaoYiTec");
        request.setAttribute("book",book);
        return "test";
    }
}

5. 編寫好控制器後,啓動項目,成功,如下:

 

6. 測試:

6.1 在瀏覽器中輸入URL: http://localhost:8080/SSMTest2/test

      結果如下圖:

6.2 在瀏覽器輸入RUL: http://localhost:8080/SSMTest2/Test

      結果如下圖:

6.3 測試成功

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