spring4全註解web專案demo
阿新 • • 發佈:2018-12-10
記得沒接觸框架的時候,寫demo測試時真的很爽,新建web專案,然後隨便寫寫servlet隨便除錯
框架越來越多,配置記不得了,整合容易出問題,整合新東西越來越少了,不敢動了。
這是個spring4的全註解的專案,沒有任何功能,僅僅是spring+springMVC配置,測試通過
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instancepom.xml" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zhen</groupId> <artifactId>Spring4-WebSocket</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <spring.version>4.3.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- 安全起見,新增,這個對任務排程,jml、ftl等提供支援 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- jsp和servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!-- 為@Response等的json資料繫結提供支援 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.6</version> </dependency> </dependencies> <build> <plugins> <!-- 編譯外掛 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- tomcat 外掛 --> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.7.v20160115</version> <configuration> <httpConnector> <port>8080</port> <host>localhost</host> </httpConnector> <webApp> <contextPath>/spring4webSocket</contextPath> </webApp> </configuration> </plugin> </plugins> </build> </project>
package com.zhen.spring_websocket.config; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.*; import java.text.SimpleDateFormat; import java.util.List; /** * @author zhen * @Date 2018/12/10 15:19 */ @Configuration @EnableWebMvc //這個註解類似於 <mvc:annotation-driven/> public class SpringMVCConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry){ registry.addViewController("/").setViewName("/index"); } @Override public void configureViewResolvers(ViewResolverRegistry registry) { //spring4 預設不是jsp,而是Thymeleaf registry.jsp().prefix("/jsp").suffix(".jsp"); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //靜態資源處理 registry.addResourceHandler("/static/**").addResourceLocations("/static/"); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { //增加jackson 的支援,json轉換器,支援@RequestBody等 Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() .dateFormat(new SimpleDateFormat("yyyy-MM-dd")); converters.add(new MappingJackson2HttpMessageConverter(builder.build())); } }springMVC配置
package com.zhen.spring_websocket.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; /** * @author zhen * @Date 2018/12/10 16:01 */ @Configuration @ComponentScan("com.zhen.spring_websocket") @Import(SpringMVCConfig.class) public class ProductConfig { }spring配置
package com.zhen.spring_websocket.config; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; /** * @author zhen * @Date 2018/12/10 15:13 * servlet 3支援 */ public class MyWebApplicationInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { //Spring AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(ProductConfig.class); ctx.setServletContext(servletContext); //Spring MVC ServletRegistration.Dynamic registration = servletContext.addServlet("servletDispatcher", new DispatcherServlet(ctx)); registration.setLoadOnStartup(1); registration.addMapping("/"); } }啟用spring與springmvc
基於spring4,servlet 3
package com.zhen.spring_websocket.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * @author zhen * @Date 2018/12/10 17:32 */ @Controller @RequestMapping("/test") public class TestController { @RequestMapping("/do") @ResponseBody public String test(){ System.out.println("測試方法執行!"); return "ok!"; } }測試controller
專案結構:
參考:
spring文件:連結:https://pan.baidu.com/s/1zZj-HtKBKbRKjz7FyiXkUg 提取碼: 47gu ,這個可以從官網下載最新的文件版本
https://blog.csdn.net/hcwbr123/article/details/78422120 spring與springmvc的掃描重複問題
https://www.cnblogs.com/silfox/p/7659353.html jsp與servlet包的引入maven
https://www.cnblogs.com/csonezp/p/5315725.html 純註解spring4的demo
https://www.cnblogs.com/tibit/p/5400880.html jetty外掛配置