JavaWeb从JSP传递给controller参数的两种方式

    xiaoxiao2021-12-14  19

    第一种:通过使用form表单(隐藏域)

    第二种:通过ajax提交,传递的少量参数

    Jquery AJAX提交表单有两种方式,一是url参数提交数据二是form提交(和平常一样在后台可以获取到Form表单的值)。在所要提交的表单中,如果元素很多的话建议用第二种方式进行提交。

    form提交

    新建两个页面:

    1、show.jsp:调用ajax,将表单中的数据发送给ajax.jsp页面。

    2、ajax.jsp:获取show.jsp页面传递的表单数据,并返回结果。

    两个页面的编码格式要设置为GBK:

    ? 1 <%@ page contentType= "text/html;charset=GBK" %>

    show.jsp页面的重点部分:

    1、添加对 jquery-1.3.2.min.js 的引用:

    ? 1 <script type= "text/javascript" src= "jquery-1.3.2.min.js" ></script>

    2、设置表单的id,在调用ajax的方法时要用到。一般通过表单的id来submit();

    ? 1 <form id= "ajaxFrm" >

    3、设置一个div,用于显示ajax.jsp页面返回的结果

    ? 1 <div id= "ajaxDiv" ></div>

    4、增加一个按钮,用来调用ajax,这里使用onclick事件来提交表单

    ? 1 <input type= "button" onClick= "doFind();" value= "调用一下ajax" >

    5、增加调用ajax的函数:

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 function doFind(){   $.ajax({   cache: false ,   type: "POST" ,   url: "ajax.jsp" , //把表单数据发送到ajax.jsp   data:$( '#ajaxFrm' ).serialize(), //要发送的是ajaxFrm表单中的数据   async: false , //不同步   error: function (request) {   alert( "发送请求失败!" );   },   success: function (data) {   $( "#ajaxDiv" ).html(data); //将返回的结果显示到ajaxDiv中   }   });   }

    ajax.jsp页面的源代码:

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <%@ page contentType= "text/html;charset=GBK" %>   <%   String userName = request.getParameter( "UserName" );   if (userName!= null ){   userName = new String(userName.getBytes( "ISO-8859-1" ), "utf-8" ); //解决乱码的问题   }   String returnString = "" ;   returnString= "你好," + userName;   out.print(returnString); //输出的结果显示到<div id="ajaxDiv"></div>   %> jquery ajax提交表单从action传值到jsp

    jsp页面:

    代码如下:

    ? 1 2 3 4 5 6 7 8 9 var clientTel = $( "#clientTel" ).val(); var activityId = $( "#activityId" ).val(); $.ajax({ type : "post" , //发送方式 url : "/arweb/reserve/saveCode.action" , // 路径 data : "clientTel=" +clientTel+ "&activityId=" +activityId , //url中添加参数传递 success: function (text){$( "#randomCode" ).val(text);}, error: function (text) {alert( "对不起,用户ID不存在,请输入正确的用户ID" );} });

    acion类:

    代码如下:

    ? 1 2 3 4 5 6 7 HttpServletResponse res = ServletActionContext.getResponse(); res.reset(); res.setContentType( "text/html;charset=utf-8" ); PrintWriter pw = res.getWriter(); pw.print(random); pw.flush(); pw.close();

    pw.print(random);这里的random就是action要向jsp传的值,在jsp中,success: function(text)这里的text就是接收从action传过来的值。

    第三种:url+参数传递,controller层都可以使用request或者方法的形参中定义同名参数接收

    第四种:pojo对象的传递,controller可以在方法的形参中定义pojo对象,这样jsp页面标签的name属性名要和pojo对象的属性名一致,pojo才可以接收到

    第五种:简单类型的传递,jsp页面标签的name属性名和controller方法形参的变量名一致,可以接收,类似于第三种

    第六种:数组的传递,controller方法形参使用数组名称要和jsp页面的标签的name属性值一致

    第七种:集合list传递(通常都是批量提交数据时使用一个或多个list<pojo>),  controller方法形参不能直接接收list,一般使用包装类型Vo(Vo中包含list属性,例如:list<pojo>,Vo可以包装复杂的查询条件,多个表对象都包含进去),jsp页面标签的name属性名要与Vo中的list<UserCustom> userlist的对象的UserCustom中属性名一致,jsp页面标签name的写法为name=userlist.[${status.index }].name

    转载请注明原文地址: https://ju.6miu.com/read-971156.html

    最新回复(0)