第一步:需要的jar包:
<dependency>
<groupId>org.springframework
<artifactId>spring-context
<version>5.0.2.RELEASE
</dependency>
<dependency>
<groupId>org.springframework
<artifactId>spring-webmvc
<version>5.0.2.RELEASE
</dependency>
<!–本案例使用Tomcat进行部署,Tomcat自己有servlet-api的包,需要部署的时候排出这个包–>
<dependency>
<groupId>javax.servlet
<artifactId>servlet-api
<version>2.5
</dependency>
<!–本案例使用Tomcat进行部署,Tomcat自己有servlet-api的包,需要部署的时候排出这个包–>
<dependency>
<groupId>javax.servlet.jsp
<artifactId>jsp-api
<version>2.0
</dependency>
第二步:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns=“http://java.sun.com/xml/ns/javaee”
xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”
id=“WebApp_ID” version=“2.5”>
<!–这个web.xml的头一定要用2.5这个版本的如果用2.3在解析一些jsp资源的时候会报错–>
<display-name>Archetype Created Web Application
<servlet>
<servlet-name>hello
<servlet-class>org.springframework.web.servlet.DispatcherServlet
<init-param>
<!–配置加载SpringMVC的核心配置文件–>
<!–contextConfigLocation:是固定写法,是springMVC的属性,不能改–>
<param-name>contextConfigLocation
<param-value>classpath:springmvc.xml
</init-param>
<!–配置在什么时候启动SpringMVC容器–>
<!–使用这种方式:当配置的值是大于0的正整数的时候,SpringMVC是随着Servlet容器(Tomcat)的启动而启动–>
<!–如果为0或者这不进行配置,SpringMVC的容器是用户第一次访问的时候启动–>
<load-on-startup>1
</servlet>
<servlet-mapping>
<servlet-name>hello
<!-- 所有的请求都会被Servlet处理 -->
<url-pattern>/
</servlet-mapping>
</web-app>
第三步:编写springmvc.xml
<?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: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/context
http://www.springframework.org/schema/context/spring-context.xsd”>
<!--配置注解包扫描,因为SpringMVC的开发基本上都是基于注解的-->
<context:component-scan base-package="com.xzw.controller"/>
<!--配置视图解析器,告诉SpringMVC需要返回的jsp在哪里-->
<!--需要返回jsp就有jsp的视图解析器-->
<!--需要返回xlsx(Excel)就有xls的视图解析器-->
<!--需要返回PDF就有PDF的视图解析器-->
<!--需要返回HTML就有静态化模板的视图解析器-->
<!--InternalResourceViewResolver:jsp的视图解析器-->
<!--Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" ->-->
<!--"/WEB-INF/jsp/test.jsp" 配置前缀prefix,配置后缀suffix,编写java代码的时候,只需要返回viewname名字即可-->
<!--视图解析器会自动进行拼接,获取jsp的物理地址,就能找到jsp并进行使用-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
第四部: 编写HelloController(就相当于之前使用Servlet里面的业务逻辑)
@Controller
public class HelloController {
//本例中,用户访问的固定地址
//http://127.0.0.1:8080
//注解中有/hello
//最终的访问地址就是 固定的地址+注解里的参数
//http://127.0.0.1:8080/hello
//@RequestMapping:把用户请求的url地址和Controller的方法进行映射
//本例中,把http://127.0.0.1:8080/hello这个请求和hello()这个方法进行映射
//效果,只要用户是http://127.0.0.1:8080/hello请求,最终就是由hello方法处理
@RequestMapping("/hello")
public String hello() {
System.out.println("入门案例,执行到这里了");
//设置返回到哪个jsp
// 在spirngmvc.xml中配置了视图解析器
//<!--Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="success" ->-->
///WEB-INF/jsp/success.jsp"
//返回的字符串,就相当于上面的例子中的viewname
return "success";
}
}
编写一个success.jsp,一个简单的案例就完成了。如果需要数据类型之间的转换
实现自定义转换器
```java
//本例是需要把字符串转为时间格式
//泛型S:source 源 是String
//泛型T:target 目标 是Date
public class DateConverter implements Converter<String,Date> {
@Override
public Date convert(String source) {
//字符串格式2011-11-11
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
//把字符串转为Date类型
Date date = sdf.parse(source);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
在springmvc.xml中配置转换器
```xml
<!--配置注解驱动,自动加载三大组件-->
<!--把转换器设置给注解驱动-->
<mvc:annotation-driven conversion-service="conversionService"/>
<!--配置转换器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.itheima.converter.DateConverter"/>
</set>
</property>
</bean>