记得没接触框架的时候,写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.</modelVersion> <groupId>com.zhen</groupId>
<artifactId>Spring4-WebSocket</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> <properties>
<spring.version>4.3..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.</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.</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.</version>
</dependency> </dependencies> <build>
<plugins>
<!-- 编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-</encoding>
</configuration>
</plugin> <!-- tomcat 插件 -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3..v20160115</version>
<configuration>
<httpConnector>
<port></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();
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插件配置

spring4全注解web项目demo的更多相关文章

  1. 【1】springmvc4 + servlet3 零配置全注解入门项目helloword

    自从servlet3.0支持注解,使得javaweb项目使用全注解成为可能. 注解化将成为javaweb的发展方向.包括spring去年推出的spring-boot也是全部使用注解. 代码:https ...

  2. spring4.2完整web项目(使用html视图解析器)

    完整配置springmvc4,最终视图选择的是html,非静态文件. 最近自己配置spring的时候,遇到很多问题,由于开发环境和版本的变化导致网友们给出的建议很多还是不能用的,可能还会有很多人会遇到 ...

  3. 初步配置基于Struts的maven的Web项目demo

    1.创建mavenweb项目 eclipse | Idea 中创建maven项目,具体步骤PASS. 2.导入Struts2核心架包 Idea中在maven项目上,选择ADD FRAMEWORK ec ...

  4. MyEclipse web项目导入Eclipse,详细说明

    最近导入一个MyEclipse的项目,具体是:spring4.1的web项目,同时遇到了一些问题,总结一下. 1.进入项目目录,找到.project文件,打开.增加一个<buildCommand ...

  5. 基于全注解的SpringMVC+Spring4.2+hibernate4.3框架搭建

    概述 从0到1教你搭建spring+springMVC+hibernate整合框架,基于注解. 本教程框架为基于全注解的SpringMVC+Spring4.2+hibernate4.3,开发工具为my ...

  6. 一款基于SSM框架技术的全栈Java web项目(已部署可直接体验)

    概述 此项目基于SSM框架技术的Java Web项目,是全栈项目,涉及前端.后端.插件.上线部署等各个板块,项目所有的代码都是自己编码所得,每一步.部分都有清晰的注释,完全不用担心代码混乱,可以轻松. ...

  7. Java全栈程序员之07:IDEA中使用MAVEN构架生产级的Web项目

    在上一篇我们介绍了如何在IDEA中使用MAVEN,以及如何创建依赖等.那么在这一篇中,我们就试图搭建一个生产级的解决方案,大家可以使用这个解决方案作为骨架代码来搭建自己的开发环境. 在这里,我们要完成 ...

  8. 【持久化框架】SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载

    上篇博文我们介绍了mybatis的基本概念与原理,这篇博文我们通过Spring与Mybatis集成,开发一个简单用户增删改查的Web项目. 基本准备工作 1.安装JDK1.6以上版本,安装与配置 2. ...

  9. 基于已构建S2SH项目配置全注解方式简化配置文件

    如果还不熟悉s2sh项目搭建的朋友可以先阅读 eclipse环境下基于tomcat-7.0.82构建struts2项目 eclipse环境下基于已构建struts2项目整合spring+hiberna ...

随机推荐

  1. 请问WCF 跟 WebService之间的相同跟异同

    https://social.msdn.microsoft.com/Forums/zh-CN/c06420d1-69ba-4aa6-abe5-242e3213b68f/wcf-webservice W ...

  2. You Don't Know JS: this & Object Prototypes( 第4章 Mixing "Class" Objects)

    本章移到“Object oriented programming”和"classes". 看‘class orientation‘ 的设计模式: instantiation, in ...

  3. You Don't Know JS: this & Object Prototypes( 第一章 this or That?)

    Foreword this 关键字和prototypes 他们是用JS编程的基础.没有他们创建复杂的JS程序是不可能的. 我敢说大量的web developers从没有建立过JS Object,仅仅对 ...

  4. KDD Cup 99网络入侵检测数据的分析

    看论文 该数据集是从一个模拟的美国空军局域网上采集来的 9 个星期的网络连接数据, 分成具有标识的训练数据和未加标识的测试数据.测试数据和训练数据有着不同的概率分布, 测试数据包含了一些未出现在训练数 ...

  5. wordpress +window 走起~

    一.安装XAMPP   1 百度搜索xampp后下载安装到D盘(安装时按默认勾选安装即可) 2 安装完后,点击Start来启动Apache和MySQL这两个服务 3 如果Apache服务不能启动,多数 ...

  6. JS代码判断IE6,IE7,IE8,IE9

    做网页有时候会用到JS检测IE的版本,下面是检测Microsoft Internet Explorer版本的三种代码! 有一种代码: <script type="text/javasc ...

  7. C/S,B/S的区别与联系

    C/S 是Client/Server 的缩写.服务器通常采用高性能的PC.工作站或小型机,并采用 大型数据库系统,如Oracle.Sybase.Informix 或SQL Server.客户端需要安装 ...

  8. python-前20天的着重知识点

    1.CPU存在两种工作状态:一种是内核态,操作系统在运行--可以操作硬件: 另一种是用户态,是应用软件在运行--不可以操作硬件. 应用软件要控制硬件,就要从用户态切换成内核态 2.多道技术:(多道指的 ...

  9. 784. Letter Case Permutation C++字母大小写全排列

    网址:https://leetcode.com/problems/letter-case-permutation/ basic backtracking class Solution { public ...

  10. Oracle 11.2.0.4.0 Dataguard部署和日常维护(6)-Dataguard Snapshot篇

    1. 检查当前主备库同步状态 on primary select ads.dest_id,max(sequence#) "Current Sequence", max(log_se ...