1. 首先创建maven->webapp
把目录补全,java和resource文件。配依赖,创建前端控制器,创建spring配置文件。
1.在pom.xml中配置依赖:
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>cn.itcast</groupId>
<artifactId>springmvc_day01_01_start</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springmvc_day01_01_start Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<!--依赖-->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>springmvc_day01_01_start</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
当时由于是自己从网上下载的maven并配置,发现org.framework无法下载,从采用这个方法,使用了idea自身的maven并引用清华源,ok了。
参考链接:
自己配置的maven无法下载依赖(https://zhuanlan.zhihu.com/p/71998219)
2.配前端控制器(servlet)
最基本配置:
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.springmvc配置文件
在resource下new一个xml configuration file
<?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.xsd">
</beans>
4.部署服务器
run->edit configuration ->创建springmvc服务器(tomcat8.5.40),deployment添加该项目。
2.编写入门程序
1.在java中创建一个类hellospring
package cn.itcast.controller;
//控制器的类
public class HelloController {
public String sayHello(){
System.out.println("Hello StringMVC");
return null;
}
}
要想运行该类中的函数,需要创建一个对象,因此在resource——>springmvc.xml中修改bean:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
开启注解扫描获得hellocontroller类:
<context:component-scan base-package="cn.itcast"/>
在类上加一个注解:@controller
等于把这个类交给ioc管理
如果想要运行类中的函数,则需要在类上加一个注解@RequestMapping(path="/hello"),即请求映射,其中“/hello”即该函数的请求映射
@Controller
public class HelloController {
@RequestMapping(path="/hello")
public String sayHello(){
System.out.println("Hello StringMVC");
return null;
}
}
为了运行函数,需要加载springmvc.xml,使其可以扫描类的函数,因此在前端控制器,在servlet后添加init-param,使其加载classpath路径下的配置文件。
<servlet>
<servlet-name>dispatcherServlet</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>
添加load-on-start,使其在启动服务器时就可创建对象servlet,对象一创建,便可扫描注解,从而获得函数。
@Controller
public class HelloController {
@RequestMapping(path="/hello")
public String sayHello(){
System.out.println("Hello StringMVC");
return "success";
}
}
当函数运行成功,print“success”,为了让其跳转另一界面,因此在webapp->WEB-INF创建文件夹(directory)pages,在pages创建success.jsp,使其作为success的跳转界面。
为了使其成功跳转,需要在resource创建视图解析器。视图解析器是为了执行跳转某一界面的功能。prefix表示执行文件目录,suffix表示执行文件后缀。
<!--视图解析器对象-->
<bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
在后面加上mvc框架注解的支持:
<!--开启SpringMVC框架注解的支持-->
<mvc:annotation-driven/>
配置完毕,运行tomcat,发现报错:
解决方法:tomcat乱码
运行,ok!
运行逻辑:
1.启动服务器,加载配置文件:
创建servlet对象(指挥中心)
加载springmvc.xml配置文件
配置文件运行,扫描注解,将controller类进入容器中,创建成对象
2.发送请求,后台处理请求:
超链接请求被dispatcherServlet拦截,运行springmvc.xml,运行注解,从而print“hello springmvc”,之后返回success又被dispatcherServlet拦截,之后继续运行springmvc.xml,运行另一个对象(因为有bean)InternalResourceViewResolver,进而跳转success,jsp,进而显示入门成功。