博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC进阶
阅读量:6570 次
发布时间:2019-06-24

本文共 7601 字,大约阅读时间需要 25 分钟。

1.springmvc(注解版本)

注解扫描

Action类

@Controllerpublic class HelloAction {    public HelloAction(){        System.out.println(this .hashCode() );    }    @RequestMapping(value = "/hello.action")    public String hello(Model model)throws Exception{        System.out.println("hello");        model.addAttribute("message","this is the first....");        return "success";    }    @RequestMapping(value = "/bye.action")    public String bye(Model model)throws Exception{        System.out.println("bye");        model.addAttribute("message","bye");        return "success";    }}

2.一个Action中,写多个类似的业务控制方法

@Controller@RequestMapping(value="/user")public class UserAction {    @RequestMapping(value = "/register")    public String register(Model model)throws Exception{        model.addAttribute("message","注册成功");        return "/jsp/success.jsp";    }    @RequestMapping(value = "/login")    public String login(Model model)throws Exception{        model.addAttribute("message","登录成功");        return "/jsp/success.jsp";    }}

3.在业务控制方法中写入普通变量收集参数,限定某个业务控制方法,只允许GET或POST请求方式访问

@Controller@RequestMapping(value="/user")public class UserAction {    @RequestMapping(method= RequestMethod.POST,value = "/register")    public String register(Model model,String username,double salary)throws Exception{        System.out.println(username+salary);        model.addAttribute("message","注册成功");        return "/jsp/success.jsp";    }    @RequestMapping(value = "/login", method= {RequestMethod.POST,RequestMethod.GET})    public String login(Model model,String username,double salary)throws Exception{        System.out.println(username+salary);        model.addAttribute("message","登录成功");        return "/jsp/success.jsp";    }}

4.在业务控制方法中写入HttpServletRequest,HttpServletResponse,Model等传统web参数

@Controller@RequestMapping(value="/user")public class UserAction {    @RequestMapping(method= RequestMethod.POST,value = "/register" )    public void  register(HttpServletRequest request, HttpServletResponse response)throws Exception{        String username = request.getParameter("username");        String salary = request.getParameter("salary");        System.out.println(username+salary);        request.setAttribute("message","转发参数");//        response.sendRedirect(request.getContextPath()+"/jsp/success.jsp");        request.getRequestDispatcher("/jsp/success.jsp").forward(request,response);    }}

5.在业务控制方法中写入模型变量收集参数,且使用@InitBind来解决字符串转日期类型

  jsp中的元素name属性不用加user.前缀

@Controller@RequestMapping(value="/user")public class UserAction {    @InitBinder    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {        //向springmvc中注册一个自定义的类型转换器        //参数一:将string转成什么类型的字节码        //参数二:自定义转换规则        binder.registerCustomEditor(Date.class,                new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));    }    @RequestMapping(method= RequestMethod.POST,value = "/register" )    public String   register(User user, Model model)throws Exception{        System.out.println("用户注册");        model.addAttribute("user",user);        System.out.println(new Timestamp(user.getHiredate().getTime()));        return "/jsp/success.jsp";    }}

  在业务控制方法中写入User,Admin多个模型收集参数,需要更大的模型包装User,Admin

6.在业务控制方法中收集数组参数

@Controller@RequestMapping(value = "/emp")public class EmpAction {    @RequestMapping(value = "/deleteAll",method = RequestMethod.POST)    public String deleteAll(Model model,int[] id)throws Exception{        for(int i:id)        {            System.out.println(i);        }        model.addAttribute("message","批量删除员工成功");        return "/jsp/success.jsp";    }}

jsp

编号 姓名
哈哈
嘻嘻
呵呵

7.在业务控制方法中收集List<JavaBean>参数

bean

public class Bean {    private List
empList = new ArrayList
(); public Bean(){} public List
getEmpList() { return empList; } public void setEmpList(List
empList) { this.empList = empList; }}

action

@Controller@RequestMapping(value = "/emp")public class EmpAction {    @RequestMapping(value = "/addAll",method = RequestMethod.POST)    public String addAll(Model model,Bean bean)throws Exception{        for(Emp emp:bean.getEmpList())        {            System.out.println(emp);        }        model.addAttribute("message","批量添加员工成功");        return "/jsp/success.jsp";    }}

jsp

8.结果的转发和重定向

@Controller@RequestMapping(value="/emp")                public class EmpAction {        @RequestMapping(value="/find")    public String findEmpById(int id,Model model) throws Exception{        System.out.println("查询"+id+"号员工信息");                //转发到EmpAction的另一个方法中去,即再次发送请求        return "forward:/emp/update.action";                //重定向到EmpAction的另一个方法中去,即再次发送请求        //return "redirect:/emp/update.action?id=" + id;    }    @RequestMapping(value="/update")    public String updateEmpById(int id,Model model) throws Exception{        System.out.println("更新" + id +"号员工信息");        model.addAttribute("message","更新员工信息成功");        return "/jsp/ok.jsp";    }    }

 

9.异步发送表单数据到JavaBean,并响应JSON文本返回

1.导入包jackson-core-asl-1.9.11、jackson-mapper-asl-1.9.11

2.配置json转换器

3.方法名中加注解

/**     * @ResponseBody Emp 表示让springmvc将Emp对象转成json文本     */    @RequestMapping(value="/bean2json")    public @ResponseBody Emp bean2json() throws Exception{        //创建Emp对象        Emp emp = new Emp();        emp.setId(1);        emp.setUsername("哈哈");        emp.setSalary(7000D);        emp.setHiredate(new Date());        return emp;    }            @RequestMapping(value="/listbean2json")    public @ResponseBody List
listbean2json() throws Exception{ //创建List对象 List
empList = new ArrayList
(); //向List对象中添加三个Emp对象 empList.add(new Emp(1,"哈哈",7000D,new Date())); empList.add(new Emp(2,"呵呵",8000D,new Date())); empList.add(new Emp(3,"嘻嘻",9000D,new Date())); //返回需要转JSON文本的对象 return empList; } @RequestMapping(value="/map2json") public @ResponseBody Map
map2json() throws Exception{ //创建List对象 List
empList = new ArrayList
(); //向List对象中添加三个Emp对象 empList.add(new Emp(1,"哈哈",7000D,new Date())); empList.add(new Emp(2,"呵呵",8000D,new Date())); empList.add(new Emp(3,"嘻嘻",9000D,new Date())); //创建Map对象 Map
map = new LinkedHashMap
(); //向Map对象中绑定二个变量 map.put("total",empList.size()); map.put("rows",empList); //返回需要转JSON文本的对象 return map; }

 

转载于:https://www.cnblogs.com/juaner767/p/5746982.html

你可能感兴趣的文章
phalcon遇到的那些坑
查看>>
Java类的连接与初始化 (及2013阿里初始化笔试题解析)
查看>>
unity优化笔记
查看>>
linux
查看>>
JavaScript改变 HTML 内容
查看>>
IPv6过渡技术
查看>>
内核调度进程的机制
查看>>
python-68:BS4获取多个标签的文本
查看>>
OSPF中stub area配置实例
查看>>
c primer plus 5 读书笔记1
查看>>
YY的GCD
查看>>
AGC029 E: Wandering TKHS
查看>>
iphone-common-codes-ccteam源代码 CCRadix.m
查看>>
百度分页样式
查看>>
变量声明和定义的区别
查看>>
卖了5个月水果之后再看互联网思维
查看>>
国内maven库镜像(阿里云)
查看>>
SNMP AGENT函数介绍
查看>>
Git提交到多个远程仓库(多看两个文档)
查看>>
html5和html的区别是什么(精问)
查看>>