SpringMVC的基本使用步驟

SpringMVC的基本使用步驟

1.創建web工程

2.在src下創建lib文件夾,導入SpringMVC相關jar包(在Spring的基礎之上再添加一個mvc包)

 3.在工程文件夾下創建resource文件夾,並添加配置文件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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/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/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!--開啓註解掃描-->
    <context:component-scan base-package="com.helong"/>

    <!--視圖解析器前/後綴設置-->
    <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        &lt;!&ndash;設置前綴&ndash;&gt;
        <property name="prefix" value="/WEB-INF/helong/"/>
        &lt;!&ndash;設置後綴&ndash;&gt;
        <property name="suffix" value=".jsp"/>
    </bean>-->
    
</beans>

4.配置前端控制器(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">

    <!-- 配置SpringMVC前端控制器 -->
    <servlet>
        <servlet-name>mySpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定SpringMVC配置文件 -->
        <!-- SpringMVC的配置文件的默認路徑是/WEB-INF/${servlet-name}-servlet.xml -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!--在下面的配置中寫上大於0的數字後,當服務器啓動的時候就會加載上面的初始化類,就不會讓用戶在點擊的時候再做初始化
        從而減少用戶的等待時間-->
        <load-on-startup>1</load-on-startup>


    </servlet>
    <servlet-mapping>
        <servlet-name>mySpringMVC</servlet-name>
        <!-- 設置所有以action結尾的請求進入SpringMVC -->
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>


</web-app>

5.創建控制器

package com.helong.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/*使用註解時,要在springmvc.xml中開啓註解掃描 並在設置中設置開啓註解*/
@Controller
public class MyController {

    //匹配請求可以省略action
    /*如果有數據要發送到其他界面就可以返回ModeAndView來傳遞數據*/
    /*@RequestMapping("/first.action") 這個位置的.action可以省略*/
    @RequestMapping("/first")
    public ModelAndView show1(){

        ModelAndView modelAndView = new ModelAndView();

        //請求過來後跳轉到某一個界面 往另一個界面中傳一些數據(存放數據)
        modelAndView.addObject("name","helong");
        //訪問該頁面的時候會自動跳轉到下面的頁面,並且可以在這個頁面中獲取上方存入的數據
       /* modelAndView.setViewName("/result.jsp");*/
        //重定向到其他的action(也就是直接執行其他action,這個位置.action不能省略)
        modelAndView.setViewName("redirect:/second.action");//發送請求的時候不能省略.action

        return modelAndView;
    }


    /*如果不需要傳遞數據到其他界面就可以直接返回一個String轉發到對應的界面*/
    /*@RequestMapping("/second.action") 這個位置的.action可以省略*/
    @RequestMapping("/second")
    public String show2(){
        ModelAndView modelAndView = new ModelAndView();
        /*轉發到return後面設置的界面*/
      /*  return "/result.jsp";*/
        //重定向到其他頁面(直接跳轉到其他頁面)
        /*重定向是把前端發送過來的數據直接轉發給其他定向的位置
        * 重定向還會改變瀏覽器的地址欄信息*/
        return "redirect:/result.jsp";
    }
}

6.創建發送請求jsp文件,並設置請求

 <a href="${pageContext.request.contextPath}/first.action">發送請求到result.jsp界面獲取數據</a>
  <a href="${pageContext.request.contextPath}/second.action">發送請求到result.jsp界面獲取數據</a>

7.創建接收數據/轉發條跳轉的jsp界面

<!--直接可以通過在控制器中添加的數據模型名稱來獲取所設置的數據-->
<h1>result--界面</h1>
<h2>name:${name}</h2>

 

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