spring4全注解web項目demo


記得沒接觸框架的時候,寫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-instance"
         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>
pom.xml
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插件配置


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM