1.springmvc(注解版本)

注解扫描

<?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"
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-3.0.xsd"> <!--让spring容器去扫描注释-->
<context:component-scan base-package="com.juaner.app14"/> <!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

Action类

@Controller
public 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

<form action="${pageContext.request.contextPath}/emp/deleteAll.action" method="post">
<table>
<tr><th>
编号
</th><th>
姓名
</th></tr> <tr>
<td>
<input type="checkbox" name="id" value="1123">
</td>
<td>哈哈</td>
</tr>
<tr>
<td>
<input type="checkbox" name="id" value="2123">
</td>
<td>嘻嘻</td>
</tr>
<tr>
<td>
<input type="checkbox" name="id" value="3">
</td>
<td>呵呵</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>

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

bean

public class Bean {
private List<Emp> empList = new ArrayList<Emp>();
public Bean(){} public List<Emp> getEmpList() {
return empList;
} public void setEmpList(List<Emp> 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

<form action="${pageContext.request.contextPath}/emp/addAll.action" method="post">
<table>
<tr>
<td><input name="empList[0].username" type="text" value="哈哈"></td>
<td><input name="empList[0].salary" type="text" value="6000"></td>
</tr>
<tr>
<td><input name="empList[1].username" type="text" value="呵呵"></td>
<td><input name="empList[1].salary" type="text" value="7000"></td>
</tr>
<tr>
<td><input name="empList[2].username" type="text" value="嘻嘻"></td>
<td><input name="empList[2].salary" type="text" value="8000"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="注册"></td>
</tr>
</table>
</form>

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转换器

    <!--json转换器-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>

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<Emp> listbean2json() throws Exception{
//创建List对象
List<Emp> empList = new ArrayList<Emp>();
//向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<String,Object> map2json() throws Exception{
//创建List对象
List<Emp> empList = new ArrayList<Emp>();
//向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<String,Object> map = new LinkedHashMap<String,Object>();
//向Map对象中绑定二个变量
map.put("total",empList.size());
map.put("rows",empList);
//返回需要转JSON文本的对象
return map;
}

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. html5,进度条

    <form action="" id="myform">        <progress value="20" max= ...

  2. 【过程改进】 windows下jenkins常见问题填坑

    没有什么高深的东西,1 2天的时间大多数人都能自己摸索出来,这里将自己遇到过的问题分享出来避免其他同学再一次挖坑. 目录 1. 主从节点 2. Nuget自动包还原 3. powershell部署 4 ...

  3. printk函数日志级别的设置【转】

    本文转载自: 下面执行cat /proc/sys/kernel/printk 打印出的四个数字分别代表: 控制台日志级别.默认的消息日志级别.最低的控制台日志级别和默认的控制台日志级别 只有当prin ...

  4. Linux之curl命令详解

    url命令是一个功能强大的网络工具,它能够通过http.ftp等方式下载文件,也能够上传文件.其实curl远不止前面所说的那些功能,大家可以通过man curl阅读手册页获取更多的信息.类似的工具还有 ...

  5. 06-BCD计数器设计与应用——小梅哥FPGA设计思想与验证方法视频教程配套文档

    芯航线--普利斯队长精心奉献   实验目的:1.掌握BCD码的原理.分类以及优缺点          2.设计一个多位的8421码计数器并进行验证          3.学会基本的错误定位以及修改能力 ...

  6. Python Locust性能测试框架实践

    [本文出自天外归云的博客园] Locust的介绍 Locust是一个python的性能测试工具,你可以通过写python脚本的方式来对web接口进行负载测试. Locust的安装 首先你要安装pyth ...

  7. Android自定义View自定义属性

    1.引言 对于自定义属性,大家肯定都不陌生,遵循以下几步,就可以实现: 自定义一个CustomView(extends View )类 编写values/attrs.xml,在其中编写styleabl ...

  8. http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

    The Contoso University sample web application demonstrates how to create ASP.NET MVC 5 applications ...

  9. 在mysql数据库原有字段后增加新内容

    update table set user=concat(user,$user) where xx=xxx; [注释]这个语法要求原来的字段值不能为null(可以为空字符''):

  10. 一步一步学习underscore的封装和扩展方式

    前言 underscore虽然有点过时,这些年要慢慢被Lodash给淘汰或合并. 但通过看它的源码,还是能学到一个库的封装和扩展方式. 第一步,不污染全局环境. ES5中的JS作用域是函数作用域. 函 ...