天天看點

SpringMVC學習筆記---SpringMVC簡介

SpringMVC學習筆記---SpringMVC簡介

SpringMVC版HelloWorld

  1. 加入相應的jar包
  2. 在web.xml檔案中配置DispatcherServlet
  3. 加入SpringMvc的配置檔案
  4. 編寫請求處理器,并辨別為處理器
  5. 編寫視圖:使用jsp檔案作為視圖
  • 所需的基礎jar包
SpringMVC學習筆記---SpringMVC簡介
  • 配置web.xml檔案
<?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_4_0.xsd"
         version="4.0">

    <!--配置轉發器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        
        <!--配置Spring配置檔案的路徑和名稱-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-cfg.xml</param-value>
        </init-param>
    </servlet>

    <!--定義servlet映射的路徑-->
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
           
  • 配置springmvc-cfg.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="mao.shu.springmvc"/>

    <!--配置視圖解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--
            prefix 定義路徑字首
            suffix 定義路徑字尾
            
            則請求的路徑将會被拼湊為/WEB-INF/pages/"路徑名稱".jsp
        -->
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
           
  • 建立請求處理類,使用"@RequestMapping" 注解設定請求路徑,
    • HelloWorld.java
package mao.shu.springmvc.action;

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

@Controller
public class HelloWorld {
    @RequestMapping("/HelloWorld")
    public String execute(){
        System.out.println("Hello World");
        return "success";
    }
}

           
  • 定義success.jsp頁面
<%--
  Created by IntelliJ IDEA.
  User: Xiemaoshu
  Date: 2019/3/6
  Time: 23:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>HelloWorld</title>
</head>
<body>
<h1>HelloWorld</h1>
</body>
</html>

           
  • 測試請求路徑:http://localhost:8080/SpringMVCModule1/HelloWorld
  • 效果
SpringMVC學習筆記---SpringMVC簡介
  • 背景輸出
SpringMVC學習筆記---SpringMVC簡介

繼續閱讀