用Maven创建Web项目

选择webapp模板

创建成功后点Enable Auto-Import

idea给我们创建出来的结构是这样的,这还不标准,需要自己修改。

在main文件夹下创建java文件夹,这是放置源码的地方,标记为sources。创建resources文件夹且标记为resource。

初始结构如下。

配置Spring

pom.xml的配置

在与之间插入以下代码,添加依赖包。

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- Servlet Library -->
<!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency> <!-- Spring dependencies -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>

配置web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>HelloWorldSpring</display-name> <servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- Other XML Configuration -->
<!-- Load by Spring ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/root-context.xml</param-value>
</context-param> <!-- Spring ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>

Spring MVC的 DispatcherServlet 将根据默认原则读取XML配置文件: 在/WEB-INF文件夹下搜寻{servlet-name}-servlet.xml,在这里则是 spring-mvc-servlet.xml

接下来,标记指示哪些URL将由DispatcherServlet处理。 这里所有为/的HTTP请求都将由spring-mvc DispatcherServlet处理。

在Spring应用程序 ContextLoaderListener 将读取其他 XML 配置文件(如下的 abc.xml 和 root-context.xml 两个文件)。 可能不需要配置 ContextLoaderListener,如果你的应用程序并不需要读取其他XML配置文件。

<!-- web.xml -->
<!-- Spring ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- Load by Spring ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/root-context.xml,
/WEB-INF/abc.xml
</param-value>
</context-param>

下面添加了一个root-context.xml配置文件,仅仅是为了用于学习这功能。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Empty --> </beans>

配置spring-mvc-servlet.xml

<?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:p="http://www.springframework.org/schema/p"
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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <context:component-scan base-package="com.fang.controller"/> <context:annotation-config/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property> <property name="suffix">
<value>.jsp</value>
</property>
</bean> </beans>

[servlet-name]-servlet.xml文件将用于创建定义的bean,它会覆盖在全局范围中使用相同名称定义的任何bean的定义。

<context:component-scan …>标签将用于激活Spring MVC注释扫描功能,允许使用@Controller和@RequestMapping等注释。

InternalResourceViewResolver将定义用于解析视图名称的规则。根据上面定义的规则,hello的逻辑视图将委托给位于/WEB-INF/jsp/hello.jsp这个视图来实现。

创建控制器Controller

在java文件夹下创建包com.fang.controller,用于放置controller。

创建HelloWorldController类

package com.fang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class HelloWorldController {
@RequestMapping("/hello")
public String hello(Model model){
model.addAttribute("greeting","Hello Spring MVC");
return "helloworld";
}
}

value属性指示处理程序方法映射到的URL。

定义的服务方法可以返回一个String,它包含要用于渲染模型的视图的名称。此示例将“helloworld”返回为逻辑视图名称。

创建jsp视图

在WEB-INF文件夹下创建jsp文件夹,然后创建helloworld.jsp。

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring4 MVC -HelloWorld</title>
</head>
<body>
<h1>${greeting}</h1>
</body>
</html>

这里${greeting}是在Controller中设置的属性。可以在视图中显示多个属性。

部署tomcat server服务

点击工具栏的run->edit configurations

点击+号,选择tomcat-local

部署tomcat,选择war结尾的那个

完成后左下角会出现这个

点击绿色小箭头就运行了

运行结果

DispatcherServlet拦截解析请求,发现是/hello,转到HelloWorldController下的hello方法中->helloworld 将请求转发到/WEB-INF/jsp/helloworld.jsp界面

项目最终结构图

应用程序的流程

现在来看看程序的运行流程吧!这很重要!

IDEA+Maven+Spring MVC HelloWorld示例的更多相关文章

  1. Spring MVC 项目示例

    Spring MVC是Spring Framework的一部分,是基于Java实现MVC的轻量级Web框架.Spring的web框架围绕DispatcherServlet设计, 作用是将请求分发到不同 ...

  2. IntelliJ IDEA上创建maven Spring MVC项目

    IntelliJ IDEA上创建Maven Spring MVC项目 各软件版本 利用maven骨架建立一个webapp 建立相应的目录 配置Maven和SpringMVC 配置Maven的pom.x ...

  3. maven Spring MVC项目

    IntelliJ IDEA上创建maven Spring MVC项目 各软件版本 利用maven骨架建立一个webapp 建立相应的目录 配置Maven和SpringMVC 配置Maven的pom.x ...

  4. Maven+Spring MVC Spring Mybatis配置

    环境: Eclipse Neon JDK1.8.0 Tomcat8.0 先决条件: Eclipse先用maven向导创建web工程.参见本站之前随笔. 本机安装完成mysql5:新建用户xuxy03设 ...

  5. 1.【转】spring MVC入门示例(hello world demo)

    1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于 ...

  6. Spring MVC 入门示例讲解

    在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...

  7. Intellij IDEA采用Maven+Spring MVC+Hibernate的架构搭建一个java web项目

    原文:Java web 项目搭建 Java web 项目搭建 简介 在上一节java web环境搭建中,我们配置了开发java web项目最基本的环境,现在我们将采用Spring MVC+Spring ...

  8. Spring MVC 入门示例讲解 - howtodoinjava

    在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...

  9. JAVA入门教程 - idea 新建maven spring MVC项目

    用的是Idea2017版本.其他大同小异 1.新建项目 2.勾选Create from archetype 选中maven-archetype-webapp 3.输入项目名字. 4.下一步 5.点Fi ...

随机推荐

  1. jmeter接口测试实战-创建用户

    jmeter接口测试实战-创建用户 相信大多数看到标题的同学都会有疑问, 创建用户不是很简单吗, 调用一下创建用户接口, 传入指定入参, 用户即可创建成功, 今天我们的实战来讲讲创建场景.通过接口创建 ...

  2. java编译过程中出现了Exception in thread “main" java.lang.UnsupportedClassVersionError

    原因:这个问题确实是由较高版本的JDK编译的java class文件试图在较低版本的JVM上运行产生的错误. 以下是报错截图: 1.解决措施就是保证jvm(java命令)和jdk(javac命令)版本 ...

  3. Django Rest Framework(一)

    •基于Django 先创建一个django项目,在项目中创建一些表,用来测试rest framework的各种组件 class UserInfo(models.Model): "" ...

  4. List、Map、Set之间的联系与区别:

    一.数组和集合的区别: 1.数组的大小是固定的,并且同一个数组只能是相同的数据类型 2.集合的大小是不固定的,在不知道会有多少数据的情况下可使用集合. 二.集合的三种类型:list(列表).set(集 ...

  5. C++编程音视频库ffmpeg的pts时间换算方法

    ffmpeg中的pts,dts,duration时间记录都是基于timebase换算,我们主要分析下pts的时间怎么换算,其它的是一样的换算.ffmpeg的时间换算对许多新接触同学算是一个大坑,很多刚 ...

  6. SpringBoot自动装配源码解析

    序:众所周知spring-boot入门容易精通难,说到底spring-boot是对spring已有的各种技术的整合封装,因为封装了所以使用简单,也因为封装了所以越来越多的"拿来主义" ...

  7. 第一章 Python基本语法元素分析(二)

    1.3   实例1:温度转换 根据华氏和摄氏温度定义,利用转换公式如下: C=(F-32)/1.8 F=C*1.8+32 代码如下: 运行结果: 1.4   Python程序语法元素分析 注释:不被程 ...

  8. UNION的使用方法 (表与表直接数据和在一起的示例)

    SELECT o.CATEGORY CATEGORY,o.KEY_WORK KEY_WORK FROM BO_EU_KEY_WORK wo RIGHT OUTER JOIN BO_EU_WORK_ON ...

  9. 洛谷P1197 [JSOI2008]星球大战

    题目 由于题目不要求强制在线,所以可以离线. 而离线的话就会带来许多便利,所以我们可以先处理出全部打击后的图,通过并查集来判断是否连通. 然后再从后往前枚举,得出答案 #include <bit ...

  10. 百度编辑器前后端二开图片上传Js Thinkphp tp5 ueditor

    百度编辑器图片上传Jsueditor.all.min.js 下载链接 链接:https://pan.baidu.com/s/1VNgw9ELgRRHKeCQheFkQTw 提取码:fnfi 使用方法: ...