SpringMVC项目步骤:
注意:mvc必须配置的三大件:处理器映射器,处理器适配器,视图解析器。通常我们只需要手动配置视图解析器(一般公司也已经写好),而处理器映射器和处理器适配器只需要开启注解驱动即可,省去了大段的xml配置。
1.新建web项目:maven
2.导入相关坐标:在pom.xml文件中
<packaging>war</packaging>
<dependencies>
<!-- servlet3.1规范的坐标 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--jsp坐标-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!--spring的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--spring web的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--springmvc的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>cn.itcast</groupId>
<artifactId>mySpirng</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
3.编写web.xml,注册DispatcherServlet
<!--注释DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--关联一个springmvc的配置文件,【servlet-name】-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--起动级别-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--/匹配所有的请求:(不包括.jsp)-->
<!--/*匹配所有的请求:(包括.jsp)-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
4.编写Springmvc-servlet.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-3.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.0.xsd">
<!--扫描包,让包下的注解生效,便于IOC容器统一管理-->
<context:component-scan base-package="com.kuang.controller"/>
<!--让spring MVC不处理静态资源,过滤默认的 如 :css js mp3 mp4....-->
<mvc:default-servlet-handler/>
<!--这就代替上面的配置HandlerMapping和HandlerAdapter-->
<mvc:annotation-driven/>
<!--支持MVC注解驱动:
在SpringMVC中一般采用@RequestMapping完成注解关系
但是想要是@RequestMapping生效,就必须要往上下文中注册
DefaultAnnotationHandlerMapping
和AnnotationMethodHandlerAdapter实例,这两个实例分别在类级别和方法级别处理。
而annotation-driven配置,帮助我们自动完成上述两个实例的注入。-->
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
5.创建对应的控制类controller,并完善前端视图和controller之间的对应。
package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/h1") //浏览器地址栏输入的字符串,它就可以找到hello.jsp
public String hello(Model model){
//封装数据
model.addAttribute("msg","Hello SpringMVC! Annotation!");
return "hello"; //会被视图解析器处理
}
}
6.配置tomcat,运行测试:
注意:1.看Artifacts中对应的项目文件夹下有没有lib文件夹·没有就创建并导入lib包
2.查看pom.xml文件中是否有war 这是打JAR包·没有就不能运行