0

ich zum ersten Mal meines Spring MVC-Projekt gebaut mit gradle bootRun mit der folgenden Controller-Klasse erfolgreich „Can not Symbols finden“:Gebäude Spring MVC-Anwendung, Regler Modell

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 

@Controller 
public class HelloController { 

    @RequestMapping("/") 
    public String hello() { 
    return "resultPage"; 
    } 
} 

Dann habe ich es Daten zu meiner Sicht Klasse weitergeben müssen:

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 

@Controller 
public class HelloController { 

    @RequestMapping("/") 
    public String hello(Model model) { 
    model.addAttribute("message", "Hello from the controller"); 
    return "resultPage"; 
    } 
} 

Wenn ich jetzt mein Projekt bauen, bekomme ich folgende Fehlermeldung:

HelloController.java:13: error: cannot find symbol 
    public String hello(Model model) { 
         ^
    symbol: class Model 
    location: class HelloController 
1 error 
:compileJava FAILED 

FAILURE: Build failed with an exception. 

Irgendwelche Ideen, was ich falsch mache?

+0

vielleicht ein fehlender Import? –

+0

@MatiasElorriaga Ja, richtig! Ich habe vor ein paar Minuten eine Antwort hinzugefügt. – KZcoding

Antwort

1

mir das Problem gelöst.

Wenn wir möchten, dass das DispatcherServlet das Modell in die Funktion einfügt, sollten wir die Model-Klasse importieren.

import org.springframework.ui.Model; 

Also änderte ich meine Controller-Klasse auf die folgenden und es hat funktioniert!

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.ui.Model; 

@Controller 
public class HelloController { 

    @RequestMapping("/") 
    public String hello(Model model) { 
    model.addAttribute("message", "Hello from the controller"); 
    return "resultPage"; 
    } 
}