2015-07-10 28 views
1

我尝试发送与HTTP请求的查询参数,但我不知道该怎么做,这是我的实际CONTROLER如何发送和recive与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或类似的东西。 有没有人有任何想法我可以做到这一点? Thx求救:D

+0

看看[这里](https://spring.io/guides/gs/rest -service /) –

+0

实际上您必须绑定请求参数,但不绑定路径参数。 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestparam –

回答

1

嗯,这是一种方法来做到这一点。

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

如果我使用这种方法,http://i.imgur.com/ ikskAta.png和这个链接:localhost:8080/news?startDate = 1436531644&author = v他在这个http:// localhost:8080/news /?startDate = 1436531644&author = v中自动改变我,我不知道为什么,做你有什么想法? –

+0

nvm,它的工作原理:D –

1

实际上带有多个问号(“?”)的“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) { 
+0

如果我使用,http://i.imgur.com/7IZRZeB.png并尝试使用该链接localhost:8080/news?startDate = 1436531644&author = v这里是我的结果,而我不知道为什么没有工作:http://i.imgur.com/h7seoc5.png每当我使用这个链接,他改变自动在这个http:// localhost:8080/news /?startDate = 1436531644&author =你知道我能做什么吗? –

+0

我相信你的调度程序servlet映射有问题,请你分享一下。您可以在控制台“未找到映射”上看到错误。 –