//獲取Userid function getUserId(){ $.getJSON('<%=basePath %>user/getUserId.do', function(data){ alert(eval(data).userId); document.getElementById("userId").value=data.userId; }); }
<input id="userId" name="userId" value="" type="text" readonly="readonly"/><input value="獲取最大的Userid" onclick="javascript:getUserId();" type="button"> @ResponseBody //@ResponseBody 返回的是數據,不加 @ResponseBody 返回的是頁面。 @RequestMapping(value="/user/getUserId.do")//method=RequestMethod.POST public JSONObject getUserId(HttpServletRequest request, HttpServletResponse response)throws Exception { response.setContentType("text/html"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); JSONObject jsonObject = new JSONObject(); Integer userId = 0; try { userId = userSlaveService.getUserId(); } catch (Exception e) { Loger.logtxt("user", "獲取id異常:" + e.toString()); } jsonObject.accumulate("userId", userId); System.out.println(jsonObject.toString()); //輸出: {"userId":182888} System.out.println(jsonObject.values()); //輸出: [182888] return jsonObject; //返回json,在上面的jsp頁面中接收,並且保存值到input中。 }
UserSlaveService.java 接口聲明方法 : public Integer getUserId()throws Exception;
UserSlaveServiceImpl.java 實現UserSlaveService接口:
@Override
public Integer getUserId() throws Exception {
return userDAO.getUserId();
}
IUserDAO.java 接口聲明方法: public Integer getUserId()throws Exception;
user.xml 中的查詢:
<select id="getUserId" resultType="java.lang.Integer">
<![CDATA[
SELECT (MAX(userid)+1) AS userid FROM userinfo
]]>