SpringMVC入门案例
前期准备:
首先要导入SpringMVC坐标与Servlet坐标
创建SpringMVC控制器类(等同于Servlet功能)
@Controller
public class UserController{
@RequestMapping("/save")
@ResponseBody
public String save(String name){
sout("springmvc save name ==> " + name);
return "{'module':'springmvc save'}";
}
}功能在控制器(Controller)中实现
初始化SpringMVC环境(同Spring环境),设定SpringMVC加载对应的Bean
@Configuration
@ComponentScan("com.forelink.controller")
public class SpringMvcConfig{
}初始化Servlet容器,加载SpringMVC环境,并设置SpringMVC技术处理的请求
导入Tomcat插件
创建模块:
选择New Module – Maven Archetype – 选择JDK – Archetype选择后缀为webapp的骨架
在pom.xml中,可以把多余的 和 删除 右键main文件夹,新建一个源码目录java,然后mark directory为source code源码目录(其实能自动化)
然后导入springmvc和servlet的坐标
springmvc导入org.springframework即可。
创建控制器:
在java下建立文件内容如下
package com.forelink.controller; //包名
import org.springframework.stereotype.Controller; //导入控制器包
import org.springframework.web.bind.annotation.RequestMapping;
//2.定义Controller
//2.1使用@Controller定义Bean
@Controller //类名前要加入注解
public class UserController {
@RequestMapping("/save")
public String save(){
System.out.println("user save...");
return "{'module':'springmvc'}";
}
}
创建SpringMVC配置:
在forelink下创建一个config包用来存放配置文件,创建SpringMvcConfig
// 3. 创建springmvc的配置文件,加载controller对应的bean
package com.forelink.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.forelink.controller")
public class SpringMvcConfig {
}
创建Servlet:
启动服务器时,需要加载Spring,就需要做Tomcat容器启动的配置,来加载SpringMVC配置文件
//4. 定义一个servlet容器启动的配置类,在里面加载spring的配置
package com.forelink.config;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer{
//重写三个方法
//加载SpringMVC容器配置
@Override
protected WebApplicationContext createServletApplicationContext() {
//普通ctx不能用于web环境,要用web类型的ctx(下面的Web)
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
//注册配置 会加载SpringMvcConfig的配置
ctx.register(SpringMvcConfig.class);
return ctx;
}
//设置哪些请求归属SpringMVC管理
@Override
protected String[] getServletMappings() {
//任何请求
return new String[]{"/"};
}
//加载spring容器配置
@Override
protected WebApplicationContext createRootApplicationContext() {
return null; //只有MVC就不用管
}
}
导入Tomcat插件:
在pom.xml中build项加入
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
<finalName>springmvc_01_quickstart</finalName>
</build>
启动程序(Maven启动):
点击运行按钮旁边的三个点,选择Edit,点击加号,然后选中Maven,修改项目地址,在 command line 中加入
tomcat7:run
然后点击确定,就可以看到这个程序通过maven方式启动。
报错总结:
报错内容:
无法访问jakarta.servlet.ServletException
A child container failed during start
解决方法1:
用SpringBoot,内嵌tomcat和maven。
解决方法2:
把web.xml中没用的部分删掉,删到只剩下如下内容时,可以运行。
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<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">
</web-app>