Springmvc流程(面试题)
1.用户发送请求到前端控制器DispatcherServlet(前端控制器在web.xml中配置).
2.DispatcherServlet收到请求调用HandlerMapping处理器映射器.
3.处理器映射器HandlerMapping 找到具体的处理器(可以根据xml配置、注解进行查找),
生成处理器对象及处理器拦截器一并返回给DispatcherServlet.
4.DispatcherServlet 请求执行调用 HandlerAdapter处理器适配器.
5.HandlerAdapter处理器适配器,执行Handler找到相对应的controller(后端控制器)
6.Controller执行完成返回ModelAndView.
7.HandlerAdapter处理器适配器将controller执行结果ModelAndView返回给前端控制器DispatcherServlet.
8.前端控制器DispatcherServlet将ModelAndView传给ViewReslover视图解析器。
9.ViewReslove视图解析器r解析后返回具体View.
10.DispatcherServlet根据View进行渲染视图.
11.前端控制器DispatcherServlet响应用户。
springMvc的入门案例
1.导包
2.创建项目,建包
3.
步骤1:jar包放到WEB-INF下面
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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!--开启mvc注解扫描驱动 -->
<mvc:annotation-driven/>
<!--扫描controller-->
<context:component-scan base-package="com.ujiuye"/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--后缀-->
<property name="suffix" value=".jsp"/>
<!--前缀-->
<property name="prefix" value="/view/"/>
</bean>
</beans>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 读取springmvc的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 拦截的路径,让请求拦截到springmvc中,如果写成/,代表拦截所有路径,此时会拦截.jpg,.js,.css
不应该拦截静态资源,专门拦截指定的请求路径,所有加一个后缀,.do,.action-->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Controller
@Controller
public class HelloController {
@RequestMapping("/ko.do")
public ModelAndView hello(){
ModelAndView mv=new ModelAndView();
mv.setViewName("ok");
mv.addObject("info","这是我的第一次");
return mv;
}
}
Spring编码过滤器
<!--spring编码过滤器-->
<filter>
<filter-name>encode</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encode</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Form表单数据处理
Servlet
接受
Req.getParament(“”)
Springmvc 接收参数
public class PersonBean {
private int age;
private String username;
private DogBean dog;
private List<DogBean> dogs;
private Map<String,DogBean> map;
private Set<DogBean> dogSet;
public PersonBean(){
//初始化list map Set
dogs=new ArrayList<>();
map =new HashMap<>();
//set必须要指定数据量
dogSet=new HashSet<>();
dogSet.add(new DogBean());
dogSet.add(new DogBean());
}
Contoller:
@RequestMapping("/p2.do")
public String p2(PersonBean p){
System.out.println(p);
return "index";
}
Jsp:
<form action="../p2.do" method="post">
<div>
年龄:<input name="age" value="18"/>
</div>
<div>
用户名:<input name="username" value="张三"/>
</div>
<div>
狗:
狗颜色:<input name="dog.color" value="绿色1"/>
性别:<input name="dog.sex" value="公狗1"/>
</div>
<div>
狗list
狗颜色1:<input name="dogs[0].color" value="绿色list1"/>
性别1:<input name="dogs[0].sex" value="公狗list1"/>
狗颜色2:<input name="dogs[1].color" value="绿色list2"/>
性别2:<input name="dogs[1].sex" value="公狗list2"/>
</div>
<div>
狗map
狗颜色1:<input name="map['m1'].color" value="绿色map1"/>
性别1:<input name="map['m1'].sex" value="公狗map1"/>
狗颜色2:<input name="map['m2'].color" value="绿色map2"/>
性别2:<input name="map['m2'].sex" value="公狗map2"/>
</div>
<div>
狗set
狗颜色1:<input name="dogSet[0].color" value="绿色set1"/>
性别1:<input name="dogSet[0].sex" value="公狗set1"/>
狗颜色2:<input name="dogSet[1].color" value="绿色set2"/>
性别2:<input name="dogSet[1].sex" value="公狗set2"/>
</div>
<div>
<input type="submit" value="提交"/>
</div>
</form>
自己定义的类型转换器(Date)
配置文件的位置
<!--配置日期转换器 注意name的命名不要叫conversionServices,系统中也有一个重名的东西-->
<bean name="conversionServicess" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.ujiuye.convert.DateConvert"/>
</set>
</property>
</bean>
<!--开启mvc注解扫描驱动 -->
<mvc:annotation-driven conversion-service="conversionServicess"/>
public class DateConvert implements Converter<String, Date> {
@Override
public Date convert(String str) {
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
Date date = sf.parse(str);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
静态资源处理问题
<!--第一种方式-->
<!--<mvc:default-servlet-handler/>-->
<!--第一种方式-->
<mvc:default-servlet-handler/>
<!--第二种方式 ,不拦截静态资源-->
<!--<mvc:resources mapping="/img/*" location="/img/"/>
<mvc:resources mapping="/js/*" location="/js/"/>-->
Springmvc跳转(转发重定向):
转发,重定向
不会帮你加前后缀
Forward:路径
Redirect:路径
@Controller
@RequestMapping("/person/")
public class HelloController {
@RequestMapping("for")
public String jump(Model model){
model.addAttribute("info","这是我的转发数据");
return "forward:/view/ok.jsp";
}
@RequestMapping("red")
public String jump2(Model model){
model.addAttribute("info","这是我的转发数据");
return "redirect:/view/ok.jsp";
}
}
默认参数绑定
1.Form-controller
2.Controller 本质上就是servlet的扩展,有关servlet的内容可以直接使用
3.HttpServletRequest ,Model ,ModelMap ,Map
@Controller
@RequestMapping("/jump/")
public class JumpController {
@RequestMapping("p1")
public String p1(HttpServletRequest request, HttpServletResponse response, HttpSession session){
request.setAttribute("info","request");
session.setAttribute("username","张三");
return "ok";
}
@RequestMapping("p2")
public String p2(Model m){
m.addAttribute("info","model穿过来的参数");
return "ok";
}
@RequestMapping("p3")
public String p3(ModelMap m){
m.addAttribute("info","ModelMap穿过来的参数");
return "ok";
}
@RequestMapping("p4")
public String p4(Map m){
m.put("info","Map穿过来的参数");
return "ok";
}
}
返回值处理
1.可以返回modelview
ModelAndView mv=new ModelAndView();
mv.setViewName("ok");
mv.addObject("info","这是我的第一次");
2.String 直接认为你是资源,帮你自动加前后缀
3.Void 比如直接在session中保存数据,不涉及页面的跳转
4.public void saveSession(HttpSession session){
session.setAttribute("username","admin");
}
5.异步返回数据(ajax)
希望后台返回一串json数据
(1):导入jsckson.....jar
(2):加注解@Resposebody代表响应的文本内容,如果返回的对象,自动转成json字符串
@Controller
@RequestMapping("/json/")
public class JsonController {
@RequestMapping("j1")
@ResponseBody
public String jsonTest1(){
return "hello";
}
@RequestMapping("j2")
@ResponseBody
public DogBean jsonTest2(){
DogBean dog=new DogBean();
dog.setColor("红色");
dog.setSex("男");
return dog;
}}
@RestController注解放在类名上,会将该类中的所有的方法上面统一加上@ResponseBody