【问题标题】:How to send and recive a QUERY parameters with HTTP request如何使用 HTTP 请求发送和接收 QUERY 参数
【发布时间】: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

【问题讨论】:

标签: java mysql spring http httprequest


【解决方案1】:

实际上,带有多个问号 (“?”) 的“URL”不是有效的 URL。如果您正在寻找如何访问像 http://localhost:8080/news?startDate=1436529204&author=v 这样的有效 URL 的查询参数,那么您的方法签名应该如下所示:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String getNewsByDateAndAuthor(@RequestParam("date") Long date, @RequestParam("author") String author, Model model) {

【讨论】:

  • 如果我使用它,i.imgur.com/7IZRZeB.png 并尝试使用该链接 localhost:8080/news?startDate=1436531644&author=v 这是我的结果,我不知道为什么不起作用: i.imgur.com/h7seoc5.png 每次我使用此链接时,他都会在此 localhost:8080/news/?startDate=1436531644&author=v 中自动更改,你知道我能做什么吗?
  • 我认为您的调度程序 servlet 映射有问题,请您分享一下。您可以在控制台“未找到映射”上看到相同的错误。
【解决方案2】:

嗯,这是一种方法。

@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");
}

【讨论】:

猜你喜欢
  • 2014-02-24
  • 2020-07-05
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
相关资源
最近更新 更多