SpringBoot微框架
来源:互联网 发布:编程数字金字塔 编辑:程序博客网 时间:2024/06/07 17:11
简介
创建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和controllerpublic class HellowServlet extends HttpServlet {private static final long serialVersionUID = 1L; @Overridepublic 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;@WebListenerpublic 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被销毁……"); }}
阅读全文
0 0
- SpringBoot微框架
- SpringBoot框架初接触
- springboot框架组件
- java springBoot框架01
- SpringBoot集成MyBatis框架
- 便捷的springboot框架
- SpringBoot框架集成开发
- springboot+security框架整合
- 学习搭建springboot框架
- SpringBoot整合shiro框架
- SpringBoot+Mybatis框架Demo
- springboot 框架搭建
- springboot+mybatis+shiro框架
- 初次认识SpringBoot框架
- 初探SpringBoot,简单搭建一个SpringBoot框架
- 【微框架实战】Maven+SpringBoot 短信接口详解与Demo
- springboot框架下的上传下载
- Springboot 集成slf4j logback框架
- 【Python】类
- Dungeon Game
- Java基础之IO流
- Java时间获取
- java入门基础-01
- SpringBoot微框架
- Linux X Window 与命令行的切换
- 排序算法之冒泡排序--Java语言
- 操作系统-首次适应算法
- [Splay]BZOJ 1208——[HNOI2004]宠物收养所
- pip 安装 sklearn
- Windows 7 Professional
- JAVA | 29
- 【恶心的模拟 && 找时刻问题】CodeForces