SpringMVC环境配置(idea)

1、创建Maven项目

SpringMVC环境配置(idea)_第1张图片

2、配置web

SpringMVC环境配置(idea)_第2张图片

3、项目结构

SpringMVC环境配置(idea)_第3张图片

4、在pom.xml文件中添加依赖坐标


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>org.examplegroupId>
    <artifactId>springartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>warpackaging>
    
    <properties>
        <spring.version>4.1.3.RELEASEspring.version>
    properties>
    <dependencies>
        
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>compilescope>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>
    dependencies>
project>

5、配置web.xml


<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">
    
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc-config.xmlparam-value>
        init-param>
    servlet>
    
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>

6、添加springMVC配置文件模板,便于以后添加spring-config.xml配置文件

SpringMVC环境配置(idea)_第4张图片


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc
						http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
						http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
						http://www.springframework.org/schema/context
          				http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	
	<mvc:default-servlet-handler/>
	
	
	<mvc:annotation-driven>mvc:annotation-driven>
	
	
	<context:component-scan base-package="包名">
	context:component-scan>
	
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="页面存放路径"/>
		<property name="suffix" value=".jsp"/>
	bean>
beans>

7、配置Tomcat

8、测试

/**
 * @Controller作用:
 * 1、表示当前类属于Controller层
 * 2、将当前类的对象的创建交给spring容器负责
 */
@Controller
public class HelloController {

	@RequestMapping("/hello")
	public String toHello() {
		System.out.println("Hello springMVC ...");
		return "home";
	}
}

SpringMVC环境配置(idea)_第5张图片

你可能感兴趣的:(教程,java,spring)