【发布时间】:2015-07-10 12:07:47
【问题描述】:
我尝试使用 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 链接:http://localhost:8080/news/startDate=1436529204/author=v
我怎样才能使这个 URL 看起来像:http://localhost:8080/news?startDate=1436529204?author=v 或类似的东西。 有谁知道我该怎么做? 谢谢帮助:D
【问题讨论】:
-
看看here
-
您实际上必须绑定请求参数而不是路径参数。 docs.spring.io/spring/docs/current/spring-framework-reference/…
标签: java mysql spring http httprequest