spring学习controller向视图传递参数1
来源:互联网 发布:淘宝搜索宝贝显示地址 编辑:程序博客网 时间:2024/05/01 12:15
1,返回页面视图
<!--JSP视图的视图解析器 基于URL-->
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"p:prefix="../view/"
p:suffix=".html" >
<property name="order" value="1" />
</bean> -->
在配置文件中配置改设置,
@Controller
@RequestMapping("/mvc")
public class MvcController {
/*@RequestMapping("/hello")
public String hello(){
System.out.println("+++++++++++++++++++++hello");
return "hello";
}*/
@RequestMapping("/index")
public String indes(){
System.out.println("+++++++++++++++++++++index");
return "index";
}
indes方法就会返回到url http://localhost:8080/TestSpring/view/idnex.html,
2,返回简单数据视图
@RequestMapping(value="/getPerson",method=RequestMethod.POST)
@ResponseBody
public String put(String username,String password){
Map<String,Object> map=new HashMap<String,Object>();
Person person=new Person();
person.setUsername(username);
person.setPassword(password);
map.put("p", person);
//model.addAttribute("person", person);
return "hello";
}
这样在页面就可获得返回的值 hello
3,返回对象数据视图
需要在配置文件中增加json数据的viewresolver
<!-- ContentNegotiatingViewResolver ,“内容协商视图解析器”, -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="atom" value="application/atom+xml"/>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="viewResolvers">
<list>
<!-- <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="../view/"/>
<property name="suffix" value=".html"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
</list>
</property>
</bean>
在conntroller中
/**
* 配合@ResponseBody的使用和配置的MappingJackson2JsonView,将controller中的对象数据转化为json传送到页面
* */
@RequestMapping(value="/getPerson",method=RequestMethod.POST)
@ResponseBody
public Map<String,Object> put(String username,String password){
Map<String,Object> map=new HashMap<String,Object>();
Person person=new Person();
person.setUsername(username);
person.setPassword(password);
map.put("p", person);
//model.addAttribute("person", person);
return map;
}
在页面中
$(function(){
$("#btn").click(function(){
$.post("mvc/getPerson",{username:$(".username").val(),password:$(".password").val()}, function(data){
alert(data.p.password);
alert(data.p.username);
});
});
});
就可以获得相应person的对象和属性 ;
- spring学习controller向视图传递参数1
- spring MVC学习笔记(三) spring MVC视图与controller之间参数传递
- Spring MVC 3学习笔记+教程(二)在controller和视图之间传递参数
- Spring MVC 3学习笔记+教程 在controller和视图之间传递参数
- Spring MVC 3学习笔记+教程(二)在controller和视图之间传递参数
- Spring MVC 3学习笔记+教程(二)在controller和视图之间传递参数
- 在controller和视图之间传递参数
- Spring MVC在controller和视图之间传递参数的方法
- Spring MVC3在controller和视图之间传递参数的方法
- Spring MVC3在controller和视图之间传递参数的方法
- Spring MVC3在controller和视图之间传递参数的方法
- Spring MVC3在controller和视图之间传递参数的方法
- Spring MVC3在controller和视图之间传递参数的方法
- [SSM]Spring MVC3在controller和视图之间传递参数的方法
- springmvc在controller和视图之间传递参数
- Ruby on Rails,通过页面链接向Controller中传递参数,附赠调试技巧
- spring websocket stomp如何向服务端传递参数
- 向sqlplus传递参数
- 安卓简单手电筒开发
- window compile bitcoin
- [数据结构]Linked_stack
- 2016年腾讯实习生校园招聘-电面2
- 正确用dd测试磁盘写速度
- spring学习controller向视图传递参数1
- iOS 指纹解锁
- 下载aptana插件网址
- 第二篇——坚持!!
- 正整数分组
- 实模式
- Android Activity详解
- UICollectionView
- 面试题67:把二叉树打印成多行