IDEA+Maven+Spring MVC HelloWorld示例

用Maven建立Web項目

選擇webapp模板html

image

image

建立成功後點Enable Auto-Importjava

image

idea給咱們建立出來的結構是這樣的,這還不標準,須要本身修改。web

image

在main文件夾下建立java文件夾,這是放置源碼的地方,標記爲sources。建立resources文件夾且標記爲resource。spring

image

初始結構以下。api

image

配置Spring

pom.xml的配置

之間插入如下代碼,添加依賴包。spring-mvc

<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.xmltomcat

接下來,標記指示哪些URL將由DispatcherServlet處理。 這裏全部爲/的HTTP請求都將由spring-mvc DispatcherServlet處理。mvc

在Spring應用程序 ContextLoaderListener 將讀取其餘 XML 配置文件(以下的 abc.xml 和 root-context.xml 兩個文件)。 可能不須要配置 ContextLoaderListener,若是你的應用程序並不須要讀取其餘XML配置文件。app

<!-- 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配置文件,僅僅是爲了用於學習這功能。webapp

<?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

image

點擊+號,選擇tomcat-local

image

部署tomcat,選擇war結尾的那個

image

image

image

完成後左下角會出現這個

image

點擊綠色小箭頭就運行了

image

運行結果

image

DispatcherServlet攔截解析請求,發現是/hello,轉到HelloWorldController下的hello方法中->helloworld 將請求轉發到/WEB-INF/jsp/helloworld.jsp界面

項目最終結構圖

image

應用程序的流程

如今來看看程序的運行流程吧!這很重要!

i

相關文章
相關標籤/搜索