《spring-boot学习》-01-初体验
来源:互联网 发布:linux rhel 环境变量 编辑:程序博客网 时间:2024/06/07 00:09
1.项目结构如下
2.pox.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>springBootTest1</groupId> <artifactId>springBootTest1</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springBootTest1 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.3.RELEASE</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j-core</artifactId> <version>1.4-dev-8</version> </dependency> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.9</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.2.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core /weixin_maven/src/main/resources/applicationContext.xml 这个文件要和本文件用的hibernate是同一个版本 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.10.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.10.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.3.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.3.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.3.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.9</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> <!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib --> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <!-- https://mvnrepository.com/artifact/xom/xom --> <dependency> <groupId>xom</groupId> <artifactId>xom</artifactId> <version>1.2.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.json/json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160810</version> </dependency> <!-- https://mvnrepository.com/artifact/jstl/jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 使用slf4j和logback收集日志 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.13</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.0.13</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>1.0.13</version> </dependency> <!-- import javax.servlet.ServletRegistration.Dynamic cannot be found Dynamic 这个类的引入 --> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-servlet_3.0_spec</artifactId> <version>1.0</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx)); --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build></project>
3.配置logback.xml日志配置文件
<?xml version="1.0" encoding="UTF-8"?><configuration scan="true" scanPeriod="1 seconds"> <contextListenter class="ch.gos.logback.classic.jul.levelChangePropagator"> <resetJUL>true</resetJUL> </contextListenter> <jmxConfigurator/> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>logback: %d{HH:mm:ss.SSS} %logger{36} - %msg%n</pattern> </encoder> </appender> <logger name="org.springframework.web" level="DEBUG"/> <root level="info"> <appender-ref ref="console" /> </root></configuration>
该配置可以在控制台看到打印如下
logback: 15:11:24.302 o.s.w.s.h.BeanNameUrlHandlerMapping - Rejected bean name 'applicationEventMulticaster': no URL paths identifiedlogback: 15:11:24.320 o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Feb 23 15:11:23 CST 2017]; root of context hierarchylogback: 15:11:24.370 o.s.w.c.s.AnnotationConfigWebApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@94bf0b5]logback: 15:11:24.375 o.s.web.servlet.DispatcherServlet - Unable to locate MultipartResolver with name 'multipartResolver': no multipart request handling providedlogback: 15:11:24.377 o.s.web.servlet.DispatcherServlet - Unable to locate LocaleResolver with name 'localeResolver': using default [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver@6f88c31e]logback: 15:11:24.380 o.s.web.servlet.DispatcherServlet - Unable to locate ThemeResolver with name '
4./springBootTest1/src/main/resources/views/index.jsp文件内容
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body>xxxxxxxxxxxxx</body></html>
5./springBootTest1/src/main/java/com/biluo/springmvc/config/MySpringMVCConfig.java
package com.biluo.springmvc.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.view.InternalResourceViewResolver;import org.springframework.web.servlet.view.JstlView;/* * springMVC的一些配置 */@Configuration @EnableWebMvc@ComponentScan("com.biluo") //这一点配置要扫描的包 要包括所有的包public class MySpringMVCConfig { @Bean public InternalResourceViewResolver viewResolver(){ InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); //页面映射的位置 这一点要配置成/WEB-INF/classes/views/而不是/WEB-INF/classes/views viewResolver.setPrefix("/WEB-INF/classes/views/"); viewResolver.setSuffix(".jsp"); //页面的后缀 viewResolver.setViewClass(JstlView.class); return viewResolver; }}
6./springBootTest1/src/main/java/com/biluo/springmvc/config/WebInitializer.java
package com.biluo.springmvc.config;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRegistration.Dynamic;import org.springframework.web.WebApplicationInitializer;import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;import org.springframework.web.servlet.DispatcherServlet;public class WebInitializer implements WebApplicationInitializer{ @Override public void onStartup(ServletContext servletContext) throws ServletException { // TODO Auto-generated method stub AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(MySpringMVCConfig.class); ctx.setServletContext(servletContext); /* * Your javaee/servlet.jar is outdated. Servletcontext#Addservlet wasn't introduced until servlet 3.0 spec and you are using 2.5. 这里的servlet-api.jar 要是3.0以上的 */ Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); }}
7。/springBootTest1/src/main/java/com/biluo/spring/controller/HellowController.java
package com.biluo.spring.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HellowController { @RequestMapping("/index2") public String hello(){ System.out.println("===============>>>>>>>>>>>>>>>>>>>"); return "index"; }}
9.访问http://localhost:8080/springBootTest1/index2
输出xxxxxxxxxxxxxx
0 0
- 《spring-boot学习》-01-初体验
- Spring Boot 初体验
- Spring-Boot初体验
- spring boot 初体验
- Spring Boot初体验
- Spring Boot初体验
- Spring Boot初体验
- spring boot 初体验
- spring boot之初体验
- Spring Boot初体验(一)
- Spring学习初体验
- Spring Security-01-初体验
- spring boot 学习--01--springboot开始
- spring boot 学习--08---搭建ssmm-01
- Spring boot 学习知识点
- spring-boot学习
- Spring Boot 学习(一)
- spring-boot学习笔记
- 【PAT】1016. Phone Bills
- 第三十四章 SpringBoot获取配置属性
- Android学习笔记——问题汇总
- 完美打印JSON
- 在自定义的 btrfs+subvolume 上安装 Deepin 15
- 《spring-boot学习》-01-初体验
- [BZOJ3509][CodeChef][FFT][分块]COUNTARI
- JS图片类之苹果导航效果
- 第三十五章 SpringBoot Profiles
- Java中四舍五入保留两位小数或不保留小数
- Material Design实战
- Map集合的遍历
- 遍历二叉树所有路径并求指定值
- 获取用户输入的数字,进行判断并输出相应的语句