SpringMVC的其他功能使用
一、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的其他功能使用的更多相关文章
- Intellij IDEA +MAVEN+Jetty实现SpringMVC简单查询功能
利用 Intellij IDEA +MAVEN+Jetty实现SpringMVC读取数据库数据并显示在页面上的简单功能 1 新建maven项目,配置pom.xml <project xmlns= ...
- 解决springmvc中文件下载功能中使用javax.servlet.ServletOutputStream out = response.getOutputStream();后运行出异常但结果正确的问题
问题描述: 在springmvc中实现文件下载功能一般都会使用javax.servlet.ServletOutputStream out = response.getOutputStream();封装 ...
- springMVC中一些功能
1.controller的生命周期 spring框架默认为单例模式,会使数据之间的传递互相影响,而springMVC给我们提供了request与session两个,request每次请求就会产生一个单 ...
- SpringMVC实现查询功能
1 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...
- SpringMVC记住密码功能
CookieTool (Cookie帮助类): package com.utcsoft.common.cookie; import java.util.HashMap; import java.uti ...
- SpringMVC操作指南-登录功能与请求过滤
[1] Source http://code.taobao.org/p/LearningJavaEE/src/LearningSpringMVC005%20-%20Login%20and%20Filt ...
- SpringMVC的删除功能
Dao层 package net.roseindia.dao; import java.util.Date; import java.util.List; import net.roseindia.m ...
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)【转】
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- spring+springmvc+mybatis xml配置文件
一.jdbc.properties 文件: driver=com.mysql.jdbc.Driverurl=jdbc:mysql://192.168.31.xxx:3306/abc?useUnicod ...
随机推荐
- EasyUI DataGrid合并单元
<table id="tt"></table> $('#tt').datagrid({ title:'Merge Cells', iconC ...
- easyui添加删除tooltip
/** * 扩展两个方法 */$.extend($.fn.datagrid.methods, { /** * 开打提示功能 * @param {} jq * @param {} params 提示消息 ...
- Linux 复制、移动覆盖文件不提示
# vi ~/.bashrc 如果你看到如下内容,以下命令都会用别名执行了,就是说自动加了 -i 参数 alias rm='rm -i'alias cp='cp -i'alias mv='mv - ...
- 关于Unity中的新手编码技巧
写代码遇到报错,问题怎么办?怎么查看unity代码的接口?函数参数不记得了怎么办? 解决方法: 1.选择不懂的函数或类,按F12,跳转到代码的定义,自己去看就可知道了. 2.有的时候,选择一个函数,按 ...
- 下列JSP代码:
下列JSP代码: <html> <body> <% for(int i = 0; i < 10; i++) { //1 } %> </body> ...
- js调绝对定位的top
$("ggg div").each(function () { this.style.top = (parseFloat(this.style.top ...
- ubuntu 16.04安装 navicat
原文地址:http://www.cnblogs.com/wbJson/p/5655537.html 下载地址:http://download2.navicat.com/download/navicat ...
- zara
[1]ZARA是西班牙Inditex集团旗下的一个子公司,它既是服装品牌,也是专营ZARA品牌服装的连锁零售品牌.1975年设立于西班牙的ZARA,隶属于Inditex集团,为全球排名第三.西班牙排名 ...
- 【Openwrt】刷
设定你的电脑ip 为192.168.1.100 网线一头连接lan口,另外一头连接电脑.WAN口不能插线. 按住路由器的qss 键,开启路由器的电,灯灭掉,等6秒左右灯会再次闪几下就松开,用googl ...
- switch语句相关
Cannot switch on a value of type long. Only convertible int values, strings or enum variables are pe ...