springmvc是类似于web层的mvc框架
springmvc是spring的一部分
Struts2执行流程:
1.创建一个web程序
需要的jar包
配置web.xml(配置前端控制器DispatcherServelet)
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
springmvc
*.do
配置springmvc.xml
自定义controller
public class MyController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//接收请求 参数 验证参数
//封装参数调用业务方法
//返回视图
ModelAndView mv=new ModelAndView();
//设置页面回显数据
mv.addObject("hello","欢迎学习springmvc");
//指定跳转的视图
//返回物理视图
//mv.setViewName("WEB-INF/jsps/index.jsp");
//返回逻辑视图
mv.setViewName("index");
return mv;
}
}
定义视图页面
根据视图解析出路径:WEB-INF/jsps/index.jsp
${hello}
根据代码分析springmvc执行流程
适配器的源代码
处理映射器
BeanNameUrlHandlerMapping 功能:寻找controller,根据url请求去匹配bean的name的url,从而获取controller
SimpleUrlHandlerMaping
功能:寻找Controller,根据浏览器url匹配简单url的key又根据Controller的id找到Controller
myController
http
http
ControllerClassNameHandlerMapping
功能:寻找controller根据类名.do访问,类名首字母小写
映射器之间共存:3个处理映射器可以共存
SimpleControllerHandlerAdapter
功能:执行controller,调用controller里面的方法,返回modelAndView
HttpRequestHandlerAdapter
执行controller
2个处理适配器可以共存
springmvc通过命令设计模式接收页面参数
自定义命令控制器
public class CommandController extends AbstractCommandController{
//指定把参数封装到那个对象
public CommandController(){
this.setCommandClass(User.class);
}
@Override
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException error)
throws Exception {
//值被封装命令对象
User user=(User) command;
ModelAndView mv=new ModelAndView();
//设置model数据 值可以任意的pojo
mv.addObject("user",user);
//指定返回视图页面
mv.setViewName("index");
return mv;
}
}
定义JavaBean
private Integer id;
private String username;
private String age;
private Date birthday;
private String sex;
private String address;
add.jsp
跳转到add页面
由于add页面在WEB-INF下面不能访问,需要通过controller来访问
public class ToAddController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//跳转到添加页面
ModelAndView mv=new ModelAndView();
mv.setViewName("add");
return mv;
}
}
在springmvc中配置bean
中文乱码解决
get请求乱码
post乱码
Spring编码过滤器:在web.xml配置
characterEncoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncoding
/*
时间类型转换
创建一个web工程
配置web.xml
characterEncoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncoding
/*
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
springmvc
*.do
配置springmvc.xml
自定义controller
注解开发流程
RequestMapping
requestMapping("hello")
requestMapping("/hello.do")
requestMapping(value="/hello.do")
@RequestMapping("/hello.do")
public String hello(){
return "index";
}
requestMapping(value=”/hello.do”,method=RequestMethod.GET)
requestMapping(value=”/hello.do”,method=RequestMethod.GET)
public String hello(){
return "index";
}
requestMapping(value=”/hello.do”,method=RequestMethod.POST)
浏览器直接访问,a标签都是get请求
表单提交(指定post),ajax指定post提交,post提交。
requestMapping(value=”/hello.do”,method={RequestMethod.POST, RequestMethod.GET})
@requestMapping(value=”/hello.do”,method={RequestMethod.POST, RequestMethod.GET})
public String hello(){
return "index";
}
Request的根路径
@RequestMapping(”/user”)
UserController{
requestMapping(“save”)
Save()
requestMapping(“update”)
Update{}
requestMapping(“find”)
Fiind()
}
项目名/user/save.do
@RequestMapping(”/items”)
ItemsController{
requestMapping(“save”)
Save()
requestMapping(“update”)
Update{}
requestMapping(“find”)
Fiind()
}
项目名/items/save.do
封装参数
分析接受参数类型
基本类型,int,String等等基本类型
pojo类型
包装类型
springmvc默认支持类型:HttpSession,HttpRequestServlet,Model..
struts2参数:属于属性封装
springmvc参数封装:基于方法进行封装
基本类型
需求:封装int类型参数
页面传递参数都是字符串
@RequestMapping("recieveInt")
public String recieveInt(Integer id){
System.out.println(id);
return "success";
}
接受字符串类型
//接收string类型参数
@RequestMapping("recieveStr")
public String recieveStr(String username){
System.out.println(username);
return "success";
}
接受数组:分析:批量删除:checkbox复选框。Value必须有值。
@RequestMapping("recieveArray")
public String recieveIds(Integer []ids){
System.out.println(ids);
return "success";
}
接收POJO
@RequestMapping("recieveUser")
public String recieveUser(User user){
System.out.println(user);
return "success";
}
接受包装类型参数
userCustom{
private user user;
private List
private Map
private items items;
}
public class UserCustomer {
private User user;
private List userList;
private Map maps=new HashMap();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List getUserList() {
return userList;
}
public void setUserList(List userList) {
this.userList = userList;
}
public Map getMaps() {
return maps;
}
public void setMaps(Map maps) {
this.maps = maps;
}
}