SpringMVC入门环境搭建
1.项目结构
项目结构如图:
2.导入依赖
项目都是使用maven创建的webapp,依赖由pom.xml管理
pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!--springmvc核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!--servlet-api-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
3.编写springmvc配置文件
在springmvc.xml中编写。
springmvc.xml:
<!--开启注解扫描-->
<context:component-scan base-package="com"/>
<!--注册处理器映射器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!--注册处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!--注册视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
其中
<mvc:annotation-driven/> 代替处理器映射器,处理器适配器
4.配置springmvc的核心Servlet
web.xml:
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--指定springmvc配置文件位置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
注意: 这里还要加载spring配置,通过在servlet写initparam标签,还是contextConfigLocation属性,value用来加载springmvc配置文件
6.创建控制器
创建controller包
可以写自己的业务处理代码了,响应前端消息,进行转发重定向等。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloController {
/**
* @RequestMapping 修饰范围: 用在类上和方法上
* 1.用在方法上用来给当前方法加入指定的请求路径
* 2.用在类上是用来给类中的所有方法加入统一的请求路径,访问方法之前需要加入类上的@RequestMapping的路径
* 相当于struts2中package标签的namespace属性
* 注意:一旦类上和方法同时加入@requestMapping访问时必须
* /项目名/类上@RequestMapping的路径/访问方法RequestMapping的路径
*
* */
@RequestMapping(value="/hello")
public String hello(){
//1.收集数据
//2.调用业务方法
//3.处理响应
System.out.println("Hello SpringMVC!");
return "index"; //页面逻辑名
}
@RequestMapping("save")
public String save(){
//1.收集数据
//2.调用业务方法
//3.处理响应
System.out.println("save method.....");
return "index";
}
}
7.部署项目,启动项目测试
自行配置添加tomcat,然后访问网址
访问路径: http://localhost:8989/springmvc_day1/hello/hello