1、springMVC和springboot启动项目差异
springboot是快速开发脚手架,内嵌有tomcat,jetty 和 undertow三种servlet容器,通过maven管理导入对应serlvet依赖包,在启动springboot时就会根据依赖包启动其中一个容器(默认是tomcat)。而springMVC搭建好后没有启动按钮,需要另外设置启动的容器,下图是springMVC启动流程:
以上流程中需要在springmvc配置中配置handlerMapping(处理器映射器)和handlerAdapter(处理器适配器),在web.xml配置前端控制器和视图解析器,拦截器也可以在此配置,本文拦截器主要处理utf-8编码问题。
2、springMVC项目配置
开发springMVC项目前需要准备idea、maven和tomcat工具。
2.1、导入springMVC依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency>
2.2、配置springMVC.xml
<?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/beans/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/beans/spring-context.xsd
">
<context:component-scan base-package="com.test.controller"/>
<!--mvc:annotation-driven功能整合了处理器映射器handlerMapping和HandlerAdapter处理器适配器-->
<mvc:annotation-driven/>
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->
<!--全部静态资源放行-->
<mvc:default-servlet-handler/>
<!-- 配置Html试图解析器 -->
<!-- <bean id="htmlViewResolver"-->
<!-- class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!-- <property name="order" value="0" />-->
<!-- <property name="viewClass"-->
<!-- value="com.jack.common.spring.HtmlResourceView" />-->
<!-- <property name="prefix" value="/WEB-INF/html/" />-->
<!-- <property name="suffix" value=".html"></property>-->
<!-- <property name="contentType" value="text/html;charset=UTF-8"></property>-->
<!-- </bean>-->
<!-- Jsp视图解析器 -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="1" />
<!-- 如果配置的有html视图解析器,不能用jstl的那个 -->
<!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> -->
<property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2.3、配置WEB_INF文件下web.xml配置文件
<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">
<!--用于读取spring原生配置,如整合mybatis的bean--SqlSessionFactory-->
<!--读取spring全局配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext.xml</param-value>
</context-param>
<!--加载spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端控制器,servlet和servletMapping标签的servlet-nam需要一致-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--springMVC配置文件的位置如resource文件夹,spring文件夹下的springMVC.xml-->
<param-value>classpath:spring/springMVC.xml</param-value>
</init-param>
<!--设置servlet的加载优先级别和容器是否在启动时加载该servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--设置编码过滤器处理乱码问题-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
如果java项目没有webapp文件,可以参考一下链接设置Idea怎么给java项目或模块添加webapp
3、配置tomcat服务器启动
4、测试项目运行
以上准备好,当前的文件目录如下图所示:
在controller文件夹写入接口如TestController.java
@Controller
public class TestController {
@RequestMapping("/")
public String test(){
return "../index";
}
}
由于在springMVC.xml中,配置的视图解析器解析的是WEB-INF/jsp/*.jsp,而index.jsp文件是在WEB-INF目录下,需要设置../用于返回上一层目录,
最后直接启动,idea会自动跳转tomcat配置server中的URL路径,这里controller的测试路径就是http://localhost:8080/springmvc/,因为@RequestMapping注解没有其他属性,如@RequestMapping("/test"),则测试路径为http://localhost:8080/springmvc/test
index.jsp内容如下
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>HelloWorld</title>
<a>HelloWorld</a>
</head>
<body>
</body>
</html>
最后页面展示为