1、HelloSpringMvc
直接说原理相信很多人都有点迷,先写个入门demo在一步一步分析原理
1.1 运行环境准备
新建maven项目,项目名右击添加web支持接着导入相关的maven依赖
<?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>com.xuan
</groupId>
<artifactId>SpringMvc
</artifactId>
<packaging>pom
</packaging>
<version>1.0-SNAPSHOT
</version>
<modules>
<module>Spring-01-servlet
</module>
<module>Springmvc-02-helloMvc
</module>
</modules>
<dependencies>
<dependency>
<groupId>junit
</groupId>
<artifactId>junit
</artifactId>
<version>4.12
</version>
<scope>test
</scope>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-webmvc
</artifactId>
<version>5.1.9.RELEASE
</version>
</dependency>
<dependency>
<groupId>javax.servlet
</groupId>
<artifactId>servlet-api
</artifactId>
<version>2.5
</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp
</groupId>
<artifactId>jsp-api
</artifactId>
<version>2.0
</version>
</dependency>
<dependency>
<groupId>javax.servlet
</groupId>
<artifactId>jstl
</artifactId>
<version>1.2
</version>
</dependency>
</dependencies>
</project>
编写springmvc的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="resourceViewResolver>">
<property name="prefix" value="WEB-INF/js/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="com.xuan.HelloController" id="/hello"/>
</beans>
编写web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>springmvc
</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation
</param-name>
<param-value>classpath:springmvc-servlet.xml
</param-value>
</init-param>
<load-on-startup>1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc
</servlet-name>
<url-pattern>/
</url-pattern>
</servlet-mapping>
</web-app>
1.2 编写controller
public class HelloController implements Controller {
public ModelAndView
handleRequest(HttpServletRequest httpServletRequest
, HttpServletResponse httpServletResponse
) throws Exception
{
ModelAndView modelAndView
= new ModelAndView();
modelAndView
.addObject("msg","第一个springmvc");
modelAndView
.setViewName("hello");
return modelAndView
;
}
}
1.3 编写跳转的视图hello.jsp
<%--
Created by IntelliJ
IDEA.
User
: J
Date
: 2020/10/6
Time
: 11:26
To change
this template use File
| Settings
| File Templates
.
--%>
<%@ page contentType
="text/html;charset=UTF-8" language
="java" %>
<html
>
<head
>
<title
>Hello SpringMvc
</title
>
</head
>
<body
>
<h1
>$
{msg
}</h1
>
</body
>
</html
>
1.4 404解决方法
最后配置一下tomcat,然后输入访问地址,第一次访问页面可能会出现404,这时就需要把相关依赖添加到class下的lib中,具体操作步骤如下
按住shift+ctrl+alt+s 打开Project Structure 点击Artifacts 最后就运行成功了
1.5 运行原理分析