1. 程式人生 > >SpringMVC框架學習(1)——HelloWorld

SpringMVC框架學習(1)——HelloWorld

1.建立一個名為spring26-001-hello的Maven專案,結構如下
這裡寫圖片描述

2.引入所需jar包的依賴座標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.yc</groupId> <artifactId>spring26-001-hello</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>spring26-001-hello Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies
>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- springmvc的依賴座標 --> <dependency
>
<groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.2.RELEASE</version> <scope>runtime</scope> </dependency> </dependencies> <build> <finalName>spring26-001-hello</finalName> </build> </project>

3.建立SpringMVC的配置檔案spring-mvc.xml和web.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:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 掃描指定包中,要物件交給springmvc容器管理的類 -->
    <context:component-scan base-package="com.yc.springmvc"/>

    <!-- 配置springmvc檢視處理器的bean -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<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/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <!-- 請求方式轉換:把post請求處理轉換為put或delete請求方式 -->
  <filter>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- 配置請求由springmvc框架處理 -->
  <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
  </servlet>

  <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern><!-- 不攔截處理xxx.jsp -->
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

4.建立處理類HelloHandler.java,程式碼如下

package com.yc.springmvc.web.handler;

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

@Controller
@RequestMapping("/springmvc")  //請求處理的字首,可以做過濾處理 /user/*
public class HelloHandler {
    @RequestMapping("/hello")  //具體的請求資源處理
    public String hello(){
        System.out.println("請求HelloHandler.hello()處理進來了...");
        //return "redirect:success.jsp";
        return "forward:/success.jsp";
    }

    /**
     * RequestMapper的引數
     * params
     * headers
     * method
     * consumes
     * produces
     * 用來做限制請求條件,主要有"="和"!="
     * @return
     */
    @RequestMapping(value="/hello02",params={"name!=yc"})  //具體的請求資源處理
    public String hello02(){
        System.out.println("請求HelloHandler.hello02()處理進來了...");
        //return "redirect:success.jsp";
        return "forward:/success.jsp";
    }
    /**
     * method 可以用來做rest風格處理
     * get ==>取到資源
     * post ==>插入資源
     * put ==>更新資源
     * delete ==>刪除資源
     * @return
     */
    @RequestMapping(value="/hello03",method=RequestMethod.GET)  //具體的請求資源處理
    public String hello03(){
        System.out.println("請求HelloHandler.hello03()處理進來了...");
        //return "redirect:success.jsp";
        return "forward:/success.jsp";
    }

    @RequestMapping(value="/hello04",method=RequestMethod.POST)  //具體的請求資源處理
    public String hello04(){
        System.out.println("請求HelloHandler.hello04()處理進來了...");
        //return "redirect:success.jsp";
        return "forward:/success.jsp";
    }

    @RequestMapping(value="/hello05",method=RequestMethod.PUT)  //具體的請求資源處理
    public String hello05(){
        System.out.println("請求HelloHandler.hello05()處理進來了...");
        //return "redirect:success.jsp";
        return "forward:/success.jsp";
    }

    @RequestMapping(value="/hello06",method=RequestMethod.DELETE)  //具體的請求資源處理
    public String hello06(){
        System.out.println("請求HelloHandler.hello06()處理進來了...");
        //return "redirect:success.jsp";
        return "forward:/success.jsp";
    }
}

5.前臺介面如下

<html>
<body>
    <h2>Hello World!</h2>

    <form action="springmvc/hello04" method="post">
        <button>post請求</button>
    </form>

    <form action="springmvc/hello05" method="post">
        <input type="hidden" value="PUT" name="_method">
        <button>put請求</button>
    </form>

    <form action="springmvc/hello06" method="post">
        <input type="hidden" value="DELETE" name="_method">
        <button>delete請求</button>
    </form>
</body>
</html>

跳轉介面如下

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title</title>
</head>
        <h1>你好,我是springmvc框架,歡迎你使用我,我是不是很帥!!!</h1>
<body>
</body>
</html>

6.執行結果
這裡寫圖片描述
點選按鈕之前出現
這裡寫圖片描述