Spring MVC項目搭建,入門教程:俗話說師傅領進門,修行在個人

Demo的結構:
在這裏插入圖片描述

  1. 準備jar包,上面項目結構的jar 包請自行準備;

  2. 創建resources根目錄,在裏面創建spring mvc 的配置文件:springmvc.xml
    根目錄創建方法:resources 目錄右鍵:
    在這裏插入圖片描述

  3. springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--1配置類掃描器-->
    <context:component-scan base-package="com.hao.springmvc"/>
    <!--2.配置註解驅動,可以使用springmvc的註解-->
    <mvc:annotation-driven/>
    <!--3.配置視圖解析器,解析路徑“/”不能丟-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
  1. 配置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>
        <!--配置讀取核心配置文件springmvc.xml-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!--代表當前servlet(DispatcherServlet)對象,隨tomcat啓動,創建servlet對象
        1代表優先級,必須式正整數,最小就是1-->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--配置servlet映射路徑<url-pattern>/</url-pattern>"/"代表攔截所有路徑-->
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
  1. index.jsp 首頁
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>首頁</title>
    <style>
      a{
        text-decoration: none;
      }
    </style>
  </head>
  <body>
    <a href="helloWord/hello?name=chen,pass=123">點擊進入SpringMVC入門案例</a>
  </body>
</html>

  1. controller:HelloController.java
package com.hao.springmvc.controller;

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

@Controller //該註解一般針對控制器層
@RequestMapping("helloWord")
public class HelloController {

    //此方法可以通過瀏覽器輸入地址直接訪問
    @RequestMapping(value = "/hello", method = RequestMethod.GET,params = {"name=chen,pass=123"})
    public String helloWord(){
        System.out.println("OK!歡迎來到SpringMVC");
        return "hello";//頁面跳轉
    }
}

  1. 跳轉後的頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>跳轉後的頁面</title>
</head>
<body>
    OK!歡迎來到SpringMVC頁面!!!!!!
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章