springMVC
springMVC框架是用于整合servlet层使用的,即控制层(contoller)
1,springMVC四大组件
DispatcherServlet : 前端控制器,接收所有请求(如果配置/不包 含 jsp) 。
HandlerMapping: 解析请求格式的.判断希望要执行哪个具体的方法。
HandlerAdapter: 负责调用具体的方法.。
ViewResovler:视图解析器.解析结果,准备跳转到具体的物理视图。
运行原理如下图所示:
2,springMVC配置
第一:
配置前端控制器
DispatcherServlet
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> 第二步:在 src 下新建 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:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描注解 -->
<context:component-scan
base-package="com.bjsxt.controller"></context:component-scan>
<!-- 注解驱动 -->
<!--
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandler
Mapping -->
<!--
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerA
dapter -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 静态资源 -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/images/"
mapping="/images/**"></mvc:resources>
</beans>
第三步:在 web.xml 中配置 Filter 防止乱码
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.Charac
terEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
第四步:配置视图解析器
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalR
esourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
注意:如果希望不执行自定义视图解析器,在方法返回值前面添加 forward:或 redirect;
3,@RequestParam()注解
使用在方法参数的注解,功能有:
1)前端传过来的参数名和在控制器方法的参数名不同时,使用该注解来获取前端参数。
public String demo(@RequestParam(value="name1")String name,@RequestParam(value="age1")int age){
System.out.println("执行 demo"+" "+name+""+age);
}
2)设置默认值
public String page(@RequestParam(defaultValue="2")int pageSize,@RequestParam(defaultValue="1") intpageNumber){
System.out.println(pageSize+" "+pageNumber);
return "main.jsp";
}
3)强制要求必须有某个参数:设置属性为required=true
4,restful 传值方式
在html中编写连接
<a href="demo8/123/abc">跳转</a>
在控制器中:
/*在控制器中在@RequestMapping 中一定要和请求格式对应{名称} 中名称自定义名称
@PathVariable 获取@RequestMapping 中内容,默认按照方法参数名称去寻找.*/
@RequestMapping("demo8/{id1}/{name}")
public String demo8(@PathVariable String name,@PathVariable("id1") int age){
System.out.println(name +" "+age);
return "/main.jsp";
}
5,@ResponseBody注解
在方法上只有
@RequestMapping
时
,
无论方法返回值是什么认为不需
要跳转 ,如果返回值满足 key-value 形式
(
对象或
map) ,把响应头设置为 application/json;charset=utf-8 ,转换后的内容输出流的形式响应给客户端. ;如果返回值不满足 key-value,
例如返回值为
String ,会把相应头设置为 text/html ,后把方法返回值以流的形式直接输出. 返回值包含中文,
出现中文乱码。解决方式如下:
@RequestMapping(value="demo12",produces="text/html;charset=utf-8")
6,SpringMVC 中 ModelAndView 类进行传值
@RequestMapping("demo4")
public ModelAndView demo4(){
//参数,跳转视图
ModelAndView mav = new ModelAndView("/index.jsp");
mav.addObject("mav", "mav 的值");
return mav;
}
7,拦截器
第一步:编写拦截器类,实现HandlerInterceptor 接口
public class DemoInterceptor implements HandlerInterceptor {
//在进入控制器之前执行
//如果返回值为 false,阻止进入控制器
//控制代码
@Override
public boolean preHandle(HttpServletRequest arg0,HttpServletResponse arg1, Object arg2)throws Exception{
System.out.println("arg2:"+arg2);
System.out.println("preHandle");
return true;
}
//控制器执行完成,进入到 jsp 之前执行.
//日志记录.
//敏感词语过滤
@Override
public void postHandle(HttpServletRequest arg0,HttpServletResponse arg1, Object arg2,ModelAndView arg3)throws Exception {
System.out.println("往"+arg3.getViewName()+"跳转");
System.out.println("model 的值"+arg3.getModel().get("model"));
String word =arg3.getModel().get("model").toString();
String newWord = word.replace("祖国", "**");
arg3.getModel().put("model", newWord);
// arg3.getModel().put("model", "修改后的内容");
System.out.println("postHandle");
}
//jsp 执行完成后执行
//记录执行过程中出现的异常.
//可以把异常记录到日志中
@Override
public void afterCompletion(HttpServletRequest arg0,HttpServletResponse arg1, Object arg2, Exception arg3)throws Exception {
System.out.println("afterCompletion"+arg3.getMessage());
}
}
第二步:在 springmvc.xml 配置拦截器需要拦截哪些控制器
1)拦截所有控制器
<mvc:interceptors>
<bean class="com.yjb.interceptor.DemoInterceptor"></bean>
</mvc:interceptors>
2)拦截特定的的 url
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/demo"/>
<mvc:mapping path="/demo1"/>
<mvc:mapping path="/demo2"/>
<bean class="com.yjb.interceptor.DemoInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>

浙公网安备 33010602011771号