Intellij IDEA创建基于Gradle的SpringMVC工程
来源:互联网 发布:mac没声音 编辑:程序博客网 时间:2024/06/06 23:50
在创建工程时选择基于Gradle的工程,勾选Web
如果选择使用gradle wrapper导致下载很慢,可以选择本地安装的gradle
添加tomcat(Run->Edit Configuration),最后点击绿三角运行工程
在build.gradle
中添加Spring MVC依赖,并同步工程
group 'cn.iotguard'version '1.0-SNAPSHOT'apply plugin: 'java'apply plugin: 'war'repositories { mavenCentral()}dependencies { compile 'org.springframework:spring-webmvc:4.3.6.RELEASE' testCompile group: 'junit', name: 'junit', version: '4.11'}
接下来要开始编写Java代码了,在main下创建java文件夹,并在java文件夹下创建一个package,如下
在package下创建一个Java类文件,内容如下:
package cn.iotguard.demo.controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * Created by caowentao on 2017/2/28. */@RestControllerpublic class DemoController { @RequestMapping("/greeting/{name}") public String greeting(@PathVariable("name") String name) { return "hello, " + name; }}
此时如果访问http://localhost:8080/greeting/caowentao
,浏览器返回404错误。打开Project Structure
,找到Web Gradle
模块,并点击Deployment Descriptors
栏右侧的加号添加web.xml
文件。注意web.xml
文件应该放到webapp
目录下的WEB-INF
目录下。
在web.xml文件中添加如下映射信息
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet><servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern></servlet-mapping>
此时访问http://localhost:8080/greeting/caowentao
,tomcat返回异常信息,nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
。因此还需要在WEB-INF目录中创建一个dispatcher-servlet.xml
文件(一个Spring Config文件)。
内容如下:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="cn.iotguard.demo.controller"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven></beans>
最后运行,访问http://localhost:8080/greeting/caowentao
,成功返回hello, caowentao
- Intellij IDEA创建基于Gradle的SpringMVC工程
- Intellij Idea创建基于Maven的SpringMVC+MyBatis项目
- IntelliJ IDEA 创建并部署单纯的web工程(不用Maven和Gradle管理)
- 使用Intellij Idea创建第一个SpringMVC工程
- intellij IDEA 搭建基于 Gradle 的 Spring 项目
- Intellij Idea 创建 Maven 工程
- IntelliJ IDEA 创建Maven 工程
- Intellij Idea基于现有的Eclipse工程创建 多模块工程--spring spring mvc hibernate - SSH- resin
- 使用2016版intellij idea创建SpringMVC 的helloWorld
- Intellij IDEA 创建 Maven SpringMVC 项目的方法
- intelliJ IDEA 创建maven android工程的方法
- IntelliJ IDEA快速创建Maven工程的WEB项目
- 使用IntelliJ idea创建的java工程无法编辑
- IntelliJ IDEA如何创建一个简单的java工程
- 使用IntelliJ IDEA创建SpringMVC项目
- IntelliJ IDEA创建SpringMVC+Maven项目
- IntelliJ IDEA 创建maven springmvc 简单项目
- Intellij IDEA 创建SpringMVC报404
- centos安装nginx,配置负载均衡
- 整合SSH框架的思路及方法
- 最小费用最大流背诵用模板
- Leetcode Tree Problem 树问题合集
- 用FlexboxLayout构建灵活的布局
- Intellij IDEA创建基于Gradle的SpringMVC工程
- 分布式TensorFlow集群使用入门
- 数据传输加密——非对称加密算法RSA+对称算法AES(适用于java,android和Web)
- 使用jQuery validate插件验证表单
- 超精简kernel代码简析(可能存在理解上的问题,还请多多指正)
- java Servlet(五) 异常处理(配置异常处理页)
- AndroidStudio debug调试、单元测试
- C# Func<T>、Action<T> 的区别于说明
- 使用office的word进行doc(x)->pdf文件的转换