后端-框架-Spring-Spring MVC环境搭建

后端-框架-Spring-Spring MVC环境搭建

1. 导入相关包

  • spring-web-5.1.1.RELEASE.jar
  • spring-webmvc-5.1.1.RELEASE.jar

2. 配置文件

  • web.xml中配置Servlet



<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_3_1.xsd"
  version="3.1"
  metadata-complete="true">
	<servlet>
		<servlet-name>SpringMVCservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		<init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-servlet.xmlparam-value>
		init-param>
    	<load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>SpringMVCservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>
  • 创建Spring MVC配置文件spring-servlet.xml


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

    <mvc:annotation-driven/>
	
	
	<bean name="/index.html" class="cn.smbms.controller.IndexController">
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"/>
		
		<property name="suffix" value=".jsp"/>
	bean>
	
beans>

3.创建Controller处理请求控制器

package cn.smbms.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class IndexController extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		return new ModelAndView("index");//返回逻辑视图名
	}
}

4.创建index.jsp文件

  • 建立至相应目录

5.部署至Tomcat

以下为结构图

后端-框架-Spring-Spring MVC环境搭建_第1张图片

你可能感兴趣的:(后端,框架,Spring,后端,框架,Spring,Spring,MVC环境搭建)