前置步骤
工程类型(webapp)
导包
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.1.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>5.1.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.1.9.RELEASEversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.1version>
<configuration>
<port>80port>
<path>/path>
configuration>
plugin>
plugins>
build>
中央控制器的配置
<servlet>
<servlet-name>DispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:abc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>DispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
如何创建spring容器
<beans xmlns="http://www.springframework.org/schema/beans"
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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.itheima"/>
beans>
编写Controller(servlet)
@Controller
public class UserController {
//设定当前方法的访问映射地址
@RequestMapping("/abc")
//设置当前方法返回值类型为String,用于指定请求完成后跳转的页面
public String save(){
System.out.println("user mvc controller is running ...");
//设定具体跳转的页面
return "success.jsp";
}
@RequestMapping("/hello")
public String hello(String param) {
System.out.println(param);
return "success.jsp";
}
}
<mvc:default-servlet-handler/>
web.xml中配置过滤器
<filter>
<filter-name>characterEncodingFilterfilter-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>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
tomcat插件uri编码配置
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.1version>
<configuration>
<port>80port>
<path>/path>
<uriEncoding>UTF-8uriEncoding>
configuration>
plugin>
plugins>
build>
** tomcat启动的时候回去加载ServletContainerInitializer的实现类,但是具体的实现类需要在META-INF这下面配置一个配置文件**
模拟SpringBoot内嵌tomcat
基本数据类型(String, int)
404问题:
路径写错了
你根本没有Controller注解
你根本没扫描包( spring-mvc.xml)
根本没有加载配置文件(web.xml)
资源目录写错(resources)
配置静态资源后
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
400原因
普通属性如何注入
引用属性如何注入
集合属性注入
map属性注入
请求路径
http://localhost/requestParam3?name=Jock&age=39
//http://localhost/requestParam3?name=Jock&age=39
@RequestMapping("/requestParam3")
public String requestParam3(User user){
System.out.println(user);
return "page.jsp";
}
//方法传递普通类型的数组参数,URL地址中使用同名变量为数组赋值
//http://localhost/requestParam9?nick=Jockme&nick=zahc
@RequestMapping("/requestParam9")
public String requestParam9(String[] nick){
System.out.println(nick[0]+","+nick[1]);
return "page.jsp";
}
//方法传递保存普通类型的List集合时,无法直接为其赋值,需要使用@RequestParam参数对参数名称进行转换
//http://localhost/requestParam10?nick=Jockme&nick=zahc
@RequestMapping("/requestParam10")
public String requestParam10(@RequestParam("nick") List<String> nick){
System.out.println(nick);
return "page.jsp";
}
配置文件的方式, 格式根据自己的需求修改
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="org.springframework.format.datetime.DateFormatter">
<property name="pattern" value="yyyy-MM-dd"/>
bean>
set>
property>
bean>
注解形式
@RequestMapping("/requestParam11")
public String requestParam11(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
System.out.println(date);
return "page.jsp";
}
实现一个接口Converter<参数, 需要得到的类型>
实现convert方法
public class MyDateConverter implements Converter<String, Date> {
//重写接口的抽象方法,参数由泛型决定
public Date convert(String source) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
//类型转换器无法预计使用过程中出现的异常,因此必须在类型转换器内部捕获,不允许抛出,框架无法预计此类异常如何处理
try {
date = df.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
配置将转换器添加到spring对应的组件
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.itheima.converter.MyDateConverter"/>
set>
property>
bean>
// target代表你能写在那些地方
// type代表类型, 能写在类上
// METHOD 方法上
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String name() default "";
//路径
//Alias别名
@AliasFor("path")
String[] value() default {};
//路径
@AliasFor("value")
String[] path() default {};
// method, 指定请求方式(GET,POST)
RequestMethod[] method() default {};
//参数
String[] params() default {};
//请求头信息指定
String[] headers() default {};
//接收的媒体类型
String[] consumes() default {};
//响应的媒体类型
String[] produces() default {};
}
** 没有配置视图解析器**
@RequestMapping("/showPage")
public String showPage() {
System.out.println("user mvc controller is running ...");
//返回值直接为webapp下的路径
return "/system/userlist.jsp";
}
@RequestMapping("/showPage")
public String showPage() {
System.out.println("user mvc controller is running ...");
//返回值直接为webapp下的路径
return "/WEB-INF/page/page.jsp";
}
@RequestMapping("/showPage2")
public String showPage2() {
System.out.println("user mvc controller is running ...");
// forward 转发
// redirect 重定向
return "redirect:/WEB-INF/page/page.jsp";
}
配置视图解析器
<context:component-scan base-package="com.itheima"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
bean>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
在返回值中直接写上/WEB-INF/page/的静态页面名称即可
@RequestMapping("/showPage3")
public String showPage3() {
System.out.println("user mvc controller is running ...");
return "page";
}
直接利用request对象存储数据
@RequestMapping("/showPageAndData1")
public String showPageAndData1(HttpServletRequest request) {
request.setAttribute("name","itheima");
return "page";
}
使用model对象存储数据,实际上依然存储在request中
@RequestMapping("/showPageAndData2")
public String showPageAndData2(Model model) {
//添加数据的方式,key对value
model.addAttribute("name","Jock");
Book book = new Book();
book.setName("SpringMVC入门案例");
book.setPrice(66.66d);
//添加数据的方式,key对value
model.addAttribute("book",book);
return "page";
}
使用modelAndView
//使用ModelAndView形参传递参数,该对象还封装了页面信息
@RequestMapping("/showPageAndData3")
public ModelAndView showPageAndData3(ModelAndView modelAndView) {
//ModelAndView mav = new ModelAndView(); 替换形参中的参数
Book book = new Book();
book.setName("SpringMVC入门案例");
book.setPrice(66.66d);
//添加数据的方式,key对value
modelAndView.addObject("book",book);
//添加数据的方式,key对value
modelAndView.addObject("name","Jockme");
//设置页面的方式,该方法最后一次执行的结果生效
modelAndView.setViewName("page");
//返回值设定成ModelAndView对象
return modelAndView;
}
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>2.9.0version>
dependency>
<mvc:annotation-driven/>
@RequestMapping("/mapdata")
@ResponseBody
public Map<String, Object> mapdata() {
Map map = new HashMap();
map.put("name", "zhangsan");
map.put("age", 12);
return map;
}
@RequestMapping("/showData4")
@ResponseBody
public Book showData4(HttpServletRequest request, HttpServletResponse response) {
return book;
}
//@CookieValue
//@RequestHeader
//@SessionAttribute