一、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. http常见的5个错误

    1. HTTP 500错误(内部服务器错误)对对HTTP 500错误的定义已经充分证明了这是一个最常见的HTTP错误. 一般来说,HTTP 500 错误就是web服务器发生内部错误时返回的信息. 例如 ...

  2. PHP之PHP文件引用详解

    HP的文件引用涉及到四个函数: 文件引用 1.include()2.include_once()3.require()4.require_once() 这四个函数常常会给PHP初学者造成困扰,总的来说 ...

  3. Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务

    Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具:Spr ...

  4. 如何编写jQuery插件

    要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...

  5. sdut 2159:Ivan comes again!(第一届山东省省赛原题,STL之set使用)

    Ivan comes again! Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 The Fairy Ivan gave Say ...

  6. An Edge-Guided Image Interpolation Algorithm via Directional Filtering and Data Fusion【翻译】

    基于定向滤波和数据融合的边缘引导图像插值算法 http://ieeexplore.ieee.org/document/1658087/ 摘要: 保留边缘结构对于从低分辨率对应物重建高分辨率图像的图像插 ...

  7. Socket 进行发送

    最灵活的通信方式还是Socket ,TcpClient和Tcplistener只是对Socket进行了一些包装,从而使他们使用起来更简单一些 给出同步的服务器端 static void Main(st ...

  8. Centos查看系统位数方法

    方法一:file /sbin/init 方法二:file /bin/ls 我的显示是32位

  9. Intent讲解

    什么是Intent? Intent是一个消息传递对象,可以使用它来启动其它应用组件.Intent使组件之间通信更加便利,主要用于以下三点: 启动Activity: 可以将intent作为参数调用Con ...

  10. 【IIS】模块 DLL C:\Windows\System32\inetsrv\authcert.dll 未能加载。返回的数据为错误信息。

    解决方案,check  IIS --Client Certificate Mapping Authentication installed?