SSM(SpringMVC+Spring+Mybatis)
Posted sloth-007
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM(SpringMVC+Spring+Mybatis)相关的知识,希望对你有一定的参考价值。
一、搭建SpringMVC
1、在maven项目中导入spring-webmvc依赖
<!-- springmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.8.RELEASE</version> </dependency>
2、配置web.xml文件,在web.xml文件中添加如下代码
<servlet> <servlet-name>HelloWeb</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <!-- 将springmvc核心配置文件位置修改到src/main/resources文件下 --> <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>HelloWeb</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
3、配置springmvc核心配置文件,在src/main/resources文件下创建一个springmvc.xml文件
文件名必须与web.xml文件中的<param-value>classpath:springmvc.xml</param-value>名一致
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!--扫描com.blb.controller下的注解 --> <context:component-scan base-package="com.blb.controller" /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
4、编写测试类
在src/main/java文件下创建com.blb.controller包,在包下创建一个TestController类
package com.blb.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestController //向浏览器返回 @ResponseBody //拦截地址 @RequestMapping("hello") public String test() return "hello world";
5、maven配置tomcat和jdk
<build> <finalName>SSM_T</finalName> <plugins> <!-- jdk配置 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- 配置apache提供的tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </build>
6、测试,启动maven项目
访问localhost:8080/项目名/hello
页面出现hello world 表示springmvc搭建成功
以上是关于SSM(SpringMVC+Spring+Mybatis)的主要内容,如果未能解决你的问题,请参考以下文章
后端SSM(Spring + SpringMVC + Mybatis)框架整合
SSM(Spring + Springmvc + Mybatis)框架面试题