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插件配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值