IDEA+Maven+Spring MVC HelloWorld示例
用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示例的更多相关文章
- Spring MVC 项目示例
Spring MVC是Spring Framework的一部分,是基于Java实现MVC的轻量级Web框架.Spring的web框架围绕DispatcherServlet设计, 作用是将请求分发到不同 ...
- IntelliJ IDEA上创建maven Spring MVC项目
IntelliJ IDEA上创建Maven Spring MVC项目 各软件版本 利用maven骨架建立一个webapp 建立相应的目录 配置Maven和SpringMVC 配置Maven的pom.x ...
- maven Spring MVC项目
IntelliJ IDEA上创建maven Spring MVC项目 各软件版本 利用maven骨架建立一个webapp 建立相应的目录 配置Maven和SpringMVC 配置Maven的pom.x ...
- Maven+Spring MVC Spring Mybatis配置
环境: Eclipse Neon JDK1.8.0 Tomcat8.0 先决条件: Eclipse先用maven向导创建web工程.参见本站之前随笔. 本机安装完成mysql5:新建用户xuxy03设 ...
- 1.【转】spring MVC入门示例(hello world demo)
1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于 ...
- Spring MVC 入门示例讲解
在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...
- Intellij IDEA采用Maven+Spring MVC+Hibernate的架构搭建一个java web项目
原文:Java web 项目搭建 Java web 项目搭建 简介 在上一节java web环境搭建中,我们配置了开发java web项目最基本的环境,现在我们将采用Spring MVC+Spring ...
- Spring MVC 入门示例讲解 - howtodoinjava
在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...
- JAVA入门教程 - idea 新建maven spring MVC项目
用的是Idea2017版本.其他大同小异 1.新建项目 2.勾选Create from archetype 选中maven-archetype-webapp 3.输入项目名字. 4.下一步 5.点Fi ...
随机推荐
- 2019-1-17 前言 C#高级编程(第11版)
C#已更新为更快的速度.主要版本7.0是2017年3月发布,次要版本7.1和7.2很快发布在2017年8月和2017年12月.通过项目设置,您可以与每个应用程序一起分发,是开源的,不可用仅适用于Win ...
- 用 Python 描述 Cookie 和 Session
这篇文章我们来聊聊Cookie和Session,网上有很多关于这两个知识点的描述,可惜的是大部分都没有示例代码,因此本文的重点在于示例代码. 环境 Python3.6.0 Bottle0.12.15 ...
- vue 导出xlsx表功能
详细步骤: 1.需要安装三个依赖: npm install -S file-saver xlsx npm install -D script-loader 两个命令行包含三个依赖. 2.项目中src下 ...
- 记一次 c 语言 的 多线程查找 简单实现
//仅供参考学习 1 #define _CRT_SECURE_NO_WARNINGS //屏蔽 vs 的a #include <stdio.h> #include <stdlib.h ...
- servlet之转发与重定向的区别
转发(服务器端跳转): 一次请求 <jsp:forward> request.getRequestDispatcher("new.jsp").forward(requ ...
- robotFramework第二篇之关键字的定义和使用
lesson.robot *** Keywords *** 打开谷歌浏览器并访问百度首页 Log 打开浏览器,输入http://www.baidu.com,进入百度首页 输入用户名 [Argument ...
- Python总结(二)
学习一门语言,首先就要学习它的数据类型和语法.这里与JS进行对比学习. 1.数据类型 python的数据类型有:数字(int).浮点(float).字符串(str),列表(list).元组(tuple ...
- Spring第一天——入门与IOC
大致内容 spring基本概念 IOC入门 [17.6.9更新],如何学习spring? 掌握用法 深入理解 不断实践 反复总结 再次深入理解与实践 一.Spring相关概念 1.概述: Sprin ...
- javaweb项目环境搭建,jdk,tomcat,myeclipse,sqlserver安装 配置
myeclipse是一个java的IDE,myeclipse中虽然内置了JDK和Tomacat服务器,但可以不使用,通过进行相应的配置,使用自行安装的JDK和Tomcat.安装Tomcat之前,一定要 ...
- 浅谈background-size的几个属性值
前言: css应用中,我们常会碰到background-size的用法,不妨下面总结一下,以便后续查看. 一.先弄个简单的box框. <!DOCTYPE html> <html la ...