2.Spring MVC Hello World

2017-05-11  本文已影响16人  765197250c12

项目

项目结构.png

步骤

在mavan中添加相关jar包

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>

在web.xml中配置DispatchServlet

<!DOCTYPE web-app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>dispatchServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--配置DispatchServlet的一个初始化参数:配置SpringMVC文件的位置和名称-->
    <!--实际上也可以不通过cointextConfigLocation来配置SpringMVC的配置文件和名称使用默认的
    默认的文件位置为:/WEB-INF/<servlet-name>-servlet.xml-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatchServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

DispatchServlet默认加载WEB-INF/<servletName-servlet>.xml的Spring配置文件,启动WEB层的Spring容器。可以通过contextConfigLoaction初始化参数自动以配置文件的位置和名称

加入Spring MVC 的配置文件

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

 <!--配置扫描的包-->
 <context:component-scan base-package="com.gu.controller">
 </context:component-scan>

 <!--配置视图解析器-->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/WEB-INF/Views/"></property>
   <property name="suffix" value=".jsp"></property>
 </bean>

</beans>

配置自动扫描的包

 <context:component-scan base-package="com.gu.controller">
  </context:component-scan>

配置视图解析器:视图名称解析器将视图逻辑名解析为/WEB-INF/Views/<ViewName>.jsp

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/Views/"></property>
    <property name="suffix" value=".jsp"></property>
  </bean>

编写处理请求的Controller,并标示为处理器

package com.gu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by gurongkang on 17/5/8.
 */
@Controller
public class HelloController {

  /**
   * 1.使用RequesMapping注解来映射请求URL
   * 2.返回值通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver
   * 视图解析器会做如下解析
   * 通过prefix + returnVal + suffix 得到物理视图
   */
  @RequestMapping("/hello")
  public String sayHello() {
    return "Hello";
  }

}

编写视图

在WEB-INF/Views/下加入Hello.jsp


源码:https://github.com/gurongkang/TestSpringMVC.git tag 1

上一篇下一篇

猜你喜欢

热点阅读