一、SpringMVC支持在控制器的业务方法中写入参数作为传递过来的变量

@Controller
@RequestMapping(value="/kaiye")
public class HelloHAE{ @RequestMapping(value="/hello.action")
public String hello(Model model,String name,double number){
model.addAttribute("Message",name+"|"+String.valueOf(number));
return "success";
}
}

二、SpringMVC支持限定某个业务控制方法,只允许GET或POST请求方式访问

@Controller
@RequestMapping(value="/kaiye")
public class HelloHAE{ @RequestMapping(method={RequestMethod.POST,RequestMethod.GET},value="/hello.action")
public String hello(Model model){
model.addAttribute("Message","hello");
return "success";
} @RequestMapping(method=RequestMethod.GET,value="/goodbye.action")
public String goodbye(Model model){
model.addAttribute("Message","goodbye");
return "success";
}
}

三、SpringMVC支持在业务控制方法中写入Request,Response等传统web参数

package com.jyk.springmvc.httpRequestResponse;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.support.RequestContext; /**
* SpringMVC支持web传统方式httpRequest和httpResponse获取参数(建议用model)
* 也支持void类型的业务方法
* @author fat
*/
@Controller
@RequestMapping(value="/kaiye")
public class HelloHAE{ @RequestMapping(method={RequestMethod.POST,RequestMethod.GET},value="/hello.action")
public void hello(HttpServletRequest request,HttpServletResponse response) throws IOException{
String name = request.getParameter("name");
String number = request.getParameter("number");
System.out.println(name+":"+number); //绑定到session域中
request.getSession().setAttribute("name",name);
request.getSession().setAttribute("number",number); //页面重定向
response.sendRedirect(request.getContextPath()+"/success.jsp");
}
}

四、在默认情况下,springmvc不能将String类型转成java.util.Date类型,需要使用@InitBind来解决字符串转日期类型,同时支持在参数中传递多个实体类,如下代码中Bean对象包含了Person和Animal对象

/**
* 日期格式转换,和多个实体类型接收参数
* @author fat
*/
@Controller
@RequestMapping(value="/kaiye")
public class HelloHAE{ /**
* 自定义类型转换器
* @param request
* @param binder
* @throws Exception
*/
@InitBinder
protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder)
throws Exception{
binder.registerCustomEditor(Date.class, new CustomDateEditor
(new SimpleDateFormat("yyyy-MM-dd"), true));
} @RequestMapping(value="/hello.action")
public String hello(Model model,Person person){
System.out.println(person.toString());
model.addAttribute("person",person);
return "success";
} /**
* 参数中传多个实体类时
* @param model
* @param person
* @return
*/
@RequestMapping(value="/entities.action")
public String entitiesTest(Model model,Bean bean){
System.out.println(bean.getPerson());
System.out.println(bean.getAnimal());
model.addAttribute("person",bean.getPerson());
model.addAttribute("animal",bean.getAnimal());
return "success";
}
}

五、SpringMVC支持在业务控制方法中收集数组参数

<form action="${pageContext.request.contextPath}/kaiye/array.action" method="post">
<table border="1" align="center">
<tr>
<th>名字</th>
<td><input type="checkbox" name="ids" value="1"/></td>
</tr>
<tr>
<th>学号</th>
<td><input type="checkbox" name="ids" value="2"/></td>
</tr>
<tr>
<th>日期</th>
<td><input type="checkbox" name="ids" value="3"/></td>
</tr>
<tr>
<td>
<input type="submit" value="009测试"/>
</td>
</tr>
</table>
</form>
@Controller
@RequestMapping(value="/kaiye")
public class HelloHAE{ @RequestMapping(value="/array.action")
public String hello(Model model,int ids[]){
for(int i:ids){
System.out.println(i);
}
return "success";
}
}

六、SpringMVC支持在业务控制方法中收集List<JavaBean>参数

<form action="${pageContext.request.contextPath}/kaiye/beanList.action" method="post">
<table border="1" align="center">
<tr>
<td><input type="checkbox" name="persons[0].name" value="名字1"/></td>
<td><input type="checkbox" name="persons[0].number" value="编号1"/></td>
</tr>
<tr>
<td><input type="checkbox" name="persons[1].name" value="名字2"/></td>
<td><input type="checkbox" name="persons[1].number" value="编号2"/></td>
</tr>
<tr>
<td>
<input type="submit" value="010测试"/>
</td>
</tr>
</table>
</form>
public class Bean {
private List<Person> persons = new ArrayList<Person>(); public List<Person> getPersons() {
return persons;
} public void setPersons(List<Person> persons) {
this.persons = persons;
}
}
@Controller
@RequestMapping(value="/kaiye")
public class HelloHAE{ @RequestMapping(value="/beanList.action")
public String beanList(Model model,Bean bean){
System.out.println(bean.getPersons());
return "success";
}
}

七、SpringMVC支持结果的转发和重定向,在转发情况下,共享request域对象,会将参数从第一个业务控制方法传入第二个业务控制方法

/**
* 重定向共享参数
* @author fat
*/
@Controller
@RequestMapping(value="/kaiye")
public class HelloHAE{ @RequestMapping(value="/hello.action")
public String hello(Model model,String name){
System.out.println("hello "+name);
return "forward:/kaiye/goodbye.action";
} @RequestMapping(value="/goodbye.action")
public String goodbye(Model model,String name){
System.out.println("goodbye "+name);
return "success";
}
}

八、SpringMVC支持异步发送表单数据到JavaBean,并响应JSON文本返回

<h3>013测试</h3>
<input type="button" value="person转json">
<input type="button" value="persons转json">
<input type="button" value="map转json">
<script type="text/javascript">
$(":button:first").click(function(){
var url = "${pageContext.request.contextPath}/kaiye/hello.action";
var sendData = null;
$.post(url,sendData,function(backData,textStatu,ajax){
alert(ajax.responseText);
})
});
$(":button:eq(1)").click(function(){
var url = "${pageContext.request.contextPath}/kaiye/hellos.action";
var sendData = null;
$.post(url,sendData,function(backData,textStatu,ajax){
alert(ajax.responseText);
})
});
$(":button:eq(2)").click(function(){
var url = "${pageContext.request.contextPath}/kaiye/hellomap.action";
var sendData = null;
$.post(url,sendData,function(backData,textStatu,ajax){
alert(ajax.responseText);
})
});
</script>
/**
* 将实体对象以Json的格式返回
* @author fat
*/
@Controller
@RequestMapping(value="/kaiye")
public class HelloHAE{ @RequestMapping(value="/hello")
public @ResponseBody Person hello(){
return new Person("kaiye","呵呵","12");
} @RequestMapping(value="/hellos")
public @ResponseBody List<Person> hellos(){
List<Person> persons = new ArrayList<Person>();
persons.add(new Person("狗","1","33"));
persons.add(new Person("猫","2","44"));
return persons;
} @RequestMapping(value="/hellomap.action")
public @ResponseBody Map<String,Object> hellomap(){
Map<String,Object> map = new HashMap<String,Object>();
map.put("age",1);
map.put("name","你好");
return map;
}
}

SpringMVC的其他功能使用的更多相关文章

  1. Intellij IDEA +MAVEN+Jetty实现SpringMVC简单查询功能

    利用 Intellij IDEA +MAVEN+Jetty实现SpringMVC读取数据库数据并显示在页面上的简单功能 1 新建maven项目,配置pom.xml <project xmlns= ...

  2. 解决springmvc中文件下载功能中使用javax.servlet.ServletOutputStream out = response.getOutputStream();后运行出异常但结果正确的问题

    问题描述: 在springmvc中实现文件下载功能一般都会使用javax.servlet.ServletOutputStream out = response.getOutputStream();封装 ...

  3. springMVC中一些功能

    1.controller的生命周期 spring框架默认为单例模式,会使数据之间的传递互相影响,而springMVC给我们提供了request与session两个,request每次请求就会产生一个单 ...

  4. SpringMVC实现查询功能

    1 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...

  5. SpringMVC记住密码功能

    CookieTool (Cookie帮助类): package com.utcsoft.common.cookie; import java.util.HashMap; import java.uti ...

  6. SpringMVC操作指南-登录功能与请求过滤

    [1] Source http://code.taobao.org/p/LearningJavaEE/src/LearningSpringMVC005%20-%20Login%20and%20Filt ...

  7. SpringMVC的删除功能

    Dao层 package net.roseindia.dao; import java.util.Date; import java.util.List; import net.roseindia.m ...

  8. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)【转】

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  9. spring+springmvc+mybatis xml配置文件

    一.jdbc.properties 文件: driver=com.mysql.jdbc.Driverurl=jdbc:mysql://192.168.31.xxx:3306/abc?useUnicod ...

随机推荐

  1. Vim快捷键操作命令大全

        Vim是一个超牛的编辑器,命令功能十分强大 .而且这些命令大都可以进行组合 ,比如,9yy命令表示复制9行内容,9表示要复制的行数,同样100dd表示删除100行,当数字和命令合作的时候,就比 ...

  2. datagrid鼠标悬浮提示

    /** * 扩展两个方法 */ $.extend($.fn.datagrid.methods, { /** * 开打提示功能 * @param {} jq * @param {} params 提示消 ...

  3. Bitcoin: A Peer-to-Peer Electronic Cash System(比特币论文翻译)

    比特币历史: 2008年,比特币论文诞生 2009年1月,第一批比特币诞生 2011年4月,比特币价格第一次达到了1美元 2011年6月,涨到30美元,然后开始跌 2013年1月,4美元 2013年1 ...

  4. opencv 摄像头人脸检测

    PYTHON ubuntu16.04 默认安装的Python版本2.7.12,当用pip install opencv-python 安装了opencv for python 3.3.0.10后,运行 ...

  5. 【BZOJ】1629: [Usaco2007 Demo]Cow Acrobats(贪心+排序)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1629 这题我想了很久都没想出来啊... 其实任意两头相邻的牛交换顺序对其它牛是没有影响的.. 那么我 ...

  6. YII2与Thinkphp整合阿里云OSS

    前言: 如果上传的文件都和网站程序源代码放在一起:那是有相当多的弊端的: 1:静态文件会占用大量带宽: 2:服务器的成本略高: 常规的做法是把php源代码放到一台服务器上:图片等静态文件放在另一台服务 ...

  7. 显示图片中CDC和HDC问题

    (m_Pic.LoadPictureData(pBuffer, nSize));//接作调用函数读pBuffer的jpg数据准备显示 showimage();//显示图片 void Caccess_t ...

  8. python3----输出所有大小写字母及数字

    1. 用一行输出所有大(小)写字母,以及数字 print([chr(i) for i in range(65, 91)]) # 所有大写字母 print([chr(i) for i in range( ...

  9. iOS开发之--png图片编译时报错 (Command /Applications/Xcode.app/Contents/Developer/usr/bin/copypng failed with exit code 1 )

    编译或者运行APP的时候,老是报这个错误:Command /Applications/Xcode.app/Contents/Developer/usr/bin/copypng failed with ...

  10. C语言函数的概念

    在<我们对函数进行了简单的解释,函数(Function)是一段可以重复使用的代码,这是从整体上对函数的认识. C语言本身带了很多库函数,并分门别类地放在了不同的头文件中,使用时只要引入对应的头文 ...