基于注解的springMVC 联系客服

发布时间 : 星期一 文章基于注解的springMVC更新完毕开始阅读49fd8542b307e87101f696e6

的prefix的值+requestMapping返回的值+suffix的值 第二种:返回一个String类型:

/**

* 根据String字符串返回对应的地址 prefix前缀+返回值+suffix后缀组成 * */

@RequestMapping(\) public String string() {

logger.info(\方法调用\); return \; }

第三种:返回一个ModelAndView对象

/**

* spring2.5的方法,返回一个ModelAndView 对象,通过setViewName方法跳转到指定的页面 调用addObject * 相当于调用request.setAttribute方法 * */

@RequestMapping(\)

public ModelAndView view(Model model) { logger.info(\方法调用\);

ModelAndView andView = new ModelAndView(); andView.setViewName(\); return andView; }

第四种返回一个Map集合

/**

* @see 使用map作为返回值的时候 是以prefix前缀+requestMapping的value+suffix后缀组成 返回一个map

* ,map的put方法调用相当于request.setAttribute方法 * */

@RequestMapping(\)

public Map mapa(ModelMap map1) {

Map map = new HashMap(); UserBean bean = new UserBean(); bean.setId(1);

bean.setUsername(\); bean.setPassword(\); map.put(\, \); map.put(\, bean); return map;

}

使用第四种方法,可以在页面中通过调用JSTL进行取值,如下面jsp代码

<%@ page language=\ pageEncoding=\

Mapa Map a

姓名:${user.username } 密码:${user.password } hello:${hello }

第五种返回一个ModelMap类型:

/**

* @see 返回一个ModelMap类型,返回地址根据以prefix前缀+requestMapping的value+suffix后缀组成

* ModelMap 本身也拥有hashmap的方法,也可以使用addAllAttributes对一个map添加到attribute里面 * */

@RequestMapping(\ public ModelMap map() {

ModelMap map = new ModelMap(); map.addAttribute(\ map.addAllAttributes(temp()); return map; }

/**

*@see 临时类

*@return 返回一个map类型 * */

public Map temp() {

Map map1 = new HashMap();

UserBean bean = new UserBean(); bean.setId(1);

bean.setUsername(\

bean.setPassword(\ map1.put(\

// map1.put(\ UserBean bean1 = new UserBean(); bean1.setId(2);

bean1.setUsername(\ bean1.setPassword(\ map1.put(\ System.out.println(map1); return map1; }

使用ModelMap可以把一个多个集合存到一个属性中,可以直接在页面调用EL 语言进行读取,jsp代码如下:

<%@ page language=\ pageEncoding=\

Mapa Map a

姓名:${user.username } 密码:${user.password } hello:${hello } aa:${aa }

[学习笔记]基于注解的spring3.0.x MVC学习笔记(四)

好久没有更新关于spring3.0.x的学习笔记了,这笔记除了平时工作上用的代码之外还有部分代码是根据我的老师学习下来后我再重新理解所整理的,接着上期第三部分的内容: 上次写到了requestMapping对返回集合的用法,使用集合返回的话则需要根据requestmapping中values的值进行命名jsp页面,而整个路径地址是根据视图模型的返回地址来确定.这次主要介绍的是返回类型为Model,List.Collection,Set,Object,先看返回类型为Model,代码如下:

1/** 2 * @see 返回一个Model接口.通过创建ExtendedModelMap可以使用,方法同modelMap一样 3 * */ 4 @RequestMapping(\ 5 public Model model() { 6 Model model = new ExtendedModelMap(); model.addAllAttributes(temp()); 8 return model; 910 }

7

temp方法代码如下:

1: /**

2: * @see 临时类

3: * @return 返回一个map类型

4: * */

5: public Map temp() {

6: Map map1 = new HashMap();

7: UserBean bean = new UserBean(); 8: bean.setId(1);

9: bean.setUsername(\); 10: bean.setPassword(\); 11: map1.put(\, bean);

12: // map1.put(\ 13: UserBean bean1 = new UserBean(); 14: bean1.setId(2);

15: bean1.setUsername(\); 16: bean1.setPassword(\); 17: map1.put(\, bean1); 18: System.out.println(map1); 19: return map1; 20: }

通过model的addAllAttributes可以把一个map集合的内容放到model对象中,通过jsp中读取指定的数据,相当于读取request.getAttributes一样 jsp代码如下图:

效果如下图:

对于List,Collection,Set这3种类型返回的方式都是根据requestmapping的value标记进行读取指定页面,但是在jsp页面中读取方式值的方式相对有些不同,首先是对List: 对于List代码只是把返回类型改为了List之外,在jsp中我们需要根据代码第一个add的数据类型进行保存,代码如下: