一:创建一个SpringMVC demo程序
1.在lib文件夹下添加SprigMVC相应的jar包
2.添加Web.xml配置文件中关于SpringMVC的配置
<servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- SpringMVC的上下文配置文件 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:servlet-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
3.在src下添加servlet-mvc.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置springmvc自动扫描的包 --> <context:component-scan base-package="com.jredu.controller"> <!-- 可以配置过滤不需要的文件或需要的文件 --> </context:component-scan> <!-- 设置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/pages/" p:suffix=".jsp"/> <mvc:default-servlet-handler /> <mvc:annotation-driven> </mvc:annotation-driven> </beans>
4.在WEB-INF文件夹下创建名为pages的文件夹,用来存放jsp视图。创建一个hello.jsp,在body中添加用于显示的部分
5.创建控制类HelloController
package com.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/hello") public class HelloController { @RequestMapping("/hello1.action") public String hello1() { return "hello"; } /** * method:限制访问的方式(方法) * @return */ @RequestMapping(value="/hello2.action",method={RequestMethod.GET,RequestMethod.POST}) public String hello2() { return "hello"; } /** * params:限制必须按照设置传参才可以访问 * @return */ @RequestMapping(value="/hello3.action",params={"uname","pwd"}) public String hello3() { return "hello"; } @RequestMapping(value="/hello4.action",headers={"Cookie"}) public String hello4() { return "hello"; } /** * ?匹配任意一个字符 * @return */ @RequestMapping("/h?") public String hello5() { return "hello"; } /** * *匹配任意多个字符 * @return */ @RequestMapping("/a*") public String hello6() { return "hello"; } /** * **匹配任意多重路径 */ @RequestMapping("/a/**/hello7.action") public String hello7(){ return "hello"; } /** * 支持占位符URL映射 * 通过@PathVariable可以将URL中占位符参数 * 绑定到控制器的处理方法中的参数中。 * @param pwd * @param uname * @return */ @RequestMapping("/hello8.action/{uname}/{pwd}") public String hello8(@PathVariable("pwd") String pwd2,@PathVariable("uname") String uname){ System.out.println(uname); System.out.println(pwd2); return "hello"; } }
6.启动tomcat,在浏览器访问localhost:8080/SpringMVC/hello/hello.action
二、配置解析
1.Dispatcherservlet
DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据相应的规则分发到目标Controller来处理,是配置spring MVC的第一步。
2.InternalResourceViewResolver
视图名称解析器
3.以上出现的注解
@Controller 负责注册一个bean 到spring 上下文中
@RequestMapping 注解为控制器指定可以处理哪些 URL 请求
三、SpringMVC常用注解
@Controller
负责注册一个bean 到spring 上下文中
@RequestMapping
注解为控制器指定可以处理哪些 URL 请求
@RequestBody
该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上 ,再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上
@ResponseBody
该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区
@ModelAttribute
在方法定义上使用 @ModelAttribute 注解:Spring MVC 在调用目标处理方法前,会先逐个调用在方法级上标注了@ModelAttribute 的方法
在方法的入参前使用 @ModelAttribute 注解:可以从隐含对象中获取隐含的模型数据中获取对象,再将请求参数 –绑定到对象中,再传入入参将方法入参对象添加到模型中
@RequestParam
在处理方法入参处使用 @RequestParam 可以把请求参 数传递给请求方法
@PathVariable
绑定 URL 占位符到入参
@ExceptionHandler
注解到方法上,出现异常时会执行该方法
@ControllerAdvice
使一个Contoller成为全局的异常处理类,类中用@ExceptionHandler方法注解的方法可以处理所有Controller发生的异常