springMVC的作用,处理servlet相关操作
1.使用的jar包
spring-aop.jar
spring-bean.jar
spring-context.jar
spring-core.jar
spring-web.jar
spring-webmvc.jar
2.创建springmvc配置文件
选中常用命名空间:bean、aop、context、mvc
3.servlet流程
普通servlet流程
请求->url-pattern->servlet-class处理相关请求
使用springmvc
让spingmvc介入程序,让springmvc获取全部的请求,配置springmvc自带的servlet
<servlet>
<servlet-name>springDispatherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springDispatherServlet</servlet-name>
<url-pattern>/</url-pattern><!--使用下划线拦截全部请求-->
</servlet-mapping>
4.springmvc控制器
使用注解方式配置控制器,常见包名为controller、handler、service。
请求方式默认为GET。
此处RequestMapping的值与jsp页面链接值相同
package person.demo.handler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("SpringMVCHandler")
public class SpringMVCHandler {
@RequestMapping(value="welcome" , method = RequestMethod.POST)
public String welcome() {
return "success"; //默认使用请求转发,地址栏不变
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="SpringMVCHandler/welcome">springmvc-welcome</a>
</body>
</html>
创建完控制器后在配置文件中将控制器所在包纳入扫描器中
<!-- 配置扫描包 -->
<context:component-scan base-package="person.demo.handler"></context:component-scan
在配置文件中配置视图解析器
作用是拼接路径,若控制器方法返回的是一个‘success’,则经过视图解析器可生成路径‘/views/success.jsp’
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/views/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
5.配置文件的存放位置
- 默认位置
WebContent/WEB-INF/springDispatherServlet-servlet.xml
- 配置方式
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:配置文件存放路径</param-value>
</init-param>
6.全部代码
类SpringMVCHandler
package person.demo.handler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("SpringMVCHandler")
public class SpringMVCHandler {
@RequestMapping(value="welcome" , method = RequestMethod.POST)
public String welcome() {
return "success"; //默认使用请求转发,地址栏不变
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="SpringMVCHandler/welcome">springmvc-welcome</a>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
welcome to springmvc
</body>
</html>
配置文件:/springDispatherServlet-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: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="person.demo.handler"></context:component-scan>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/views/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVCProject1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springDispatherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param> -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>