一、springMVC环境搭建和第一个程序
一、导包
spring-aop.jar
spring-context.jar
spring-core.jar
spring-web.jar
spring-bean.jar
spring-webmvc.jar
commons-loging.jar
相较于spring,springMVC多了spring-webMVC.jar和commons-loging.jar 包
二、配置web.xml
<?xml version="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">
<!--欢迎页面-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--配置springDispatcherServlet,所有的请求都交给springMVC的DispatcherServlet处理-->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化信息,告诉DispatcherServlet,springmvc.xml加载的地址-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--项目启动时就加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--配置拦截,将所有的请求都交给springDispatcherServlet处理-->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
普通的servlet流程:
请求-url-pattern -交给对应的servlet去处理
如果现在想用springmvc,而不是普通的servlet,如何告知程序?-如何让springmvc 介入程序:
需要配置一个 Springmvc自带的DispatcherServlet
拦截注意:
/ :拦截所有的请求
/user :拦截以user开头的请求
user/user.do : 只拦截该请求
.action :只拦截以.action结尾的请求
项目中同时兼容 springMVC和Servlet:
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>.action</url-pattern>
</servlet-mapping>
三、创建控制层springMVCHandler.java
package org.hk.handler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//注解说明此类是一个控制类
@Controller
public class springMvcHandler {
//表示拦截的具体请求
@RequestMapping("welcome")
public String welcome(){
return "success";
}
}
四、创建springMVC的配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 扫描 有注解的包 -->
<context:component-scan base-package="org.hk.handler"></context:component-scan>
<!--配置视图解析器(InternalResourceViewResolver) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"></property> //前缀
<property name="suffix" value=".jsp"></property> //后缀
/views/(前缀)+requestMapping返回值+.jsp(后缀)
</bean>
</beans>
可以通过<init-param> 指定配置文件的地址
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
如果不指定的话就必须放到 默认路径:
/WEB-INF/springDispatcherServlet-servlet.xml
五、测试
创建两个jsp,index.jsp和在views目录下创建success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="welcome">点击进入springmvc</a>
</body>
</html>
success.jsp
</head>
<body>
欢迎进入springmvc
</body>
</html>
然后启动Tomcat服务器运行