如何使用 HTTP 请求发送和接收 QUERY 参数
How to send and recive a QUERY parameters with HTTP request
我试过用 http 请求发送查询参数,但我不知道该怎么做,这是我的实际控制器
package com.iquest.news.controller;
import com.iquest.news.dao.AbstractGenericDao;
import com.iquest.news.entities.News;
import com.iquest.news.services.ServiceInterface;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
public class NewsController {
@Autowired
private ServiceInterface<News> newsService;
Logger logger = Logger.getLogger(AbstractGenericDao.class);
@RequestMapping(value = "/", method = RequestMethod.GET)
public String showNews(Model model) {
List<News> news = newsService.getAll();
if (news.size() != 0) {
model.addAttribute("news", news);
logger.debug("CONTROLLER: News controller has executed with success");
return "news";
} else {
return "error";
}
}
@RequestMapping(value = "/startDate={date}", method = RequestMethod.GET)
public String getNewsByDate(@PathVariable long date, Model model) {
List<News> news = newsService.getNewsByDate(date);
if (news.size() != 0) {
model.addAttribute("news", news);
logger.debug("CONTROLLER: News controller has delivered data (from GET NEWS BY DATE) with success");
return "news";
} else {
return "error";
}
}
@RequestMapping(value = "/startDate={date}/author={author}", method = RequestMethod.GET)
public String getNewsByDateAndAuthor(@PathVariable long date, @PathVariable String author, Model model) {
List<News> news = newsService.getNewsByDateAndAuthor(date, author);
if (news.size() != 0) {
model.addAttribute("news", news);
logger.debug("CONTROLLER: News controller has delivered data (from GET NEWS BY DATE AND AUTHOR) with success");
return "news";
} else {
return "error";
}
}
}
这是我的 URL link 执行后:http://localhost:8080/news/startDate=1436529204/author=v
如何使 URL 看起来像:http://localhost:8080/news?startDate=1436529204?author=v 或类似的东西。
有谁知道我该怎么做?
感谢帮助:D
好吧,这里有一个方法可以做到这一点。
@RequestMapping(value={"/news"},method={org.springframework.web.bind.annotation.RequestMethod.POST,
org.springframework.web.bind.annotation.RequestMethod.GET})
public String getQueryParams(final Model model,final HttpServletRequest request){
String startDate= request.getParameter("startDate");
String author= request.getParameter("author");
}
实际上 "URL" 有一个以上的问号 ("?") 是无效的 URL。如果您正在寻找如何访问有效 URL(如 http://localhost:8080/news?startDate=1436529204&author=v)的查询参数,那么您的方法签名应如下所示:
@RequestMapping(value = "/", method = RequestMethod.GET)
public String getNewsByDateAndAuthor(@RequestParam("date") Long date, @RequestParam("author") String author, Model model) {
我试过用 http 请求发送查询参数,但我不知道该怎么做,这是我的实际控制器
package com.iquest.news.controller;
import com.iquest.news.dao.AbstractGenericDao;
import com.iquest.news.entities.News;
import com.iquest.news.services.ServiceInterface;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
public class NewsController {
@Autowired
private ServiceInterface<News> newsService;
Logger logger = Logger.getLogger(AbstractGenericDao.class);
@RequestMapping(value = "/", method = RequestMethod.GET)
public String showNews(Model model) {
List<News> news = newsService.getAll();
if (news.size() != 0) {
model.addAttribute("news", news);
logger.debug("CONTROLLER: News controller has executed with success");
return "news";
} else {
return "error";
}
}
@RequestMapping(value = "/startDate={date}", method = RequestMethod.GET)
public String getNewsByDate(@PathVariable long date, Model model) {
List<News> news = newsService.getNewsByDate(date);
if (news.size() != 0) {
model.addAttribute("news", news);
logger.debug("CONTROLLER: News controller has delivered data (from GET NEWS BY DATE) with success");
return "news";
} else {
return "error";
}
}
@RequestMapping(value = "/startDate={date}/author={author}", method = RequestMethod.GET)
public String getNewsByDateAndAuthor(@PathVariable long date, @PathVariable String author, Model model) {
List<News> news = newsService.getNewsByDateAndAuthor(date, author);
if (news.size() != 0) {
model.addAttribute("news", news);
logger.debug("CONTROLLER: News controller has delivered data (from GET NEWS BY DATE AND AUTHOR) with success");
return "news";
} else {
return "error";
}
}
}
这是我的 URL link 执行后:http://localhost:8080/news/startDate=1436529204/author=v
如何使 URL 看起来像:http://localhost:8080/news?startDate=1436529204?author=v 或类似的东西。 有谁知道我该怎么做? 感谢帮助:D
好吧,这里有一个方法可以做到这一点。
@RequestMapping(value={"/news"},method={org.springframework.web.bind.annotation.RequestMethod.POST,
org.springframework.web.bind.annotation.RequestMethod.GET})
public String getQueryParams(final Model model,final HttpServletRequest request){
String startDate= request.getParameter("startDate");
String author= request.getParameter("author");
}
实际上 "URL" 有一个以上的问号 ("?") 是无效的 URL。如果您正在寻找如何访问有效 URL(如 http://localhost:8080/news?startDate=1436529204&author=v)的查询参数,那么您的方法签名应如下所示:
@RequestMapping(value = "/", method = RequestMethod.GET)
public String getNewsByDateAndAuthor(@RequestParam("date") Long date, @RequestParam("author") String author, Model model) {