js怎么获取从springmvc后台传过来的数据

js怎么获取从springmvc后台传过来的数据,第1张

在springmvc中controller的结果集可通过json格式传到js前端接受,也可以通过Map传给前端,具体实现如下

1,通过json格式传递

controller层实现如下

@RequestMapping("queryCityInfo")

@ResponseBody

public String queryCityInfo()throws Exception{

String provinceId = getString("id");

@SuppressWarnings("rawtypes")

List cityList = personalServicequeryCity(provinceId);

if(null != cityList && cityListsize() >0 ){

String json = JSONUtilstoJSONString(cityList);

superoutStr(json);

}

return null;

}

protected void outStr(String str)</span>

{

try

{

responsesetCharacterEncoding("UTF-8");

responsegetWriter()write(str);

}

catch (Exception e)

{

}

}

public static <T> String toJSONString(List<T> list)

{

JSONArray jsonArray = JSONArrayfromObject(list);

return jsonArraytoString();

}

js端接受如下

function selectBankCity(id){

$ajax({

url:baseAddress+"queryCityInfodoprovinceId="+id,

type:'get',

dataType:'json',

success:function(data){

$('#custBankArea')empty();

$('#custBankArea')append("<option >--请选择城市信息--</option>");

for(var i=0;i<datalength;i++){

$('#custBankArea')append("<option value='"+scjgcjcomdata[i]id+"'>"+data[i]cityName+"</option>");

}

}

});

}

2,通过Map传递

controller层实现如下

@RequestMapping("queryProvince")

@ResponseBody

public Map<String, Object>  queryProvince(>

如果json数组直接作为POSTDATA的内容传递,Controller在方法上声明@RequestBody,参数选好类型,名称随缘,你会得到数组或List,但是需要依赖jackson包

如果json作为FORMDATA传递,Controller方法参数为String,名称需要与表单参数相同,你会得到json字符串

1、第一步,先新建一个properties文件,

appproperties里面内容

admin=admin

test=test

2、第二步,新建一个xml文件,在applicationContextxml,

<!-- 用来解析Java Properties属性文件值(注意class指定的类)-->

<bean id="placeholderConfigurer" class="orgspringframeworkbeansfactoryconfigPropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>classpath:appproperties</value>

</list>

</property>

</bean>

<!-- 把properties里面的信息读进来: -->

<bean id="report" class="javalangString">

<constructor-arg value="${admin}"/>

</bean>

private void createDefaultEditors() {

thisdefaultEditors = new HashMap(64);

// Simple editors, without parameterization capabilities

// The JDK does not contain a default editor for any of these target types

thisdefaultEditorsput(Charsetclass, new CharsetEditor());

thisdefaultEditorsput(Classclass, new ClassEditor());

thisdefaultEditorsput(Class[]class, new ClassArrayEditor());

thisdefaultEditorsput(Currencyclass, new CurrencyEditor());

thisdefaultEditorsput(Fileclass, new FileEditor());

thisdefaultEditorsput(InputStreamclass, new InputStreamEditor());

thisdefaultEditorsput(InputSourceclass, new InputSourceEditor());

thisdefaultEditorsput(Localeclass, new LocaleEditor());

thisdefaultEditorsput(Patternclass, new PatternEditor());

thisdefaultEditorsput(Propertiesclass, new PropertiesEditor());

thisdefaultEditorsput(Resource[]class, new ResourceArrayPropertyEditor());

thisdefaultEditorsput(TimeZoneclass, new TimeZoneEditor());

thisdefaultEditorsput(URIclass, new URIEditor());

thisdefaultEditorsput(URLclass, new URLEditor());

thisdefaultEditorsput(UUIDclass, new UUIDEditor());

// Default instances of collection editors

// Can be overridden by registering custom instances of those as custom editors

thisdefaultEditorsput(Collectionclass, new CustomCollectionEditor(Collectionclass));

thisdefaultEditorsput(Setclass, new CustomCollectionEditor(Setclass));

thisdefaultEditorsput(SortedSetclass, new CustomCollectionEditor(SortedSetclass));

thisdefaultEditorsput(Listclass, new CustomCollectionEditor(Listclass));

thisdefaultEditorsput(SortedMapclass, new CustomMapEditor(SortedMapclass));

// Default editors for primitive arrays

thisdefaultEditorsput(byte[]class, new ByteArrayPropertyEditor());

thisdefaultEditorsput(char[]class, new CharArrayPropertyEditor());

// The JDK does not contain a default editor for char!

thisdefaultEditorsput(charclass, new CharacterEditor(false));

thisdefaultEditorsput(Characterclass, new CharacterEditor(true));

// Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor

thisdefaultEditorsput(booleanclass, new CustomBooleanEditor(false));

thisdefaultEditorsput(Booleanclass, new CustomBooleanEditor(true));

// The JDK does not contain default editors for number wrapper types!

// Override JDK primitive number editors with our own CustomNumberEditor

thisdefaultEditorsput(byteclass, new CustomNumberEditor(Byteclass, false));

thisdefaultEditorsput(Byteclass, new CustomNumberEditor(Byteclass, true));

thisdefaultEditorsput(shortclass, new CustomNumberEditor(Shortclass, false));

thisdefaultEditorsput(Shortclass, new CustomNumberEditor(Shortclass, true));

thisdefaultEditorsput(intclass, new CustomNumberEditor(Integerclass, false));

thisdefaultEditorsput(Integerclass, new CustomNumberEditor(Integerclass, true));

thisdefaultEditorsput(longclass, new CustomNumberEditor(Longclass, false));

thisdefaultEditorsput(Longclass, new CustomNumberEditor(Longclass, true));

thisdefaultEditorsput(floatclass, new CustomNumberEditor(Floatclass, false));

thisdefaultEditorsput(Floatclass, new CustomNumberEditor(Floatclass, true));

thisdefaultEditorsput(doubleclass, new CustomNumberEditor(Doubleclass, false));

thisdefaultEditorsput(Doubleclass, new CustomNumberEditor(Doubleclass, true));

thisdefaultEditorsput(BigDecimalclass, new CustomNumberEditor(BigDecimalclass, true));

thisdefaultEditorsput(BigIntegerclass, new CustomNumberEditor(BigIntegerclass, true));

// Only register config value editors if explicitly requested

if (thisconfigValueEditorsActive) {

StringArrayPropertyEditor sae = new StringArrayPropertyEditor();

thisdefaultEditorsput(String[]class, sae);

thisdefaultEditorsput(short[]class, sae);

thisdefaultEditorsput(int[]class, sae);

thisdefaultEditorsput(long[]class, sae);

}

}

2、基本数据类型绑定:

@RequestMapping("testdo")

public void test(int num) {

}

"testdo" method="post">

"num" value="10" type="text"/>

注意:表单中input的name值和Controller的参数变量名保持一致,就能完成基本数据类型的数据绑定,如果不一致可以使用@RequestParam标注实现。值得一提的是,如果Controller方法参数中定义的是基本数据类型,但是从jsp提交过来的数据为null或者""的话,会出现数据转换的异常。也就是说,必须保证表单传递过来的数据不能为null或"",所以,在开发过程中,对可能为空的数据,最好将参数数据类型定义成包装类型。

3、包装类型

@RequestMapping("testdo")

public void test(Integer num) {

}

"testdo" method="post">

"num" value="10" type="text"/>

和基本数据类型基本一样,不同之处在于,JSP表单传递过来的数据可以为null或"",以上面代码为例,如果jsp中num为""或者表单中无num这个input,那么,Controller方法参数中的num值则为null。

4、自定义对象类型

public class User {

private String firstName;

private String lastName;

}

@RequestMapping("testdo")

public void test(User user) {

}

"testdo" method="post">

"firstName" value="张" type="text"/>

"lastName" value="三" type="text"/>

只需将对象的属性名和input的name值一一对应即可。

5、自定义复合对象类型

public class ContactInfo {

private String tel;

private String address;

。。。

}

public class User {

private String firstName;

private String lastName;

private ContactInfo contactInfo;

。。。

}

@RequestMapping("testdo")

public void test(User user) {

Systemoutprintln(usergetFirstName());

Systemoutprintln(usergetLastName());

Systemoutprintln(usergetContactInfo()getTel());

Systemoutprintln(usergetContactInfo()getAddress());

}

<</span>form action="testdo" method="post">

<</span>input name="firstName" value="张" /><</span>br>

<</span>input name="lastName" value="三" /><</span>br>

<</span>input name="contactInfotel" value="13809908909" /><</span>br>

<</span>input name="contactInfoaddress" value="北京海淀" /><</span>br>

<</span>input type="submit" value="Save" />

</</span>form>

User对象中有ContactInfo属性,Controller中的代码和第3点说的一致,但是,在jsp代码中,需要使用“属性名(对象类型的属性)属性名”来命名input的name。

以上就是关于js怎么获取从springmvc后台传过来的数据全部的内容,包括:js怎么获取从springmvc后台传过来的数据、SpringMVC中,前台传json数组,后台怎么接受,然后解析成list、springmvc中如何从配置文件中读取信息等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9350181.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存