上一节我们学习了Spring JDBC相关知识,接下来我们继续学习SSM框架的Spring MVC相关知识。
查看官方文档地址
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.1.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.freemarkergroupId>
<artifactId>freemarkerartifactId>
<version>2.3.28version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>5.1.9.RELEASEversion>
dependency>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
init-param>
<load-on-startup>0load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<context:component-scan base-package="com.imooc.springmvc">context:component-scan>
<mvc:annotation-driven conversion-service="conversionService">
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=utf-8value>
<value>text/html;charset=utf-8value>
list>
property>
bean>
mvc:message-converters>
mvc:annotation-driven>
<mvc:default-servlet-handler/>
@Controller
public class TestController {
@GetMapping("/t") //localhost/t
@ResponseBody //直接向响应输出字符串数据,不跳转页面
public String test(){
return "Hello Spring MVC";
}
}
注解 | 作用 |
---|---|
@RequestMapping | 通用绑定 |
@GetMapping | 绑定Get请求 |
@PostMapping | 绑定Post请求 |
@PostMapping("/apply")
@ResponseBody
public String apply(@RequestParam(value = "n",defaultValue = "ANON") String name, String course, Integer[] purpose){
System.out.println(name);
System.out.println(course);
for (Integer p : purpose) {
System.out.println(p);
}
return "SUCCESS";
}
@PostMapping("/apply")
@ResponseBody
public String apply(String name, String course, @RequestParam List<Integer> purpose){
System.out.println(name);
System.out.println(course);
for (Integer p : purpose) {
System.out.println(p);
}
return "SUCCESS";
}
@PostMapping("/apply")
@ResponseBody
public String apply(Form form){
return "SUCCESS";
}
案例:假如我们某个系统的用户有自己的用户名和密码,另外每个用户有自己的身份证实名信息。
高级方法(时间转换器)
public class MyDateConverter implements Converter<String, Date> {
public Date convert(String s) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = sdf.parse(s);
return d;
} catch (ParseException e) {
return null;
}
}
}
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.imooc.springmvc.converter.MyDateConverter"/>
set>
property>
bean>
乱码是因为Tomcat默认使用的字符集是ISO-8859-1,属于西欧字符集。
<filter>
<filter-name>characterFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<mvc:annotation-driven conversion-service="conversionService">
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=utf-8value>
<value>text/html;charset=utf-8value>
list>
property>
bean>
mvc:message-converters>
mvc:annotation-driven>
<dependency>
<groupId>org.freemarkergroupId>
<artifactId>freemarkerartifactId>
<version>2.3.28version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>5.1.9.RELEASEversion>
dependency>
<bean id="ViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="contentType" value="text/html;charset=utf-8"/>
<property name="suffix" value=".ftl"/>
bean>
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl"/>
<property name="freemarkerSettings">
<props>
<prop key="defaultEncoding">UTF-8prop>
props>
property>
bean>
@Controller
@RequestMapping("/fm")
public class FreemarkerController {
@GetMapping("/test")
public ModelAndView showTest(){
ModelAndView mav = new ModelAndView("/test");
User user = new User();
user.setUsername("andy");
mav.addObject("u", user);
return mav;
}
}
本次我们主要学习了Spring MVC的基础知识,通过本节课的学习,我们对于Spring MVC已经有了基本的认识,可以尝试进行使用,接下里我们还将进一步学习Spring MVC的相关知识~一起看看吧