搭建mvc框架分为以下步骤
一、首先创建一个maven web工程项目
然后在pom.xml文件中加入springmvc相关架包(注意版本的匹配)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.14.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.14.RELEASE</version>
</dependency>
二、去web.xml文件中配置中央处理器顺便配置一个编码过滤器
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 二、中央处理器(DispatcherServlet):映射器、适配器、视图解析器 -->
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
三、配置spring-web.xml(映射器,适配器,视图解析器)
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 开启注解扫描 .** 所有所有包以及子包-->
<context:component-scan base-package="com.zxy.**" />
<!-- 1、处理器映射器:HandlerMapping url==>handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 2.适配器: HandlerAdatper 调用 Handler==>Controller -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!--为了使用@DateTimeFormat 使用注解驱动来加载适配器和映射器 -->
<mvc:annotation-driven/>
<!-- 3、视图解析器: -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 解析jstl标签 -->
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- 动态页面的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 动态页面的后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
然后我们搭建一个用来跳转的页面和所跳转的jsp页面
@Controller
public class EdocEntrytroller {
@RequestMapping("/test")
public String test(){
return "index";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
spring mvc
</body>
</html>
配置好了之后开启tomcat
显示以下页面就是搭建好了
然后弄一个测试类去看看能不正常运行,
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-datasource.xml")
public class Test1 {
@Autowired
private EdocEntryMapper edocEntryMapper;
@Test
public void select(){
List <EdocEntry> list = edocEntryMapper.selectByExample(null);
System.out.println(list);
}
}
正常运行结果如下