springmvc的使用

環境:java1.8/ maven 3.61/ideal

maven工程的創建

新建一個項目
在這裏插入圖片描述
避免maven工程創建太慢,有兩種方案
1 ,這個是使用idea自帶的maven:
archetypeCatalog = internal
在這裏插入圖片描述
不過創建過程還是有點慢,這個我又在setting中設置其他屬性,
-DarchetypeCatalog=internal
在這裏插入圖片描述
2,第二種,選擇自己安裝的maven
在這裏插入圖片描述修改settings.xml鏡像,替換mirror:

nexus-aliyun
central
Nexus aliyun
http://maven.aliyun.com/nexus/content/groups/public

這樣項目就創建好了。
在這裏插入圖片描述
好了,項目創建好了,是不是少了文件目錄,如java,那就自己新建一個,
不過,爲啥這個java目錄沒法創建class,沒有這個選項
需要設置一下,java --Sources Root; resources ----- Resources Root

在這裏插入圖片描述

  • 引入jar包
    修改pom.xml;
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
     <version>5.2.5.RELEASE</version>
   </dependency>
  • 配置 tomcat
    在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

war模式:將WEB工程以包的形式上傳到服務器 ;
war exploded模式:將WEB工程以當前文件夾的位置關係上傳到服務器;
(1)war模式這種可以稱之爲是發佈模式,看名字也知道,這是先打成war包,再發布;

(2)war exploded模式是直接把文件夾、jsp頁面 、classes等等移到Tomcat 部署文件夾裏面,進行加載部署。因此這種方式支持熱部署,一般在開發的時候也是用這種方式。

需要熱部署的參考這個:
https://blog.csdn.net/xlgen157387/article/details/56498938

  • 在 webapp/WEB-INF/目錄下新建一個index.jsp
  • 啓動項目
    不意外這個瀏覽器會自動跳出來一個網頁:http://localhost:8080/mvc_war/
    內容就是index.jsp的內容
  • 不過這只是剛開始,要是需要使用control,還需要修改配置

control的使用

  • 配置contextConfig
    • 配置 webapp/WEB-INF/web.xml
   <!--
      dispatcherServlet 前端控制器
      -->
 <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- 配置Servlet的初始化參數,讀取springmvc的配置文件,創建spring容器 -->
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <!--文件名自己定義-->
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>
      <!-- 配置servlet啓動時加載對象 -->
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  • 添加 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"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  <!--修改base-package包名-->
  <context:component-scan base-package="com.zzg"/>
  
          <!--    &lt;!&ndash;視圖解析&ndash;&gt;-->
  <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <!--視圖文件夾, 後面一定要加/-->
  <property name="prefix" value="/WEB-INF/pages/"/>
  <!-- 後綴-->
  <property name="suffix" value=".jsp"></property>
  </bean>
          <!-- 開啓springmvc框架註解支持-->
  <mvc:annotation-driven></mvc:annotation-driven>
          </beans>

  • 編寫control

    
    	package com.zzg.control;
    
    	import org.springframework.stereotype.Controller;
    	import org.springframework.web.bind.annotation.RequestMapping;
    	import org.springframework.web.bind.annotation.RequestMethod;
    	import org.springframework.web.bind.annotation.ResponseBody;
    	
    	@Controller
    	public class TestControl {
    	
    	    @RequestMapping(value = "/test" ,method = RequestMethod.GET )
    	    //表示返回json
    	    @ResponseBody
    	    public String test(){
    	        System.out.println(12222);
    	        return "success";
    	    }
    	
    		//返回success.jsp
    	    @RequestMapping(value = "/test1" ,method = RequestMethod.GET )
    	    public String test1(){
    	        System.out.println(12222);
    	        return "success";
    	    }
    	}
    
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章