从今天开始学习松哥的视频并做笔记。
创建一个maven工程
然后点击next
到此一个maven项目搭建完成。
我们搭建的是web项目,所以需要在pom文件中加一个标签,指明这是一个web工程,如下:
然后点击项目按F4或者右键选择:
此时项目如下:
接下来就是添加SSM所需依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
接下来创建spring和springmvc的配置文件(必须添加了上面的依赖才能找到spring的配置文件选项):
可以以同样方法创建springmvc的配置文件,也可以直接复制粘贴修改配置文件名。
接下来就是配置两个配置文件
如下:
// spring的配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.juge" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
</beans>
sprinbmvc的配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.juge" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
由于还没有和Mybati整合,所以只需要配置包扫描就行
下面配置Web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<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>
<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:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
接下来就是写一个测试类测试一下,目录结构如下:
其中controller层代码:
package com.juge.comtroller;
import com.juge.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping(value = "/hello",produces = "text/html;charset=utf-8")
public String hello(){
return helloService.sayHello();
}
}
service层代码:
package com.juge.service;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
public String sayHello() {
return "hello java 极客技术";
}
}
接下来就是把项目部署到tomcat服务器,由于我的已经部署,所以存在,没有部署的不存在,点击Edit Configurations…。
最后点击apply。服务器部署完成。
接下来就是把项目跑起来:
可能会有点慢,耐心等待即可。当出现如图所示,表示项目已经在服务器跑起来了。
下面咱们访问:localhost:8080/hello
如下图: