用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. IDEA XML注释与取消注释快捷键

    IntelliJ IDEA和eclipse中编辑Java文件时,注释和取消注释的快捷键都是: "CTRL + / " 编辑xml文件时, 注释:CTRL + SHIFT + / 取 ...

  2. ASP.NET Zero--基于令牌的认证&SWAGGER UI

    基于令牌的认证 任何应用程序都可以将应用程序中的任何功能认证和使用为API.例如,您可以创建一个移动应用程序消耗相同的API.在本节中,我们将演示来自Postman的API (Google Chrom ...

  3. Java 8 Stream介绍及使用1

    (原) stream的内容比较多,先简单看一下它的说明: A sequence of elements supporting sequential and parallel aggregate * o ...

  4. Oracle硬解析,软解析,软软解析介绍

    Oracle数据库中的CURSOR分为两种类型:Shared Cursor 和 Session Cursor 1,Shared Cursor Oracle里的第一种类型的Cursor就是Shared ...

  5. ztree 为节点添加点击触发事件

    <SCRIPT type="text/javascript"> var setting = { data : { key : { title : "code& ...

  6. CodeSmith 二、多模板按目录树批量自动生成代码

    通过调用指定目录下的所有模板,逐一按照数据表生成独立的代码文件.支持多模板调用.支持所有数据表生成或批量指定多个生成.支持自动的文件目录结构.支持代码文件格式化命名等. 背景:最近一个新项目一高兴选了 ...

  7. zcu102 hdmi example(二)

    1.概述 上篇说到,调用跑HDMI IP核自带的design example,跑出来的结果是显示屏显示彩条,并伴有嘀,嘀,嘀...的声音.因为在实际项目中,我们只需要图像,不需要声音的,所以我要把声音 ...

  8. 使用python抓取数据之菜鸟爬虫1

    ''' Created on 2018-5-27 @author: yaoshuangqi ''' #本代码获取百度乐彩网站上的信息,只获取最近100期的双色球 import urllib.reque ...

  9. lazyMan

    class Lazyman { constructor() { this.tasks = []; this.init(); } init() { const task = () => { con ...

  10. 微信支付之02------整个微信支付功能----------Java实现

    先来看下微信支付官方文档: 1.在官方文档上有很多种支付方式,由于目前我只做过JSAPI和微信扫码支付二种,其他的就不说了. >>>>>第一种微信扫码支付>> ...