【发布时间】:2013-11-12 00:01:13
【问题描述】:
我有一个控制器类,其方法名为 Loginsuccesspage。此方法用于下载文件并将一些用户详细信息插入数据库。反过来,它返回到它自己的登录成功页面(这是一个 JSP)。现在我正在尝试重构代码,使其需要下载并返回 JSP 登录成功页面,并且还应该在指定的 URL (machineip:portno/target) 中以 JSON 格式构造用户详细信息。将从我的 android 应用程序访问此 URL 以检索用户详细信息。我附加了 LoginsuccessPage 方法,该方法执行下载逻辑和返回 JSON 对象的 getUserDetails 方法。这种方法行不通。有没有其他方法可以实现这一点。
@RequestMapping(value = "/loginsuccess", method = RequestMethod.GET)
public String loginSuccessPage(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
if (username != null) {
model.addAttribute("message1", username);
} else {
model.addAttribute("message1", deleteUserName);
}
HashMap<String, String> appList = new HashMap<String, String>();
List list = this.loginService.findAppPrice();
for (int i = 0; i < list.size(); i++) {
Price price = (Price) list.get(i);
appList.put(price.getApp_name(), price.getApp_price().toString());
}
model.addAttribute("appList", appList);
try {
if (filename != null) {
String appName=filename.substring(0,filename.length()-4);
Double price = appstoreBillingService.appStoreBilling(appName);
Appstorebilling appstorebilling = new Appstorebilling();
appstorebilling.setUname(username);
appstorebilling.setApp_name(appName);
appstorebilling.setApp_price(price);
appstorebilling.setTime_stamp(new Timestamp(new Date().getTime()));
String downloadSize = this.paymentService.downloadFile(request, response, filename);
appstorebilling.setDownload_size(downloadSize);
appstoreBillingService.insertBillingInfo(appstorebilling);
getUserDetails(appstorebilling); //Method which i am invoking to GET JSON object
}
} catch (IOException e) {
e.printStackTrace();
}
return "loginsuccess";
}
我为获取 JSON 对象调用的方法
@RequestMapping(value = "/target",method = RequestMethod.GET)
public @ResponseBody Object getUserDetails(Appstorebilling appstorebilling) throws NoSuchAlgorithmException {
return appstorebilling;
}
当我尝试访问 url (machineip:portno/target) 时,我在 JSON 对象中得到空值 {"id":0,"uname":null,"app_name":null,"app_price":null,"time_stamp":null,"download_size":null}
【问题讨论】:
-
要么我误解了你的问题,要么你有不可能的期望。您从
loginSuccessPage方法调用getUserDetails(appstorebilling);。这会立即返回您作为参数传递的对象。您是否希望通过调用getUserDetails()方法来处理另一个 HTTP 请求以返回相同的对象? -
方法 loginSuccessPage 只不过是一个下载逻辑,当用户单击附加到该页面的特定文件时,它会被下载并返回到我们下载文件的 loginsuccess.jsp 页面本身。 getUserDetails 方法是在发生此下载操作时仅返回 Userdetails。用户详细信息,即 uname、下载时间、下载文件名等,例如 {"id":0,"uname":null,"app_name":null,"app_price":null,"time_stamp":null,"download_size" :空值}。实际上在这个我只是得到 null 而不是每个参数的值。
-
我的要求是两件事。一种是当我单击下载文件时,它应该通过返回同一页面(loginsuccess.jsp——现在可以正常工作)来执行文件下载,并将用户详细信息的 JSON 对象发布到特定的 URL(machineip:portno/target ----在我的情况下)这是行不通的。
-
我仍然不确定我是否需要 getUserDetails 方法来返回 JSON 对象,或者这可以在 loginSuccessPage 方法本身以及文件下载选项中处理。如果有怎么办。 @SotiriosDelimanolis
-
让我重新表述一下:为什么您希望对
machineip:portno/target的 GET 请求返回您当前看到的内容以外的任何内容?
标签: android json rest spring-mvc