commit c9c380e0ae3e7f6ac38792750e07af3a371efb66 Author: yaclty Date: Tue Apr 6 19:41:08 2021 +0800 first commit diff --git a/annotation/.idea/.gitignore b/annotation/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/annotation/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/annotation/.idea/compiler.xml b/annotation/.idea/compiler.xml new file mode 100644 index 0000000..ca67c5c --- /dev/null +++ b/annotation/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/annotation/.idea/encodings.xml b/annotation/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/annotation/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/annotation/.idea/jarRepositories.xml b/annotation/.idea/jarRepositories.xml new file mode 100644 index 0000000..b074d70 --- /dev/null +++ b/annotation/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/annotation/.idea/misc.xml b/annotation/.idea/misc.xml new file mode 100644 index 0000000..919019a --- /dev/null +++ b/annotation/.idea/misc.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/annotation/.idea/uiDesigner.xml b/annotation/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/annotation/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/annotation/pom.xml b/annotation/pom.xml new file mode 100644 index 0000000..f9c53f4 --- /dev/null +++ b/annotation/pom.xml @@ -0,0 +1,98 @@ + + + + 4.0.0 + + org.example + spring_annotation + 1.0-SNAPSHOT + war + + spring_annotation Maven Webapp + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.11 + test + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + org.springframework + spring-webmvc + 5.3.3 + + + log4j + log4j + 1.2.17 + + + org.projectlombok + lombok + 1.18.18 + + + com.fasterxml.jackson.core + jackson-databind + 2.12.2 + + + com.fasterxml.jackson.core + jackson-core + 2.12.2 + + + + + spring_annotation + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-war-plugin + 3.2.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + + diff --git a/annotation/spring_annotation.iml b/annotation/spring_annotation.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/annotation/spring_annotation.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/annotation/src/main/java/org/example/spring_annotation/WebInit.java b/annotation/src/main/java/org/example/spring_annotation/WebInit.java new file mode 100644 index 0000000..3a057b2 --- /dev/null +++ b/annotation/src/main/java/org/example/spring_annotation/WebInit.java @@ -0,0 +1,44 @@ +package org.example.spring_annotation; + +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.ContextLoaderListener; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.filter.CharacterEncodingFilter; +import org.springframework.web.servlet.DispatcherServlet; + +import javax.servlet.*; +import java.util.EnumSet; + +public class WebInit implements WebApplicationInitializer { + private Logger logger = LogManager.getLogger(WebInit.class); + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + logger.info("-------->1.开始启动web应用"); + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + + // spring-mvc 上下文 + context.register(WebMvcConfig.class); + context.setServletContext(servletContext); + + /* + * 创建并注册 DispatcherServlet + */ + DispatcherServlet dispatcherServlet = new DispatcherServlet(context); + ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcherServlet", dispatcherServlet); + dynamic.setLoadOnStartup(1); + dynamic.addMapping("/"); + + servletContext.addListener(new ContextLoaderListener(context)); + + /* + * CharacterEncodingFilter: 解决中文乱码问题 + */ + CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); + characterEncodingFilter.setEncoding("utf-8"); + FilterRegistration encodingFilter = servletContext.addFilter("characterEncodingFilter", characterEncodingFilter); + encodingFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); + logger.info("-------->2.web应用启动完成."); + } +} diff --git a/annotation/src/main/java/org/example/spring_annotation/WebMvcConfig.java b/annotation/src/main/java/org/example/spring_annotation/WebMvcConfig.java new file mode 100644 index 0000000..488a7e1 --- /dev/null +++ b/annotation/src/main/java/org/example/spring_annotation/WebMvcConfig.java @@ -0,0 +1,43 @@ +package org.example.spring_annotation; + +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; +import org.springframework.http.MediaType; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.stereotype.Controller; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import java.util.ArrayList; +import java.util.List; + +@Configuration +@EnableWebMvc +@ComponentScan(basePackages = "org.example.spring_annotation", + includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class})} +) +public class WebMvcConfig implements WebMvcConfigurer { + private static final Logger logger = LogManager.getLogger(WebMvcConfig.class); + + @Override + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + logger.info("MVC配置:启用静态资源解析-------->DefaultServletHandlerConfigurer"); + configurer.enable(); + } + + @Bean + public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { + logger.info("MVC配置:JSON解析-------->MappingJackson2HttpMessageConverter"); + MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); + List mediaTypes = new ArrayList(); + mediaTypes.add(new MediaType("utf-8")); + converter.setSupportedMediaTypes(mediaTypes); + ; + return converter; + } +} diff --git a/annotation/src/main/java/org/example/spring_annotation/controller/MyServlet.java b/annotation/src/main/java/org/example/spring_annotation/controller/MyServlet.java new file mode 100644 index 0000000..3a5e0de --- /dev/null +++ b/annotation/src/main/java/org/example/spring_annotation/controller/MyServlet.java @@ -0,0 +1,23 @@ +package org.example.spring_annotation.controller; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + +@WebServlet(name = "MyServlet", value = "/ttt") +@MultipartConfig(fileSizeThreshold = 1024 * 102, maxFileSize = 1024 * 1024 * 5, maxRequestSize = 1024 * 1024 * 5 * 5) +public class MyServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.getWriter().println("need post"); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.getWriter().println("he he MyServlet"); + Part pic = request.getPart("pic"); + pic.write("/Users/yaclty/temp/exe/a.jpg"); + response.getWriter().println("success"); + } +} diff --git a/annotation/src/main/java/org/example/spring_annotation/controller/SC.java b/annotation/src/main/java/org/example/spring_annotation/controller/SC.java new file mode 100644 index 0000000..a9ea2bb --- /dev/null +++ b/annotation/src/main/java/org/example/spring_annotation/controller/SC.java @@ -0,0 +1,22 @@ +package org.example.spring_annotation.controller; + +import org.example.spring_annotation.po.User; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class SC { + + @RequestMapping("/sc-test") + @ResponseBody + public String testSc() { + return "spring-test-controller"; + } + + @RequestMapping("/sc-test-json") + @ResponseBody + public User testUser() { + return new User(1, "张三"); + } +} diff --git a/annotation/src/main/java/org/example/spring_annotation/controller/TestController.java b/annotation/src/main/java/org/example/spring_annotation/controller/TestController.java new file mode 100644 index 0000000..46462f4 --- /dev/null +++ b/annotation/src/main/java/org/example/spring_annotation/controller/TestController.java @@ -0,0 +1,31 @@ +package org.example.spring_annotation.controller; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet( + urlPatterns="/test",name="TestController", + loadOnStartup = 1 +) +public class TestController extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.getWriter().println("he he test"); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + doGet(req,resp); + } + + @Override + public void init() throws ServletException { + super.init(); + System.out.println("TestController 开始加载了"); + } +} diff --git a/annotation/src/main/java/org/example/spring_annotation/po/User.java b/annotation/src/main/java/org/example/spring_annotation/po/User.java new file mode 100644 index 0000000..8eceec6 --- /dev/null +++ b/annotation/src/main/java/org/example/spring_annotation/po/User.java @@ -0,0 +1,13 @@ +package org.example.spring_annotation.po; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class User { + private Integer id; + private String name; +} diff --git a/annotation/src/main/resources/log4j.properties b/annotation/src/main/resources/log4j.properties new file mode 100644 index 0000000..b74dec9 --- /dev/null +++ b/annotation/src/main/resources/log4j.properties @@ -0,0 +1,24 @@ +### 设置### +log4j.rootLogger = debug,stdout + +### 输出信息到控制抬 ### +log4j.appender.stdout = org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target = System.out +log4j.appender.stdout.layout = org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n + +### 输出DEBUG 级别以上的日志到=E://logs/error.log ### +log4j.appender.D = org.apache.log4j.DailyRollingFileAppender +log4j.appender.D.File = E://logs/log.log +log4j.appender.D.Append = true +log4j.appender.D.Threshold = DEBUG +log4j.appender.D.layout = org.apache.log4j.PatternLayout +log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n + +### 输出ERROR 级别以上的日志到=E://logs/error.log ### +log4j.appender.E = org.apache.log4j.DailyRollingFileAppender +log4j.appender.E.File =E://logs/error.log +log4j.appender.E.Append = true +log4j.appender.E.Threshold = ERROR +log4j.appender.E.layout = org.apache.log4j.PatternLayout +log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n \ No newline at end of file diff --git a/annotation/src/main/webapp/WEB-INF/web.xml b/annotation/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..d144652 --- /dev/null +++ b/annotation/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,12 @@ + + + + + + + Archetype Created Web Application + diff --git a/annotation/src/main/webapp/index.jsp b/annotation/src/main/webapp/index.jsp new file mode 100644 index 0000000..d53e020 --- /dev/null +++ b/annotation/src/main/webapp/index.jsp @@ -0,0 +1,10 @@ + + + +

Hello World!

+
+ + +
+ + diff --git a/xml/.idea/.gitignore b/xml/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/xml/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/xml/.idea/compiler.xml b/xml/.idea/compiler.xml new file mode 100644 index 0000000..d9e3477 --- /dev/null +++ b/xml/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/xml/.idea/dataSources.xml b/xml/.idea/dataSources.xml new file mode 100644 index 0000000..363378d --- /dev/null +++ b/xml/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306 + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/xml/.idea/encodings.xml b/xml/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/xml/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/xml/.idea/jarRepositories.xml b/xml/.idea/jarRepositories.xml new file mode 100644 index 0000000..b074d70 --- /dev/null +++ b/xml/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/xml/.idea/misc.xml b/xml/.idea/misc.xml new file mode 100644 index 0000000..919019a --- /dev/null +++ b/xml/.idea/misc.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/xml/.idea/uiDesigner.xml b/xml/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/xml/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/xml/pom.xml b/xml/pom.xml new file mode 100644 index 0000000..61b5b7c --- /dev/null +++ b/xml/pom.xml @@ -0,0 +1,90 @@ + + + + 4.0.0 + + org.example + untitled + 1.0-SNAPSHOT + war + + untitled Maven Webapp + + + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.11 + test + + + org.projectlombok + lombok + 1.18.18 + + + org.springframework + spring-webmvc + 5.3.3 + + + javax.servlet + javax.servlet-api + 3.0.1 + provided + + + com.fasterxml.jackson.core + jackson-databind + 2.12.2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/xml/src/main/java/org/example/controller/TestController.java b/xml/src/main/java/org/example/controller/TestController.java new file mode 100644 index 0000000..624f7bb --- /dev/null +++ b/xml/src/main/java/org/example/controller/TestController.java @@ -0,0 +1,28 @@ +package org.example.controller; + +import org.example.po.User; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +@RequestMapping("/test") +public class TestController { + + @RequestMapping("/t1") + public String t1(){ + return "test"; + } + @RequestMapping("/t2") + @ResponseBody + public String test2(){ + return "aaa"; + } + + @RequestMapping("/user/{id}") + @ResponseBody + public User findUser(@PathVariable("id") Integer id){ + return new User(id,"test",null,"10086"); + } +} diff --git a/xml/src/main/java/org/example/po/Job.java b/xml/src/main/java/org/example/po/Job.java new file mode 100644 index 0000000..e8366cb --- /dev/null +++ b/xml/src/main/java/org/example/po/Job.java @@ -0,0 +1,6 @@ +package org.example.po; + +public class Job { + private int id; + private String jobName; +} diff --git a/xml/src/main/java/org/example/po/User.java b/xml/src/main/java/org/example/po/User.java new file mode 100644 index 0000000..8f601db --- /dev/null +++ b/xml/src/main/java/org/example/po/User.java @@ -0,0 +1,15 @@ +package org.example.po; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class User { + private Integer id; + private String username; + private Job job; + private String phone; +} diff --git a/xml/src/main/resources/app.xml b/xml/src/main/resources/app.xml new file mode 100644 index 0000000..f0bdb66 --- /dev/null +++ b/xml/src/main/resources/app.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/xml/src/main/webapp/WEB-INF/views/aaa.jsp b/xml/src/main/webapp/WEB-INF/views/aaa.jsp new file mode 100644 index 0000000..4c3d668 --- /dev/null +++ b/xml/src/main/webapp/WEB-INF/views/aaa.jsp @@ -0,0 +1,16 @@ +<%-- + Created by IntelliJ IDEA. + User: yaclty + Date: 2021/4/6 + Time: 3:39 下午 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +aaaaa + + diff --git a/xml/src/main/webapp/WEB-INF/views/test.jsp b/xml/src/main/webapp/WEB-INF/views/test.jsp new file mode 100644 index 0000000..0864eab --- /dev/null +++ b/xml/src/main/webapp/WEB-INF/views/test.jsp @@ -0,0 +1,16 @@ +<%-- + Created by IntelliJ IDEA. + User: yaclty + Date: 2021/4/6 + Time: 3:39 下午 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +sssss + + diff --git a/xml/src/main/webapp/WEB-INF/web.xml b/xml/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..2468fc0 --- /dev/null +++ b/xml/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,22 @@ + + + + Archetype Created Web Application + + + Spring + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + classpath:app.xml + + 1 + + + Spring + / + + + diff --git a/xml/src/main/webapp/a.html b/xml/src/main/webapp/a.html new file mode 100644 index 0000000..bc06ced --- /dev/null +++ b/xml/src/main/webapp/a.html @@ -0,0 +1,12 @@ + + + + + + Document + + +

xxx

+ + \ No newline at end of file diff --git a/xml/src/main/webapp/index.jsp b/xml/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/xml/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

Hello World!

+ + diff --git a/xml/untitled.iml b/xml/untitled.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/xml/untitled.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file