SpringBoot微框架

简介



创建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正在运行中……");
	}
	
}

在启动文件中添加@ServletComponentScan注解
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被销毁……");
    }
	
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章