欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java ssm框架的controller實(shí)現(xiàn)向頁面?zhèn)鬟f參數(shù)

 更新時(shí)間:2022年05月05日 09:37:42   作者:ConfidentWU  
這篇文章主要介紹了java ssm框架的controller實(shí)現(xiàn)向頁面?zhèn)鬟f參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

ssm的controller向頁面?zhèn)鬟f參數(shù)

使用Map<String, Object> 來傳遞參數(shù)

在控制器的方法中添加一個(gè)Map類型參數(shù)A。給參數(shù)A的put方法put了鍵值對(duì)B,鍵值對(duì)B在頁面中就可以獲取到了

1.java 后臺(tái)代碼的編寫,put了operation和application鍵值對(duì)

@RequestMapping("/edit_form")
?? ?public String editApplicationFormPage(Map<String, Object> map,
?? ??? ??? ?HttpServletRequest request, String applicationId) {
?? ??? ?map.put("operation", "edit");
?? ??? ?Application application = applicationService
?? ??? ??? ??? ?.getApplicationById(applicationId);
?? ??? ?if(application.getSysBigIcon()==null||application.getSysBigIcon().equals("")){
?? ??? ??? ?application.setSysBigIcon("/www/images/default.png");
?? ??? ?}
?? ??? ?if(application.getSysIcon()==null||application.getSysIcon().equals("")){
?? ??? ??? ?application.setSysIcon("/www/images/default.png");
?? ??? ?}
?? ??? ?if (application != null) {
?? ??? ??? ?map.put("application", application);
?? ??? ?}
?? ??? ?return "/frame/system/application/application_form";
?? ?}

2.頁面使用后臺(tái)傳遞過來的鍵值對(duì)

使用的方法是鍵值對(duì)要用${}包裹起來。 比如:${operation}和${application.orgId},${operation}是引用后臺(tái)map put的operation鍵值對(duì),${application.orgId}是引用后臺(tái)map put的application實(shí)體的一個(gè)對(duì)象。

<script type="text/javascript">
?? ?window.WWWROOT = "${ctx}";
?? ?window.DefaultOrgId ?= "<%=user.getDefaultOrgId()%>";
?? ?window.Operation = "${operation}";
?? ?window.OrgId = "${application.orgId}";
?? ?window.TaskAppId = "${application.taskAppId}";
?? ?window.MenuType = "${application.menuType}";
</script>
?? ??? ??? ?<title>${operation eq 'add'?'添加':(operation eq
?? ??? ??? ??? ?'edit'?'編輯':'查看')}應(yīng)用系統(tǒng)</title>

使用PrintWriter來傳遞參數(shù)

給PrintWriter寫一些內(nèi)容。就把這些內(nèi)容返回到頁面了。

1.后臺(tái)臺(tái)代碼的編寫

在控制器的方法中添加一個(gè)PrintWrite類型參數(shù)writer,使用writer.write()方法寫入內(nèi)容。頁面就可以返回這內(nèi)容了。代碼如下:

@RequestMapping("/add")
?? ?public void ?add(HttpServletRequest request, HttpServletResponse response,MenuRight menuRight, PrintWriter writer)
?? ??? ??? ? {
?? ??? ?try{
?? ??? ? ? ?Boolean result =menuRightService.addMenuRight(menuRight);
?? ??? ??? ?writer.write("{\"success\":true}");
?? ??? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ?writer.write("{\"success\":false}");
?? ??? ??? ??? ?}
?
?? ?}

2.頁面的代碼編寫 

  • success : function(result)中的result就是后臺(tái)返回的writer.write()中的內(nèi)容
$.ajax({
?? ??? ?type : 'POST',
?? ??? ?url : WWWROOT + "/menuRight/add",
?? ??? ?data : dat,
?? ??? ?success : function(result) {
?? ??? ??? ?if ($.parseJSON(result).success == true) {
?? ??? ??? ??? ?$(stId).attr("checked", true);
?? ??? ??? ?} else {
?? ??? ??? ??? ?alert("添加授權(quán)失敗");
?? ??? ??? ??? ?$(stId).attr("checked", false);
?? ??? ??? ?}
?? ??? ?}
?? ?});

ssm框架獲取頁面?zhèn)鬟f的參數(shù)

通過@RequestParam

接收名字為age的參數(shù),且可以為空

@RequestParam(value = "age",required = false)

通過@PathVariable

通過@RequestBody –不適用于Get請(qǐng)求

  • 1.@RequestBody 接收的是一個(gè)請(qǐng)求體, @RequestBody只能存在一個(gè),接收的是所有的請(qǐng)求參數(shù)--一次接收完
  • 2.如果傳對(duì)象或者數(shù)組必須先轉(zhuǎn)為Json格式\或者是純字符串
  • 3.@RequestBody 不適合用于Get請(qǐng)求

接收日期類型: @DateTimeFormat\@JsonFormat

  • @DateTimeFormat使用場(chǎng)景:頁面直接傳遞日期格式時(shí),直接用該注解接收;
  • @JsonFormat使用場(chǎng)景:頁面?zhèn)鬟fJson格式的日期格式時(shí),使用該注解接收;  特別注意:使用不同的Json包該注解名可能不同

使用方法:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論