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";
    	    }
    	}
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值