如何在春季将数据从控制器传递到前端 [英] How to pass data from controller to front end in spring

查看:100
本文介绍了如何在春季将数据从控制器传递到前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是春季新手,在spring3.0中创建了一个应用程序 我正在使用注释弹簧.

I am new in spring,creating one application in spring3.0 I am using annotation spring.

我想将列表从我的控制器方法返回到jsp.该列表将显示在选择框中.以下是控制器和jsp部分的代码. 请帮助我理解这一点.

i want to return on list from my controller method to jsp. where that list will be shown in selection box. following is the code of controller and jsp part. Please help me to understand this.

public String showUserForm(ModelMap model)  
{       
  User user = new User();
  model.addAttribute(user);
  List<String> lookingfor=service.getOptions();
  //want to send this "lookingfor" to the jsp       
  return "welcome";     
}

和JSP

<td>    
  <form:select path="lookingfor">
  <form:option value="0" label="Select" /> 
  <form:options items="${lookingfor}" itemValue="lookingfor" itemLabel="lookingfor" />                 </form:select>           
</td>

虽然在Model中添加用户,但其工作正常,但是当我添加lookingfor属性时. model.addAttribute("lookingfor",lookingfor);

While adding user in Model, its working fine, but when i am adding lookingfor attribute. model.addAttribute("lookingfor", lookingfor);

出现以下错误.

its giving following error.

严重:servlet jsp的Servlet.service()抛出异常 org.springframework.beans.NotReadablePropertyException:无效 Bean类[java.lang.String]的属性'lookin gfor':Bean属性 'lookfor'无法读取或具有无效的getter方法: getter的返回类型与setter的参数类型匹配吗?

SEVERE: Servlet.service() for servlet jsp threw exception org.springframework.beans.NotReadablePropertyException: Invalid property 'lookin gfor' of bean class [java.lang.String]: Bean property 'lookingfor' is not readab le or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

推荐答案

您必须将looking列表作为属性添加到ModelMap对象:

You have to add the lookingFor list to the ModelMap object as an attribute:

model.addAttribute("lookingfor", lookingfor);

还要验证属性"itemValue"和"itemLabel"的用法,因为itemValue和itemLabel都引用lookingFor列表的项目的属性.您在两者上都使用了相同的值,这是错误的.

Also verify the usage of the attributes "itemValue" and "itemLabel", because both itemValue and itemLabel refer to properties of the items of lookingFor list. You're using the same value on both and that's kind of wrong.

这篇关于如何在春季将数据从控制器传递到前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆