SpringMVC是Spring的一个模块。MVC=Model+View+Controller,用于与前端交互,处理前端请求。
1.eclipse创建项目
File->New->Dynamic Web Project->输入名称->next->next->勾选Generate web.xml deployment descriptor(默认生成一个web.xml)->finish
2.导包
在WebContent->WEB-INF->lib下
commons-logging
spring-beans
spring-context
spring-core
spring-expression(Spring的5个核心包)
spring-web
spring-webmvc(MVC的核心包)
3.在WebContent->WEB-INF->web.xml中配置SpringMVC的核心(前端)控制器DispatcherServlet
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <!-- 配置前端过滤器 --> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 初始化时加载配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <!-- 表示容器在启动时立即加载Servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
注意点,两个<servlet-name>要相同,<url-pattern>夹着[/]。
4.创建控制器类,需要实现Controller接口,后面学会注解就用注解
package com.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class FirstController implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response){ ModelAndView mav=new ModelAndView(); //向模型对象添加数据 mav.addObject("msg", "搭建第一个SpringMVC程序"); //设置逻辑视图名,实现页面跳转 mav.setViewName("/WEB-INF/jsp/index.jsp"); return mav; } }
5.配置springmvc-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <!-- 配置处理器,映射/firstController请求 --> <bean name="/firstController" class="com.controller.FirstController"/> <!-- 处理器映射器,将处理器的Handle的name作为url进行查找 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- 处理器设配器,配置对处理器中handleRequest()方法调用 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/> </beans>
6.创建视图页面
WebContent->WEB-INF->jsp->index.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>入门程序</title> </head> <body> ${msg} </body> </html>
7.运行进入http://localhost:8080/MVC_01/firstController
由于我的eclipse有些问题,打开无法立即获取界面,需要手动输路径,也不清楚为啥。