****1:首先new一个Maven项目****
注意选择webapp!
2:然后配置你的GroupId 和你的ArtifactId
3:紧接着配置你的Mvaen(默认的也可以,用本地的也行)
4:然后选择你的项目名称,和项目路径
5:选择创建
6:重点记住别选This Window,如果这里存放过项目会一起加载,选择New Window
7:等待他加载完别手贱去点不然要凉凉到时候错误都找不到!
8:引入pom文件中的依赖,这两个都要点了,import导入jar包,不点他不会导入的
这就新建了一个Maven工程
9:然后我们打开src,这里面的目录结构是这样的一般eclipse会自动帮我们创建java和resources还有测试的java和resources这里面没有自动创建,而且还需要设置,下面步骤就有
new一个文件夹,在main目录下new
这就是平常的结构,但是我们还要把他们分别设置为源文件目录和资源文件目录
选择这个Sources Root设置为源码权限
再选中resources文件设置Resources Root
点击它然后目录就会变成这样:
10:现在引入jar包,记得在Maven上配置你的本地仓库路径
打开pom.xml
输入:
记得一定要手动点击一下不然的话他不会导入,每次添加新的依赖之后要import导入(这是必须的不然找不到jar包会报错)
11:打开我们的web.xml
配置好他的各种配置:<?xml versinotallow="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"
metadata-complete="true">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<!--配置DispatcherServlet控制器的名字-->
<servlet-name>dispatcherServlet</servlet-name>
<!--DispatcherServlet的类路径-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置springmvc的配置-->
<init-param>
<!--固定写法名称-->
<param-name>contextConfigLocation</param-name>
<!--配置spring的xml的路径-->
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<!--设置spring的容器随着服务的启动而启动,而不是别人访问的时候启动-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--spring的servlet配置-->
<servlet-mapping>
<!--上面配置的名称-->
<servlet-name>dispatcherServlet</servlet-name>
<!--需要拦截的路径-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置session过期时间120分钟 -->
<session-config>
<!--此参数设置过期时间分钟-->
<session-timeout>120</session-timeout>
</session-config>
<!--配置springmvc的编码-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<!--设置编码-->
<param-name>encoding</param-name>
<!--设置编码格式-->
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!--配置过滤器-->
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<!--过滤范围-->
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置spring的资源路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-context.xml</param-value>
</context-param>
<!--配置spring容器的监听器防止容器溢出-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--设置404找不到页面错误的返回提示-->
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/404.jsp</location>
</error-page>
</web-app>
有些目录会出现红色警告原因是没有创建文件夹,可以把鼠标放在文件名前alt加回车可以快速创建文件
12:打开spring-mvc.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!--开启SpringMvc注解-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置扫面Contrller控制器的包的范围-->
<context:component-scan base-package="club.kang.blog.web.contrller"></context:component-scan>
<bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置spring容器加载的默认路径的前缀-->
<property name="prefix" value="/WEB-INF/views/"></property>
<!--配置spring容器加载的默认的后缀-->
<property name="suffix" value=".jsp"></property>
<!--重定向的时候是否添加路径,如果true则自动写成/WEB-INF/views/index.jsp,否则全路径访问-->
<property name="redirectContextRelative" value="true"></property>
</bean>
<!--配置spring的访问静态资源自动映射-->
<mvc:default-servlet-handler/>
</beans>
13:新建Contrller控制器创建包名
建好之后新建Class文件
写入
package club.kang.blog.web.contrller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//配置Contrller注解表示是个控制器
@Controller
public class Hello {
// 配置访问路劲默认和输入hello访问hello
@RequestMapping({"/","/hello"})
public String hello(){
return "hello";
}
}
14:然后我们选中views文件夹新建一个hello.jsp文件
15:然后就到了配置我们的tomcat服务器,他会根据你配置的服务器在内部创建你配置的虚拟服务器
选中Edit
默认是没有服务器的我们要手动添加
这里也没有我们的Tomcat服务器
选中34 items
下滑找到我们的Tomcat Server
Load添加
这是页面
从上往下分别是
1、tomcat的路径
2、默认使用什么浏览器打开
3、访问路径(这个不用设置配置端口就可以了)
注:4、选中JRE(记住JRE选择自己本地安装的jre目录)上面报错是因为JRE没有配置
5、设置端口号
config配置下你的tomcat路径
选中目录
ok
再把端口和浏览器选中
然后别急
打开Deplovment
添加我们的war包文件
选中第一个
添加
Apply一下然后ok
这时候点击一下绿色箭头
启动它,稍微等一下会慢一点点
然后会自动打开
然后就成功访问到页面了