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.</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】springmvc4 + servlet3 零配置全注解入门项目helloword
自从servlet3.0支持注解,使得javaweb项目使用全注解成为可能. 注解化将成为javaweb的发展方向.包括spring去年推出的spring-boot也是全部使用注解. 代码:https ...
- spring4.2完整web项目(使用html视图解析器)
完整配置springmvc4,最终视图选择的是html,非静态文件. 最近自己配置spring的时候,遇到很多问题,由于开发环境和版本的变化导致网友们给出的建议很多还是不能用的,可能还会有很多人会遇到 ...
- 初步配置基于Struts的maven的Web项目demo
1.创建mavenweb项目 eclipse | Idea 中创建maven项目,具体步骤PASS. 2.导入Struts2核心架包 Idea中在maven项目上,选择ADD FRAMEWORK ec ...
- MyEclipse web项目导入Eclipse,详细说明
最近导入一个MyEclipse的项目,具体是:spring4.1的web项目,同时遇到了一些问题,总结一下. 1.进入项目目录,找到.project文件,打开.增加一个<buildCommand ...
- 基于全注解的SpringMVC+Spring4.2+hibernate4.3框架搭建
概述 从0到1教你搭建spring+springMVC+hibernate整合框架,基于注解. 本教程框架为基于全注解的SpringMVC+Spring4.2+hibernate4.3,开发工具为my ...
- 一款基于SSM框架技术的全栈Java web项目(已部署可直接体验)
概述 此项目基于SSM框架技术的Java Web项目,是全栈项目,涉及前端.后端.插件.上线部署等各个板块,项目所有的代码都是自己编码所得,每一步.部分都有清晰的注释,完全不用担心代码混乱,可以轻松. ...
- Java全栈程序员之07:IDEA中使用MAVEN构架生产级的Web项目
在上一篇我们介绍了如何在IDEA中使用MAVEN,以及如何创建依赖等.那么在这一篇中,我们就试图搭建一个生产级的解决方案,大家可以使用这个解决方案作为骨架代码来搭建自己的开发环境. 在这里,我们要完成 ...
- 【持久化框架】SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载
上篇博文我们介绍了mybatis的基本概念与原理,这篇博文我们通过Spring与Mybatis集成,开发一个简单用户增删改查的Web项目. 基本准备工作 1.安装JDK1.6以上版本,安装与配置 2. ...
- 基于已构建S2SH项目配置全注解方式简化配置文件
如果还不熟悉s2sh项目搭建的朋友可以先阅读 eclipse环境下基于tomcat-7.0.82构建struts2项目 eclipse环境下基于已构建struts2项目整合spring+hiberna ...
随机推荐
- 20165327 2017-2018-2 《Java程序设计》第7周学习总结
20165327 2017-2018-2 <Java程序设计>第7周学习总结 教材内容总结 第十一章 (一)MySQL数据库服务器 下载安装MySQL服务器 启动MySQL数据库服务器 在 ...
- linux动态库
1.编写动态库函数接口 gcc -fPIC -shared -o libfunction.so funtion.c 2.写头文件 3.测试 gcc test.c -L. -lfuntion -o ru ...
- ModelViewSet 视图集 实现接口
一.创建项目 1.创建 项目 : django-admin startprojet drf 2. 创建 两个app ------ app1 ,book python manage.py start ...
- DFS CCPC2017 南宁I题
The designers have come up with a new simple game called “Rake It In”. Two players, Alice and Bob, i ...
- 使用org.apache.poi导出Excel表格
public HSSFWorkbook MakeExcel(List<TransactionLogVO> logList) { // SimpleDateFormat sdf = new ...
- Leetcode 1029. 可被 5 整除的二进制前缀
1029. 可被 5 整除的二进制前缀 显示英文描述 我的提交返回竞赛 用户通过次数467 用户尝试次数662 通过次数477 提交次数1964 题目难度Easy 给定由若干 0 和 1 组成的 ...
- 【LeetCode】跳跃游戏
给定一组非负整数,初始时处于数组的第一位下标 0 的位置,数组的每个元素代表那个位置可以跳跃的最大长度.判断你是否能够到达数组的最后一位下标. e.g. A = [2, 3, 1, 1, 4],返回 ...
- windows工具打开命令
程序 命令 位置 记事本 notepad C:\Windows\system32 ping ping C:\Windows\System32 服务管理器 services.msc C:\Windows ...
- python 自然语言处理(四)____词典资源
词典或者词典资源是一个词和/或短语及其相关信息的集合,例如:词性和词意定义等相关信息.词典资源附属于文本,而且通常在文本的基础上创建和丰富.下面列举几种nltk中的词典资源. 1. 词汇列表语料库 n ...
- python 类方法中参数使用默认值的方法
class A(): __init__(self, **arg): self.__dict__.update(arg) def M(self, config=None, section= ...