2、其次引入Jacksonxml的maven依赖。
3、最后建立好Controller控制层,和SpringMvc一样镇告进行扒旅吵路径访问即可。春侍
springmvc 中controller获取ajax数据的方法:1、页面jsp代码:
<table style="width: 100%" class="table" cellspacing="1" cellpadding="1" border="0">
<tr><td class="ti1" colSpan="2">请选择审讯尘历纳室</td></tr>
<tr><td class="ti2hui">派没审讯室名称</td><td class="ti1cu">
<select id="roomid" name="roomid" >
<c:forEach items="${roomlist}" var="room">
<option value ="${room.id}">${room.name}</option>
</c:forEach>
</select>
</td></tr>
<tr><td class="ti2hui" colSpan="2" align="center"><input type="button" onclick="setshow()" value="确定"/> </td></tr>
</table>
点击确定后触发ajax接口:
function setshow(){
$.ajax( {
type : "POST"烂侍,
url : "<%=request.getContextPath()%>/initroom.do?method=set",
data : {
'room' : $("#roomid").find('option:selected').text(),
'roomid' :$("#roomid").val()
},
dataType: "json",
success : function(data) {
if(data.success){
alert("设置成功!")
}
else{
alert("设置失败!")
}
},
error :function(){
alert("网络连接出错!")
}
})
}
2、后台controller写法:
@RequestMapping(params = "method=set")
public void jump(HttpSession session,HttpServletRequest request, HttpServletResponse response) throws Exception{
String roomid= request.getParameter("roomid")
String room= request.getParameter("room")
session.setAttribute("ROOMID", roomid)
session.setAttribute("ROOMNAME", room)
System.out.println("session set:"+room+"=="+roomid)
response.setCharacterEncoding("utf-8")
response.getWriter().write("{\"success\":true }")
response.getWriter().flush()
}
3、springmvc 返回信息到ajax:
import com.googlecode.jsonplugin.JSONUtil
List<Records>recordList = new ArrayList<Records>()
//获取recordlist *** 作省略
response.setCharacterEncoding("utf-8")
response.getWriter().write("{\"success\":true, \"data\":" + JSONUtil.serialize(recordList) + "}")
response.getWriter().flush()
MultipartHttpServletRequest用这个request别伏尘用 HttpServletRequest
再者 如果弯哪要ajax提埋厅码交的话 需要嵌套iframe
互联网上面有这个ajaxfileuploadV2.1.js
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)