2016-02-11 1 views
0

Привет, я все вокруг, весна, отдых и сервлеты. Я создаю пример, который имеет функцию, которая получает 3 параметра от пользователя и печатает их в браузере. не не я запустить команду происходит некорректно:получить статус 400 все время при отправке параметров

http://localhost:8080/springexample/getMails/setup/user/1234/[email protected]/ 

и я получаю:

HTTP Status 400 - Required String parameter 'username' is not present 

type Status report 

message Required String parameter 'username' is not present 

description The request sent by the client was syntactically incorrect. 

это мой код:

package com.javacodegeeks.snippets.enterprise; 

import java.util.Map; 

import javax.servlet.http.HttpServletRequest; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 

@Controller 
@RequestMapping("/getMails") 
public class GetEmail { 

     @RequestMapping(value = "/setup/{username}/{pass}/{host}/", method = RequestMethod.GET) 
     public String Setup(@RequestParam("username") String username,@RequestParam("pass") String pass,@RequestParam("host") String host ,ModelMap model,HttpServletRequest request) { 
      model.addAttribute("msg","user = "+username+","+"pass = "+pass+","+"host = "+host); 
      return "helloWorld"; 
     } 

     @RequestMapping(value = "/displayMessage/{msg}", method = RequestMethod.GET) 
     public String displayMessage(@PathVariable String msg, ModelMap model) { 
      model.addAttribute("msg", msg); 
      return "helloWorld"; 
     } 

     @RequestMapping(value = "/knainz", method = RequestMethod.GET) 
     public String knainz(ModelMap model) { 
      model.addAttribute("msg", "JCG Hello World2222!"); 
      return "helloWorld"; 
     } 

     @RequestMapping(value = "/func1/{message}", method = RequestMethod.GET) 
     public String func1(@PathVariable String message,ModelMap model) { 
      model.addAttribute("msg", message); 
      return "helloWorld"; 
     } 

    } 

ответ

1

Вы путаете @RequestParam с @PathVariable. Изменить

@RequestMapping(value = "/setup/{username}/{pass}/{host}/", method = RequestMethod.GET) 
    public String Setup(@RequestParam("username") String username,@RequestParam("pass") String pass,@RequestParam("host") String host ,ModelMap model,HttpServletRequest request) { 
     model.addAttribute("msg","user = "+username+","+"pass = "+pass+","+"host = "+host); 
     return "helloWorld"; 
    } 

в

@RequestMapping(value = "/setup/{username}/{pass}/{host}/", method = RequestMethod.GET) 
    public String Setup(@PathVariable("username") String username,@PathVariable("pass") String pass,@PathVariable("host") String host ,ModelMap model,HttpServletRequest request) { 
     model.addAttribute("msg","user = "+username+","+"pass = "+pass+","+"host = "+host); 
     return "helloWorld"; 
    } 
+0

я попробовать это, но это не помогло. теперь у меня есть другая проблема, я меняю свое имя класса с «GetEmail» на «GetEmailMessages», и теперь я получил сообщение HTTP Status 500 с этим сообщением «Не удается сопоставить обработчик getEmail с URL-адресом [/ getMails/knainz]: уже есть обработчик типа [класс com.javacodegeeks.snippets.enterprise.GetEmailMessages]. –

+0

Ну, может быть, мы можем решить одну проблему за раз. Если изменение на '@ PathVariable' не сработало для вас, возможно, вам следует изменить свое имя класса на то, что было до этого, и повторить попытку. Если есть еще вопросы, то, пожалуйста, прокомментируйте. –

+0

Я удаляю проект и создаю его снова, и теперь ваше решение работает –

Смежные вопросы