MVC 设置路由 多个参数

MVC 设置路由 多个参数,第1张

routesMapRoute(

"StaticHtml",

//

路由名称

"{

controller

}/{action}/{id}html",

//

带有参数

URL

new

{

controller

=

"Home",

action

=

"Index",

id

=UrlParameterOptional

}

);

然后后台去

处理

{id}

,通过截取字符串的方式,获取所需的参数。

此时,后台Action的参数应为

string

id

在cs文件中,用 PageRequest["参数名"]就可以得到AJAX传递过来的值。

在 aspnet中,无论是普通数组还是JSON数组,都可以先转换成字符串,然后传递过去。aspnet mvc可以直接传对象

例如:

在JS中:

//这里用的是Jquery的ajax

funcation test()

{

var d = [1, 2, 3]; //数组

var s = djoin(','); //转换成字符串

$ajax({

url: "要访问的地址",

data: { p: s,n:d }, //p是参数名,s是值

success: function (msg) {

//msg是返回的结果,这里对返回值进行处理

}

});

}

aspnet后台:

public void TestData()

{

string s=PageRequest["p"];//得到ajax传递的参数p的值

string[] d=ssplit(',');//转换成字符数组,至于怎么转成整形数组我就不写了。

}

aspnet mvc后台:

public JsonResult TestData(string p,int[] n)

{

string[] d=psplit(',');//转换成字符数组

return Json(new {xxxx});

}

JSON数组和普通数组一样处理,都可以拼成字符串,然后传递

用@ResponseBody

@RequestMapping(value = "/address", method = RequestMethodPOST)

@ResponseBody

public Map<String, Object> addressPOST() {

Map<String, Object> map = new HashMap<>();

ReceivedGoodsAddress receivedgoodsaddress=new ReceivedGoodsAddress();

List<ReceivedGoodsAddress> list = addressServicefindReceivedGoodsAddress(receivedgoodsaddress);

mapclear();

mapput("list", list);

return map;

}

//前台用jquery+ajax

$ajax({

type:'POST',

url:'${contextPath}/address',

dataType:'json',

success:function(data){

$each(datalist,function(i,item){

alert(i);

alert(itemid);

alert(itemname);

});

}

});

它们发送的是XML数据,不是键值对形式的,所以才要这样获取:

if (Request>

一、使用注解式控制器注册PropertyEditor(针对具体的controller类处理)

1、使用WebDataBinder进行控制器级别的注册PropertyEditor(控制器独享)

Java代码

@InitBinder

// 此处的参数也可以是ServletRequestDataBinder类型

public void initBinder(WebDataBinder binder) throws Exception {

// 注册自定义的属性编辑器

// 1、日期

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//CustomDateEditor类是系统内部自带的类

CustomDateEditor dateEditor = new CustomDateEditor(df, true);

// 表示如果命令对象有Date类型的属性,将使用该属性编辑器进行类型转换

binderregisterCustomEditor(Dateclass, dateEditor);

// 自定义的电话号码编辑器(和4161、数据类型转换一样)

binderregisterCustomEditor(PhoneNumberModelclass, new PhoneNumberEditor());

}

备注:转换对象必须要实现PropertyEditor接口,例如CustomDateEditor类

Java代码

package orgspringframeworkbeanspropertyeditors;

import javabeansPropertyEditorSupport;

import javatextDateFormat;

import javatextParseException;

import javautilDate;

import orgspringframeworkutilStringUtils;

public class CustomDateEditor extends PropertyEditorSupport {

private final DateFormat dateFormat;

private final boolean allowEmpty;

private final int exactDateLength;

public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty) {

thisdateFormat = dateFormat;

thisallowEmpty = allowEmpty;

exactDateLength = -1;

}

public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty, int exactDateLength) {

thisdateFormat = dateFormat;

thisallowEmpty = allowEmpty;

thisexactDateLength = exactDateLength;

}

public void setAsText(String text) throws IllegalArgumentException {

if (allowEmpty && !StringUtilshasText(text)) {

setValue(null);

} else {

if (text != null && exactDateLength >= 0 && textlength() != exactDateLength)

throw new IllegalArgumentException((new StringBuilder("Could not parse date: it is not exactly"))append(exactDateLength)append("characters long")toString());

try {

setValue(dateFormatparse(text));

} catch (ParseException ex) {

throw new IllegalArgumentException((new StringBuilder("Could not parse date: "))append(exgetMessage())toString(), ex);

}

}

}

public String getAsText() {

Date value = (Date) getValue();

return value == null "" : dateFormatformat(value);

}

}

2、使用xml配置实现类型转换(系统全局转换器)

(1、注册ConversionService实现和自定义的类型转换器

Xml代码

<!-- ①注册ConversionService -->

<bean id="conversionService" class="orgspringframeworkformatsupportFormattingConversionServiceFactoryBean">

<property name="converters">

<list>

<bean class="hbbaseconvertDateConverter">

<constructor-arg value="yyyy-MM-dd"/>

</bean>

</list>

</property>

<!-- 格式化显示的配置

<property name="formatters">

<list>

<bean class="hbbaseconvertDateFormatter">

<constructor-arg value="yyyy-MM-dd"/>

</bean>

</list>

</property>

-->

</bean>

(2、使用 ConfigurableWebBindingInitializer 注册conversionService

Xml代码

<!-- ②使用 ConfigurableWebBindingInitializer 注册conversionService -->

<bean id="webBindingInitializer"

class="orgspringframeworkwebbindsupportConfigurableWebBindingInitializer">

<property name="conversionService" ref="conversionService" />

<property name="validator" ref="validator" />

</bean>

(3、注册ConfigurableWebBindingInitializer 到RequestMappingHandlerAdapter

Xml代码

<!--Spring31开始的注解 HandlerAdapter -->

<bean

class="orgspringframeworkwebservletmvcmethodannotationRequestMappingHandlerAdapter">

<!--线程安全的访问session -->

<property name="synchronizeOnSession" value="true" />

<property name="webBindingInitializer" ref="webBindingInitializer"/>

</bean>

此时可能有人会问,如果我同时使用 PropertyEditor 和 ConversionService,执行顺序是什么呢?内部首先查找PropertyEditor 进行类型转换,如果没有找到相应的 PropertyEditor 再通过 ConversionService进行转换。

有很多方法:

1控制器中跳转:

return RedirectToAction("Testing_view", new { papeid = papelisTest_paper_id, name = names, idcards = idcard, question_sums = papelismutil_questions_sum, limit_time = papelisTest_paper_spend_time });

2页面跳转:

<%:HtmlActionLink("详细", "Test_query_view",new {papeid=itemTest_paper_id,name=itemresume_infoPerson_name,idcard=itemresume_infoPerson_idcard,used_time=itemused_time})%>

还有很多

首先,上的参数只有一个,多个参数是通过&符号连接起来的

你这个的路由协议就是{controller}/{action}是没有参数的,只不过在输入url的时候加上了而已

如果url是controller/action/id这样才是定义有参数的路由协议

以上就是关于MVC 设置路由 多个参数全部的内容,包括:MVC 设置路由 多个参数、asp.net mvc中怎么获取ajax传递过来的数组、springmvc 多文件上传 MultipartFile 怎么获取前台传过来的参数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存