SpringMVC 2.HelloWorld

 面向对象面向君,不负代码不负卿。 *^o^*

SpringMVC HelloWorld


1.建立SpringMVC的Maven工程

1.1 使用iIntelliJ IDEA 创建Maven工程





1.2 配置pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ifox.hgx</groupId>
  <artifactId>springMVC_1</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>springMVC_1 Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <dependencies>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.3.RELEASE</version>
    </dependency>

    <!-- https://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>

  </dependencies>
  <build>
    <finalName>springMVC_1</finalName>
  </build>
</project>

2 HelloWorld实现

2.1 目录结构预览

2.2 配置web.xml

  • 配置 DispatcherServletDispatcherServlet 默认加载 /WEBINF/.xml的 Spring 配置文件, 启动 WEB 层的 Spring 容器。可以通过 contextConfigLocation 初始化参数自定义配置文件的位置和名称
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置DispatcherServlet初始化参数:springMVC配置文件的位置和参数-->
        <!--也可以不通过contextConfigLocation来配置springMVC的配置文件, 而使用默认的配置文件: /WEB-INF/views/<servlet-name>-servlet.xml 即:springDispatcherServlet-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>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

2.3 创建 SpringMVC 配置文件: springmvc.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: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.ifox"></context:component-scan> <!--配置视图解析器:如何把handler 方法返回值解析为实际的物理视图--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans> 

2.4 创建请求处理器类:HelloWorld .java

package com.ifox.handlers;


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

@Controller
public class HelloWorld {
    /* 1.使用@RequestMapping 注解来映射请求的URL 2.返回值会通过视图解析器解析未实际的物理视图,对于InternalResourceViewResolver 视图解析器,会做如下的解析:通过prefix + returnVal + 后缀 得到实际的物理视图,然后做转发操作 如: /WEB-INF/views/success.jsp */

    @RequestMapping("/helloworld")
    public String hello(){
        System.out.println("hello world");
        return "success" ;
    }
}

2.5 视图实现

2.5.1 index.jsp

<html>
<body>
    <h2>Hello World!</h2>
    <a href="helloworld">hello world</a>
</body>
</html>

2.5.2 success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h4>success page</h4>
</body>
</html>

3 流程分析

上一篇:SpringMVC 1.概述

下一篇:SpringMVC 3.RequestMapping,@PathVariable注解,REST

   大牛,别默默看了。快登陆帮我评论吧! *^o^*
全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务