SpringMvc入門系列實例下載完美運行親測

近幾日在研究SpringMvc,特此寫一篇入門helloworld,希望能在大家前進的道路上幫助大家。結尾附贈源碼。

文檔參考:https://www.jb51.net/article/115553.htm

開發環境:    jdk:1.0.8、    tomcat 8.5、     Eclipse LUNA

STEP:

1.首先創建一個DynamicWebPro

2.導入SpringMvc需要的jar包到WebContent/WEB-INF/lib下面

3.配置web.xml配置文件
    (1).配置springDispatcherServlet
        <!-- 配置 Spring MVC DispatchcerServlet 前端控制器 -->
            <servlet>
                <servlet-name>springmvc</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                
                <!-- 
                    contextConfigLocation 是參數名稱,該參數的值包含 Spring MVC 的配置文件路徑 
                     如不使用此配置默認爲/WEB-INF/ 下的 servletname-servlet.xml文件
                -->
                <!-- 
                <init-param>
                    <param-name>contextConfigLocation</param-name>
                    <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
                </init-param>
                 -->
                <!-- 在 Web 應用啓動時立即加載 Servlet -->
                <load-on-startup>1</load-on-startup>
            </servlet>

   (2).將配置servlet-mapping,將所有的請求轉接到springDespatcherServlet
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <!-- 監聽當前域的所有請求 -->
            <url-pattern>/</url-pattern>
        </servlet-mapping>

4.配置springMVC的配置文件
    在/WEB-INF/ 創建 servletname-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: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.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    <context:annotation-config />
    <!-- 配置自動掃描的包,完成 Bean 的創建和自動依賴注入的功能 -->
    <context:component-scan
        base-package="com.test.springmvc.controller" />

    <!-- 配置視圖解析器 : 如何把 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> 
5.配置響應前臺請求的handler類文件
    類名之前使用註解  @Controller 將類修飾爲 handler 類
    在類的方法上使用  @RequestMapping 將方法修飾爲響應方法,並通過 value索引請求的後綴

注意類的包名要和上一步驟中配置自動掃描的包名一致。否則無法映射到正確的控制器

package com.test.springmvc.controller;

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

@Controller
public class tortoise {
    public static String SUCCESS = "success";

    @RequestMapping(value = "hello")
    public String hello() {
        System.out.println("hello");
        return SUCCESS;
    }
}

6.配置頁面  index.jsp置於WebContent下
<%@ 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>Insert title here</title>
</head>
<body>
    <a href="hello">hello</a>
</body>
</html>

    配置success.jsp置於置於WebContent\WEB-INF\view下

<%@ 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>Insert title here</title>
</head>
<body>
    <h4>SUCCESS PAGE</h4>
</body>
</html>

 

ok。至此點擊運行即可。

附上資源包地址,少量積分,謝謝支持。

https://download.csdn.net/download/u012842328/11049397

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章