1.springmvc(注解版本)

注解扫描

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7.  
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  10.  
  11. <!--让spring容器去扫描注释-->
  12. <context:component-scan base-package="com.juaner.app14"/>
  13.  
  14. <!--视图解析器-->
  15. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  16. <property name="prefix" value="/jsp/"/>
  17. <property name="suffix" value=".jsp"/>
  18. </bean>
  19. </beans>

Action类

  1. @Controller
  2. public class HelloAction {
  3. public HelloAction(){
  4. System.out.println(this .hashCode() );
  5. }
  6. @RequestMapping(value = "/hello.action")
  7. public String hello(Model model)throws Exception{
  8. System.out.println("hello");
  9. model.addAttribute("message","this is the first....");
  10. return "success";
  11. }
  12.  
  13. @RequestMapping(value = "/bye.action")
  14. public String bye(Model model)throws Exception{
  15. System.out.println("bye");
  16. model.addAttribute("message","bye");
  17. return "success";
  18. }
  19. }

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

  1. @Controller
  2. @RequestMapping(value="/user")
  3. public class UserAction {
  4.  
  5. @RequestMapping(value = "/register")
  6. public String register(Model model)throws Exception{
  7. model.addAttribute("message","注册成功");
  8. return "/jsp/success.jsp";
  9. }
  10. @RequestMapping(value = "/login")
  11. public String login(Model model)throws Exception{
  12. model.addAttribute("message","登录成功");
  13. return "/jsp/success.jsp";
  14. }
  15. }

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

  1. @Controller
  2. @RequestMapping(value="/user")
  3. public class UserAction {
  4.  
  5. @RequestMapping(method= RequestMethod.POST,value = "/register")
  6. public String register(Model model,String username,double salary)throws Exception{
  7. System.out.println(username+salary);
  8. model.addAttribute("message","注册成功");
  9. return "/jsp/success.jsp";
  10. }
  11. @RequestMapping(value = "/login", method= {RequestMethod.POST,RequestMethod.GET})
  12. public String login(Model model,String username,double salary)throws Exception{
  13. System.out.println(username+salary);
  14. model.addAttribute("message","登录成功");
  15. return "/jsp/success.jsp";
  16. }
  17. }

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

  1. @Controller
  2. @RequestMapping(value="/user")
  3. public class UserAction {
  4.  
  5. @RequestMapping(method= RequestMethod.POST,value = "/register" )
  6. public void register(HttpServletRequest request, HttpServletResponse response)throws Exception{
  7. String username = request.getParameter("username");
  8. String salary = request.getParameter("salary");
  9.  
  10. System.out.println(username+salary);
  11. request.setAttribute("message","转发参数");
  12. // response.sendRedirect(request.getContextPath()+"/jsp/success.jsp");
  13. request.getRequestDispatcher("/jsp/success.jsp").forward(request,response);
  14. }
  15. }

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

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

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

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

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

  1. @Controller
  2. @RequestMapping(value = "/emp")
  3. public class EmpAction {
  4. @RequestMapping(value = "/deleteAll",method = RequestMethod.POST)
  5. public String deleteAll(Model model,int[] id)throws Exception{
  6. for(int i:id)
  7. {
  8. System.out.println(i);
  9. }
  10. model.addAttribute("message","批量删除员工成功");
  11. return "/jsp/success.jsp";
  12. }
  13. }

jsp

  1. <form action="${pageContext.request.contextPath}/emp/deleteAll.action" method="post">
  2. <table>
  3. <tr><th>
  4. 编号
  5. </th><th>
  6. 姓名
  7. </th></tr>
  8.  
  9. <tr>
  10. <td>
  11. <input type="checkbox" name="id" value="1123">
  12. </td>
  13. <td>哈哈</td>
  14. </tr>
  15. <tr>
  16. <td>
  17. <input type="checkbox" name="id" value="2123">
  18. </td>
  19. <td>嘻嘻</td>
  20. </tr>
  21. <tr>
  22. <td>
  23. <input type="checkbox" name="id" value="3">
  24. </td>
  25. <td>呵呵</td>
  26. </tr>
  27. <tr>
  28. <td colspan="2">
  29. <input type="submit" value="提交">
  30. </td>
  31. </tr>
  32. </table>
  33. </form>

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

bean

  1. public class Bean {
  2. private List<Emp> empList = new ArrayList<Emp>();
  3. public Bean(){}
  4.  
  5. public List<Emp> getEmpList() {
  6. return empList;
  7. }
  8.  
  9. public void setEmpList(List<Emp> empList) {
  10. this.empList = empList;
  11. }
  12. }

action

  1. @Controller
  2. @RequestMapping(value = "/emp")
  3. public class EmpAction {
  4. @RequestMapping(value = "/addAll",method = RequestMethod.POST)
  5. public String addAll(Model model,Bean bean)throws Exception{
  6. for(Emp emp:bean.getEmpList())
  7. {
  8. System.out.println(emp);
  9. }
  10. model.addAttribute("message","批量添加员工成功");
  11. return "/jsp/success.jsp";
  12. }
  13. }

jsp

  1. <form action="${pageContext.request.contextPath}/emp/addAll.action" method="post">
  2. <table>
  3. <tr>
  4. <td><input name="empList[0].username" type="text" value="哈哈"></td>
  5. <td><input name="empList[0].salary" type="text" value="6000"></td>
  6. </tr>
  7. <tr>
  8. <td><input name="empList[1].username" type="text" value="呵呵"></td>
  9. <td><input name="empList[1].salary" type="text" value="7000"></td>
  10. </tr>
  11. <tr>
  12. <td><input name="empList[2].username" type="text" value="嘻嘻"></td>
  13. <td><input name="empList[2].salary" type="text" value="8000"></td>
  14. </tr>
  15. <tr>
  16. <td colspan="2" align="center"><input type="submit" value="注册"></td>
  17. </tr>
  18. </table>
  19. </form>

8.结果的转发和重定向

  1. @Controller
  2. @RequestMapping(value="/emp")
  3. public class EmpAction {
  4.  
  5. @RequestMapping(value="/find")
  6. public String findEmpById(int id,Model model) throws Exception{
  7. System.out.println("查询"+id+"号员工信息");
  8.  
  9. //转发到EmpAction的另一个方法中去,即再次发送请求
  10. return "forward:/emp/update.action";
  11.  
  12. //重定向到EmpAction的另一个方法中去,即再次发送请求
  13. //return "redirect:/emp/update.action?id=" + id;
  14.  
  15. }
  16.  
  17. @RequestMapping(value="/update")
  18. public String updateEmpById(int id,Model model) throws Exception{
  19. System.out.println("更新" + id +"号员工信息");
  20. model.addAttribute("message","更新员工信息成功");
  21. return "/jsp/ok.jsp";
  22. }
  23.  
  24. }

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

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

2.配置json转换器

  1. <!--json转换器-->
  2. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  3. <property name="messageConverters">
  4. <list>
  5. <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
  6. </list>
  7. </property>
  8. </bean>

3.方法名中加注解

  1. /**
  2. * @ResponseBody Emp 表示让springmvc将Emp对象转成json文本
  3. */
  4. @RequestMapping(value="/bean2json")
  5. public @ResponseBody Emp bean2json() throws Exception{
  6. //创建Emp对象
  7. Emp emp = new Emp();
  8. emp.setId(1);
  9. emp.setUsername("哈哈");
  10. emp.setSalary(7000D);
  11. emp.setHiredate(new Date());
  12. return emp;
  13. }
  14.  
  15. @RequestMapping(value="/listbean2json")
  16. public @ResponseBody List<Emp> listbean2json() throws Exception{
  17. //创建List对象
  18. List<Emp> empList = new ArrayList<Emp>();
  19. //向List对象中添加三个Emp对象
  20. empList.add(new Emp(1,"哈哈",7000D,new Date()));
  21. empList.add(new Emp(2,"呵呵",8000D,new Date()));
  22. empList.add(new Emp(3,"嘻嘻",9000D,new Date()));
  23. //返回需要转JSON文本的对象
  24. return empList;
  25. }
  26.  
  27. @RequestMapping(value="/map2json")
  28. public @ResponseBody Map<String,Object> map2json() throws Exception{
  29. //创建List对象
  30. List<Emp> empList = new ArrayList<Emp>();
  31. //向List对象中添加三个Emp对象
  32. empList.add(new Emp(1,"哈哈",7000D,new Date()));
  33. empList.add(new Emp(2,"呵呵",8000D,new Date()));
  34. empList.add(new Emp(3,"嘻嘻",9000D,new Date()));
  35. //创建Map对象
  36. Map<String,Object> map = new LinkedHashMap<String,Object>();
  37. //向Map对象中绑定二个变量
  38. map.put("total",empList.size());
  39. map.put("rows",empList);
  40. //返回需要转JSON文本的对象
  41. return map;
  42. }

SpringMVC进阶的更多相关文章

  1. SpringMVC进阶(二)

    一.高级参数绑定 1.1. 绑定数组 Controller方法中可以用String[]接收,或者pojo的String[]属性接收.两种方式任选其一即可. /** * 包装类型 绑定数组类型,可以使用 ...

  2. SpringMVC 进阶

    请求限制 一些情况下我们可能需要对请求进行限制,比如仅允许POST,GET等... RequestMapping注解中提供了多个参数用于添加请求的限制条件 value 请求地址 path 请求地址 m ...

  3. SpringMVC 进阶版

    请求限制 一些情况下我们可能需要对请求进行限制,比如仅允许POST,GET等... RequestMapping注解中提供了多个参数用于添加请求的限制条件 value 请求地址 path 请求地址 m ...

  4. springmvc进阶(5):mvc:default-servlet-handler详解

    我们在配置dispatchServlet时配置<url-pattern>/</url-pattern>拦截所有请求,这时候dispatchServlet完全取代了default ...

  5. Spring注解开发系列VIII --- SpringMVC

    SpringMVC是三层架构中的控制层部分,有过JavaWEB开发经验的同学一定很熟悉它的使用了.这边有我之前整理的SpringMVC相关的链接: 1.SpringMVC入门 2.SpringMVC进 ...

  6. JavaEE目录

    第一章: Spring介绍 Spring项目搭建 Spring概念 第二章: Sprin配置详解 属性注入(构造方法注入,设值注入) 实例化(构造器(空参构造器),静态工厂,工厂方法) 装配(xml方 ...

  7. Spring+SpringMVC+MyBatis整合进阶篇(四)RESTful实战(前端代码修改)

    前言 前文<RESTful API实战笔记(接口设计及Java后端实现)>中介绍了RESTful中后端开发的实现,主要是接口地址修改和返回数据的格式及规范的修改,本文则简单介绍一下,RES ...

  8. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十一)redis密码设置、安全设置

    警惕 前一篇文章<Spring+SpringMVC+MyBatis+easyUI整合进阶篇(九)Linux下安装redis及redis的常用命令和操作>主要是一个简单的介绍,针对redis ...

  9. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十五)阶段总结

    作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 一 每个阶段在结尾时都会有一个阶段总结,在<SSM整合基础篇& ...

随机推荐

  1. AJAX-----02远古时期的ajax

    其实也可以利用创建元素然后用添加属性的方法进行请求后端的

  2. 自定义属性 view

    首先自定义一个圆,相信以前的学习大家都会画圆,在values下写一些自定义的属性 package com.exaple.day01rikao; import android.content.Conte ...

  3. vs2005水晶报表无法运行在X64机器上

    要下载补丁:CRRedist2005_X64.msi http://download.csdn.net/download/gcy007/7106933

  4. CI 框架访问 http://[::1]/yourproject/

    Chances are you have left the base url blank/* |---------------------------------------------------- ...

  5. .sh脚本判断判断某一变量是否为某一数值

    .sh脚本中,判断某一变量(例如:OEM_CUSTOMER_SUPPORT)是否为某一数值(例如:0),并根据条件做不同处理,写法如下: if [ $OEM_CUSTOMER_SUPPORT -eq  ...

  6. 安装ntp服务,并设置ntp客户端

    1.yum安装ntp [root@localhost ~]# yum install ntp 2.修改配置文件 配置文件在/etc/ntp.conf

  7. logstash安装与基础用法

    若是搭建elk,建议先安装好elasticsearch 来自官网,版本为2.3 wget -c https://download.elastic.co/logstash/logstash/packag ...

  8. 解决Cannot find MySQL header files under /usr/include/mysql的错误

    按照下面的步骤能成功,亲测.转帖,做笔记 编译php-5.5-6的mysql支持,出现Cannot find MySQL header files under /usr/include/mysql. ...

  9. Scrum Meeting 7-20151209

    任务安排 姓名 今日任务 明日任务 困难 董元财 服务器购买记录接口 请假(编译攻坚) 无 胡亚坤 发布记录和购买记录 请假(编译攻坚) 无 刘猛 完成Scrum Meeting 请假(编译攻坚) 无 ...

  10. Debian 上面五分钟搭建 WordPress - 博客/网站平台

    没有废话,步骤如下: 下载安装软件,MySQL Apache PHP sudo aptitude install mysql-server mysql-client ##安装 MySQLsudo ap ...