簡介
創建Maven項目
一、配置pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springboot.sample</groupId>
<artifactId>spring-boot-sample</artifactId>
<packaging>war</packaging>
<version>1.0.0.0-SNAPSHOT</version>
<name>spring-boot-sample Maven Webapp</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 添加對JSP支持的依賴包 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>Central</id>
<name>Central</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>mvnrepository</id>
<name>mvnrepository</name>
<url>http://mvnrepository.com/</url>
</repository>
<repository>
<id>sonatype</id>
<name>sonatype</name>
<url>http://www.sonatype.org/nexus/</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、定義啓動文件
package com.mingde;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan //此爲開啓servlet包中的serlvet類
public class SpringBootSimpleApplication {
public static void main(String[] args){
SpringApplication.run(SpringBootSimpleApplication.class, args);
}
}
測試
定義控制器(只是讓頁面輸出返回的字符串而已)
package com.mingde.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/xs")
public class HelloController {
@RequestMapping("hello")
public String hello(){
return "Holle,SpringBoot";
}
}
測試
在地址欄上:http://localhost:8080/xs/hello
頁面輸入結果爲返回的字符串:Holle,SpringBoot
注意:@RestController相當於@Contrller+@ResponseBody
讓SpringBoot支持JSP
定義控制器
package com.mingde.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("JSP")
public class JSPController {
@RequestMapping("/hello")
public String hello(Model model)throws Exception{
Map<String,String> map=new HashMap<String,String>();
map.put("book1", "三國演義");
map.put("book2", "水滸傳");
map.put("book3", "西遊記");
map.put("book4", "紅樓夢");
model.addAttribute("book", map);
return "hello";
}
}
在classpath目錄下定義application.properties文件
測試:
在SpringBoot中定義Servlet
package com.mingde.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
@WebServlet(description="Servlet說明",urlPatterns="/xs/hello") //設置訪問的路徑,這裏的路徑可以於控制層的路徑一樣,這樣就會同時訪問Servlet和controller
public class HellowServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
System.out.println("HelloServlet正在運行中……");
}
}
package com.mingde;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan //此爲開啓servlet包中的serlvet類
public class SpringBootSimpleApplication {
public static void main(String[] args){
SpringApplication.run(SpringBootSimpleApplication.class, args);
}
}
測試:
地址欄:http://localhost:8085/xs/hello
控制檯:HelloServlet正在運行中……
在SpringBoot中定義過濾器
package com.mingde.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
@WebFilter(filterName="myFilter",urlPatterns="/*")//代表過濾所有的請求
public class MyFilter implements Filter {
public void init(FilterConfig fConfig) throws ServletException {
System.out.println("過濾器初始化……");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("過濾器MyFilter正在執行……");
chain.doFilter(request, response);
}
public void destroy() {
System.out.println("過濾器被銷燬……");
}
}
定義監聽器:
定義SevletContextListener監聽器
package com.mingde.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.annotation.WebListener;
@WebListener
public class ServletContextListener implements javax.servlet.ServletContextListener {
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("監聽器MyServletContextListener被銷燬");
}
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("監聽器MyServletContextListener初始化");
}
}
定義HttpSessionListener監聽器
package com.mingde.listener;
import javax.servlet.http.HttpSessionEvent;
public class HttpSessionListener implements javax.servlet.http.HttpSessionListener {
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("session被創建……");
}
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("session被銷燬……");
}
}