需求:在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 测试成功