环境: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"/>
<!-- <!–视图解析–>-->
<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"; } }