IDEA创建一个Spring MVC 框架Java Web项目,Gradle构建
注:此篇有些细节没写出,此文主要写重要的环节和需要注意的地方,轻喷
新建项目
选择Gradle , 勾选java 和 web。之后就是设定项目路径和名称,这里就不啰嗦了。

build.gradle文件下导入所需要的包,刷新一下。
1 group 'com.benjious.createTest'
2 version '1.0-SNAPSHOT'
3
4 apply plugin: 'java'
5 apply plugin: 'war'
6
7 sourceCompatibility = 1.8
8
9 repositories {
10 mavenCentral()
11 }
12
13 dependencies {
14 testCompile group: 'junit', name: 'junit', version: '4.11'
15 testCompile group: 'junit', name: 'junit', version: '4.12'
16
17
18 compile'org.springframework:spring-context:4.3.6.RELEASE'
19 compile'org.springframework:spring-webmvc:4.3.6.RELEASE'
20 compile'org.springframework:spring-test:4.3.6.RELEASE'
21 }
22
目录结构
设置项目结构

贴出各个类的代码
1 public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
2
3 @Override
4 protected Class<?>[] getRootConfigClasses() {
5 return new Class[]{RootConfig.class};
6 }
7
8 @Override
9 protected Class<?>[] getServletConfigClasses() {
10 return new Class[]{WebConfig.class};
11 }
12
13 @Override
14 protected String[] getServletMappings() {
15 return new String[] { "/" };
16 }
17 }
18
1 //exclude : 过滤掉
2 @Configuration
3 @ComponentScan(basePackages = {"createtest"},excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)})
4 public class RootConfig {
5
6 }
7
1 @Configuration
2 @EnableWebMvc
3 @ComponentScan("createtest.web")
4 public class WebConfig extends WebMvcConfigurerAdapter {
5
6
7 @Bean
8 public ViewResolver viewResolver() {
9 InternalResourceViewResolver resolver = new InternalResourceViewResolver();
10 resolver.setPrefix("/WEB-INF/view/");
11 resolver.setSuffix(".jsp");
12 return resolver;
13 }
14
15
16 @Override
17 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
18 configurer.enable();
19 }
20
21 }
1 @Controller
2 public class HomeControl {
3
4 @RequestMapping("/home")
5 public String home() {
6 return "home";
7 }
8
9 }
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
2 <html>
3 <head>
4 <title>Home</title>
5 </head>
6 <body>
7 <p>home界面</p>
8 </body>
9 </html>
配置Tomcat
首先配置好Tomcat ,下面是配置好的界面


运行项目, 访问 http://localhost:8080/home/

这里需要的是在配置 Tomcat 中,设置 Application Context ,例如 Application Context 为 “/home ” ,那么 该项目的服务器的根地址为 :
http://localhost:8080/home/ , 那么要显示 home.jsp 界面应该为: http://localhost:8080/home/home .
参考:
- 《Sring in Action 》Spitrr 项目
- Spring Example
IDEA创建一个Spring MVC 框架Java Web项目,Gradle构建的更多相关文章
- 规范化创建一个vs2017 Mvc框架项目
vs2107 + dapper + MiniUi 标准化分层封装使 3.1 规范化创建一个vs2017 Mvc框架项目 此时创建的项目勾选 添加单元测试. 添加一个类库,主要用于实体类操作,类库名称 ...
- 使用Maven创建一个Spring MVC Web 项目
使用Maven创建java web 项目(Spring MVC)用到如下工具: 1.Maven 3.2 2.IntelliJ IDEA 13 3.JDK 1.7 4.Spring 4.1.1 rele ...
- IntelliJ IDEA 14.x 创建工作空间与多个Java Web项目
以往的Eclipse.NetBeans等开发工具不同,IDEA的Project相当与Eclipse的Workspace,而Module相当于Project. 下边就给出Eclipse与IDEA的概念的 ...
- 快速创建一个 spring mvc 示例
1. 创建一个 servlet 项目 参考 http://www.cnblogs.com/zno2/p/5908589.html 2. 引入 spring mvc 依赖 <dependency& ...
- 使用IDEA搭建一个Spring + AOP (权限管理 ) + Spring MVC + Mybatis的Web项目 (零配置文件)
前言: 除了mybatis 不是零配置,有些还是有xml的配置文件在里面的. 注解是Spring的一个构建的一个重要手段,减少写配置文件,下面解释一下一些要用到的注解: @Configuration ...
- 使用IDEA搭建一个 Spring + Spring MVC + Mybatis 的Web项目 ( 零配置文件 )
前言: 除了mybatis 不是零配置,有些还是有xml的配置文件在里面的. 注解是Spring的一个构建的一个重要手段,减少写配置文件,下面解释一下一些要用到的注解: @Configuration ...
- 使用go语言开发一个后端gin框架的web项目
用liteide来开发go的后端项目,需要注意的是环境变量要配置正确了 主要是GOROOT, GOPATH, GOBIN, PATH这几个, GOPATH主要用来存放要安的包,主要使用go get 来 ...
- Spring MVC框架处理Web请求的基本流程
- 【Spring】创建一个Spring的入门程序
3.创建一个Spring的入门程序 简单记录 - Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)- Spring的基本应用 Spring与Spring MVC的 ...
随机推荐
- 搜索下拉 select美化
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- linux kvm虚拟机安装
1.上传ISO文件,这里采用OEL5.8x64iso 2.开始安装OEL5.8 (1)raw格式磁盘 virt- --vcpus= --disk path=/data/test02.img,size= ...
- 【sonar-block】Use try-with-resources or close this "BufferedInputStream" in a "finally" clause.
自己的理解: try-with-resources是JDK7的新语法结构,主要功能是自动关闭资源而不需要在finally里面手动close()关闭, 而且最重要的是,try块中的异常不会被close( ...
- 报错The sandbox is not in sync with the Podfile.lock
clone下来的项目,运行的时候报错 diff: /../Podfile.lock: No such file or directory diff: Manifest.lock: No such fi ...
- Shell基本知识
Shell是什么 Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以使用Shell来启动.挂起.停止甚至编写一些程序. Shell还是一个功 ...
- python 全栈开发:python基础
python具有优美.清晰.简单,是一个优秀并广泛使用的语言.诞生于1991年2.python历史 1989年,为了打发圣诞节假期,Guido开始写Python语言的编译器.Python这个名字,来自 ...
- POJ 2253-Frogger(最小生成树的最大权)
原题链接:点击此处 题意: 一只叫Freddy的青蛙蹲坐在湖中的一块石头上.突然他发现一只叫Fiona的青蛙在湖中的另一块石头上.Freddy想要跟Fiona约会,但由于湖水太脏,他不想游泳过去而是跳 ...
- ORACLE INSERT ALL 用法
1INSERT ALL 1.1句法 multi_table_insert :: = conditional_insert_clause :: = 1.2multi_table_insert 在多表插入 ...
- Webpack的详细配置,[Webpack中各种loader的安装配置]
在使用webpack的时候,你是不是被以下这种报错所困扰: 注意看 黄色框中标注的 You may need an appropriate loader to handle this file typ ...
- docker编排工具,docker-compose下载与安装
安装很简单,但是难免会遇到问题:1.安装curl -L https://github.com/docker/compose/releases/download/1.21.0/docker-compos ...